webui.go

  1package commands
  2
  3import (
  4	"context"
  5	"errors"
  6	"fmt"
  7	"io"
  8	"log"
  9	"net"
 10	"net/http"
 11	"net/url"
 12	"os"
 13	"os/signal"
 14	"strconv"
 15	"time"
 16
 17	"github.com/99designs/gqlgen/graphql/playground"
 18	"github.com/gorilla/mux"
 19	"github.com/phayes/freeport"
 20	"github.com/skratchdot/open-golang/open"
 21	"github.com/spf13/cobra"
 22
 23	"github.com/MichaelMure/git-bug/api/auth"
 24	"github.com/MichaelMure/git-bug/api/graphql"
 25	httpapi "github.com/MichaelMure/git-bug/api/http"
 26	"github.com/MichaelMure/git-bug/cache"
 27	"github.com/MichaelMure/git-bug/commands/execenv"
 28	"github.com/MichaelMure/git-bug/entities/identity"
 29	"github.com/MichaelMure/git-bug/repository"
 30	"github.com/MichaelMure/git-bug/webui"
 31)
 32
 33const webUIOpenConfigKey = "git-bug.webui.open"
 34
 35type webUIOptions struct {
 36	host      string
 37	port      int
 38	open      bool
 39	noOpen    bool
 40	readOnly  bool
 41	logErrors bool
 42	query     string
 43}
 44
 45func newWebUICommand() *cobra.Command {
 46	env := execenv.NewEnv()
 47	options := webUIOptions{}
 48
 49	cmd := &cobra.Command{
 50		Use:   "webui",
 51		Short: "Launch the web UI",
 52		Long: `Launch the web UI.
 53
 54Available git config:
 55  git-bug.webui.open [bool]: control the automatic opening of the web UI in the default browser
 56`,
 57		PreRunE: execenv.LoadRepo(env),
 58		RunE: func(cmd *cobra.Command, args []string) error {
 59			return runWebUI(env, options)
 60		},
 61	}
 62
 63	flags := cmd.Flags()
 64	flags.SortFlags = false
 65
 66	flags.StringVar(&options.host, "host", "127.0.0.1", "Network address or hostname to listen to (default to 127.0.0.1)")
 67	flags.BoolVar(&options.open, "open", false, "Automatically open the web UI in the default browser")
 68	flags.BoolVar(&options.noOpen, "no-open", false, "Prevent the automatic opening of the web UI in the default browser")
 69	flags.IntVarP(&options.port, "port", "p", 0, "Port to listen to (default to random available port)")
 70	flags.BoolVar(&options.readOnly, "read-only", false, "Whether to run the web UI in read-only mode")
 71	flags.BoolVar(&options.logErrors, "log-errors", false, "Whether to log errors")
 72	flags.StringVarP(&options.query, "query", "q", "", "The query to open in the web UI bug list")
 73
 74	return cmd
 75}
 76
 77func runWebUI(env *execenv.Env, opts webUIOptions) error {
 78	if opts.port == 0 {
 79		var err error
 80		opts.port, err = freeport.GetFreePort()
 81		if err != nil {
 82			return err
 83		}
 84	}
 85
 86	addr := net.JoinHostPort(opts.host, strconv.Itoa(opts.port))
 87	webUiAddr := fmt.Sprintf("http://%s", addr)
 88	toOpen := webUiAddr
 89
 90	if len(opts.query) > 0 {
 91		// Explicitly set the query parameter instead of going with a default one.
 92		toOpen = fmt.Sprintf("%s/?q=%s", webUiAddr, url.QueryEscape(opts.query))
 93	}
 94
 95	router := mux.NewRouter()
 96
 97	// If the webUI is not read-only, use an authentication middleware with a
 98	// fixed identity: the default user of the repo
 99	// TODO: support dynamic authentication with OAuth
100	if !opts.readOnly {
101		author, err := identity.GetUserIdentity(env.Repo)
102		if err != nil {
103			return err
104		}
105		router.Use(auth.Middleware(author.Id()))
106	}
107
108	mrc := cache.NewMultiRepoCache()
109	_, events, err := mrc.RegisterDefaultRepository(env.Repo)
110	if err != nil {
111		return err
112	}
113
114	for event := range events {
115		if event.Err != nil {
116			env.Err.Printf("Cache building error [%s]: %v\n", event.Typename, event.Err)
117			continue
118		}
119		switch event.Event {
120		case cache.BuildEventCacheIsBuilt:
121			env.Err.Println("Building cache... ")
122		case cache.BuildEventStarted:
123			env.Err.Printf("[%s] started\n", event.Typename)
124		case cache.BuildEventFinished:
125			env.Err.Printf("[%s] done\n", event.Typename)
126		}
127	}
128
129	var errOut io.Writer
130	if opts.logErrors {
131		errOut = env.Err
132	}
133
134	graphqlHandler := graphql.NewHandler(mrc, errOut)
135
136	// Routes
137	router.Path("/playground").Handler(playground.Handler("git-bug", "/graphql"))
138	router.Path("/graphql").Handler(graphqlHandler)
139	router.Path("/gitfile/{repo}/{hash}").Handler(httpapi.NewGitFileHandler(mrc))
140	router.Path("/upload/{repo}").Methods("POST").Handler(httpapi.NewGitUploadFileHandler(mrc))
141	router.PathPrefix("/").Handler(webui.NewHandler())
142
143	srv := &http.Server{
144		Addr:    addr,
145		Handler: router,
146	}
147
148	done := make(chan bool)
149	quit := make(chan os.Signal, 1)
150
151	// register as handler of the interrupt signal to trigger the teardown
152	signal.Notify(quit, os.Interrupt)
153
154	go func() {
155		<-quit
156		env.Out.Println("WebUI is shutting down...")
157
158		ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
159		defer cancel()
160
161		srv.SetKeepAlivesEnabled(false)
162		if err := srv.Shutdown(ctx); err != nil {
163			log.Fatalf("Could not gracefully shutdown the WebUI: %v\n", err)
164		}
165
166		// Teardown
167		err := graphqlHandler.Close()
168		if err != nil {
169			env.Out.Println(err)
170		}
171
172		close(done)
173	}()
174
175	env.Out.Printf("Web UI: %s\n", webUiAddr)
176	env.Out.Printf("Graphql API: http://%s/graphql\n", addr)
177	env.Out.Printf("Graphql Playground: http://%s/playground\n", addr)
178	env.Out.Println("Press Ctrl+c to quit")
179
180	configOpen, err := env.Repo.AnyConfig().ReadBool(webUIOpenConfigKey)
181	if errors.Is(err, repository.ErrNoConfigEntry) {
182		// default to true
183		configOpen = true
184	} else if err != nil {
185		return err
186	}
187
188	shouldOpen := (configOpen && !opts.noOpen) || opts.open
189
190	if shouldOpen {
191		err = open.Run(toOpen)
192		if err != nil {
193			env.Out.Println(err)
194		}
195	}
196
197	err = srv.ListenAndServe()
198	if err != nil && err != http.ErrServerClosed {
199		return err
200	}
201
202	<-done
203
204	env.Out.Println("WebUI stopped")
205	return nil
206}