/
no_production.go
92 lines (84 loc) 路 2.63 KB
/
no_production.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//go:build !production
package cleosrv
import (
"fmt"
"net/http"
"path/filepath"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/handler/transport"
"github.com/adrg/xdg"
"github.com/go-chi/chi/v5"
"github.com/gorilla/websocket"
"github.com/rs/cors"
)
func serveFrontend(router chi.Router, frontendFooterText string) {
fmt.Println("Serving without frontend")
router.HandleFunc("/", dummyFrontendHandler)
}
func dummyFrontendHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, `<h1>Dev Mode</h1>
<p>You are currently executing without frontend (e.g. because you are
developing).</p>
<h1>GraphQL API</h1>
<p>Access the API under <a href="/query">/query</a></p>
<p>Access the playground under <a href="/playground/">/playground/</a></p>
<h1>Start Dev Frontend</h1>
To start the frontend during development as a separate process:
<pre>
cd frontend
npm start
</pre>
Then access it under <a href="http://localhost:3000">http://localhost:3000</a></p>
<h1>Embed Frontend</h1>
<p>To build and embed the frontend execute:
<pre>
make build
</pre>
And start the resulting <i>cleosrv</i> binary you can find in the dist/
directory.
</p>
`,
)
}
// configureCORS allows everything during development/testing.
func configureCORS(router *chi.Mux, srv *handler.Server) {
fmt.Println("Enabling very permissive CORS")
// Add CORS middleware around every request
// See https://github.com/rs/cors for full option listing
router.Use(cors.New(cors.Options{
AllowedOrigins: []string{"*"},
AllowedHeaders: []string{"*"},
AllowedMethods: []string{
http.MethodHead,
http.MethodGet,
http.MethodPost,
http.MethodPut,
http.MethodPatch,
http.MethodDelete,
},
// It's against the CORS spec to allow credentialed requests and
// asterisk * for Access-Control-Allow-Origin,
// Access-Control-Allow-Headers or Access-Control-Allow-Methods .
// https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
AllowCredentials: false,
Debug: true,
}).Handler)
// The following code was copied and slightly modified from the following
// link, but it's not clear if or why it's needed since we are not using
// websockets right now. Just in case, I'll leave it.
// https://gqlgen.com/recipes/cors/
// See also: https://github.com/99designs/gqlgen/issues/1250
srv.AddTransport(&transport.Websocket{
Upgrader: websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
// Check against your desired domains here
return true
},
ReadBufferSize: 1024,
WriteBufferSize: 1024,
},
})
}
func DefaultDatabasePath() string {
return filepath.Join(xdg.DataHome, "cleosrv", "cleosrv_dev.db")
}