1package commands
2
3import (
4 "fmt"
5 "github.com/MichaelMure/git-bug/graphql"
6 "github.com/MichaelMure/git-bug/webui"
7 "github.com/gorilla/mux"
8 "github.com/phayes/freeport"
9 "github.com/skratchdot/open-golang/open"
10 "github.com/spf13/cobra"
11 "github.com/vektah/gqlgen/handler"
12 "log"
13 "net/http"
14)
15
16var port int
17
18func runWebUI(cmd *cobra.Command, args []string) error {
19 if port == 0 {
20 var err error
21 port, err = freeport.GetFreePort()
22 if err != nil {
23 return err
24 }
25 }
26
27 addr := fmt.Sprintf("127.0.0.1:%d", port)
28 webUiAddr := fmt.Sprintf("http://%s", addr)
29
30 fmt.Printf("Web UI: %s\n", webUiAddr)
31 fmt.Printf("Graphql API: http://%s/graphql\n", addr)
32 fmt.Printf("Graphql Playground: http://%s/playground\n", addr)
33
34 router := mux.NewRouter()
35
36 // Routes
37 router.Path("/playground").Handler(handler.Playground("git-bug", "/graphql"))
38 router.Path("/graphql").Handler(graphql.NewHandler(repo))
39 router.PathPrefix("/").Handler(http.FileServer(webui.WebUIAssets))
40
41 open.Run(webUiAddr)
42
43 log.Fatal(http.ListenAndServe(addr, router))
44
45 return nil
46}
47
48var webUICmd = &cobra.Command{
49 Use: "webui",
50 Short: "Launch the web UI",
51 RunE: runWebUI,
52}
53
54func init() {
55 RootCmd.AddCommand(webUICmd)
56 webUICmd.Flags().IntVarP(&port, "port", "p", 0, "Port to listen to")
57}