/
serve.go
63 lines (48 loc) · 1.39 KB
/
serve.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
package cmd
import (
"io/ioutil"
"net/http"
"path/filepath"
"github.com/cueblox/blox/content"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
)
type bloxServeCmd struct {
cmd *cobra.Command
}
func newBloxServeCmd() *bloxServeCmd {
root := &bloxServeCmd{}
cmd := &cobra.Command{
Use: "serve",
Short: "Serve a GraphQL API",
Run: func(cmd *cobra.Command, args []string) {
userConfig, err := ioutil.ReadFile("blox.cue")
pterm.Debug.Printf("loading user config")
cobra.CheckErr(err)
repo, err := content.NewService(string(userConfig), referentialIntegrity)
cobra.CheckErr(err)
if static {
staticDir, err := repo.Cfg.GetString("static_dir")
pterm.Info.Printf("Serving static files from %s\n", staticDir)
cobra.CheckErr(err)
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir(filepath.Join(".", staticDir)))))
}
hf, err := repo.GQLHandlerFunc()
cobra.CheckErr(err)
http.HandleFunc("/", hf)
h, err := repo.GQLPlaygroundHandler()
cobra.CheckErr(err)
http.Handle("/ui", h)
pterm.Info.Printf("Server is running at %s\n", address)
cobra.CheckErr(http.ListenAndServe(address, nil))
},
}
cmd.Flags().BoolVarP(&static, "static", "s", true, "Serve static files")
cmd.Flags().StringVarP(&address, "address", "a", ":8080", "Listen address")
root.cmd = cmd
return root
}
var (
static bool
address string
)