webui.go

  1package commands
  2
  3import (
  4	"bytes"
  5	"context"
  6	"encoding/json"
  7	"fmt"
  8	"io/ioutil"
  9	"log"
 10	"net/http"
 11	"os"
 12	"os/signal"
 13	"time"
 14
 15	"github.com/99designs/gqlgen/graphql/playground"
 16	"github.com/gorilla/mux"
 17	"github.com/phayes/freeport"
 18	"github.com/skratchdot/open-golang/open"
 19	"github.com/spf13/cobra"
 20
 21	"github.com/MichaelMure/git-bug/graphql"
 22	"github.com/MichaelMure/git-bug/identity"
 23	"github.com/MichaelMure/git-bug/repository"
 24	"github.com/MichaelMure/git-bug/util/git"
 25	"github.com/MichaelMure/git-bug/webui"
 26)
 27
 28var (
 29	webUIPort     int
 30	webUIOpen     bool
 31	webUINoOpen   bool
 32	webUIReadOnly bool
 33)
 34
 35const webUIOpenConfigKey = "git-bug.webui.open"
 36
 37func authMiddleware(repo repository.RepoCommon, id *identity.Identity) func(http.Handler) http.Handler {
 38	return func(next http.Handler) http.Handler {
 39		return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
 40			ctx := identity.AttachToContext(r.Context(), repo, id)
 41			next.ServeHTTP(w, r.WithContext(ctx))
 42		})
 43	}
 44}
 45
 46func runWebUI(cmd *cobra.Command, args []string) error {
 47	if webUIPort == 0 {
 48		var err error
 49		webUIPort, err = freeport.GetFreePort()
 50		if err != nil {
 51			return err
 52		}
 53	}
 54
 55	var id *identity.Identity
 56	if !webUIReadOnly {
 57		// Verify that we have an identity.
 58		var err error
 59		id, err = identity.GetUserIdentity(repo)
 60		if err != nil {
 61			return err
 62		}
 63	}
 64
 65	addr := fmt.Sprintf("127.0.0.1:%d", webUIPort)
 66	webUiAddr := fmt.Sprintf("http://%s", addr)
 67
 68	router := mux.NewRouter()
 69
 70	graphqlHandler, err := graphql.NewHandler(repo)
 71	if err != nil {
 72		return err
 73	}
 74
 75	assetsHandler := &fileSystemWithDefault{
 76		FileSystem:  webui.WebUIAssets,
 77		defaultFile: "index.html",
 78	}
 79
 80	// Routes
 81	router.Path("/playground").Handler(playground.Handler("git-bug", "/graphql"))
 82	router.Path("/graphql").Handler(graphqlHandler)
 83	router.Path("/gitfile/{hash}").Handler(newGitFileHandler(repo))
 84	router.Path("/upload").Methods("POST").Handler(newGitUploadFileHandler(repo))
 85	router.PathPrefix("/").Handler(http.FileServer(assetsHandler))
 86	router.Use(authMiddleware(repo, id))
 87
 88	srv := &http.Server{
 89		Addr:    addr,
 90		Handler: router,
 91	}
 92
 93	done := make(chan bool)
 94	quit := make(chan os.Signal, 1)
 95
 96	// register as handler of the interrupt signal to trigger the teardown
 97	signal.Notify(quit, os.Interrupt)
 98
 99	go func() {
100		<-quit
101		fmt.Println("WebUI is shutting down...")
102
103		ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
104		defer cancel()
105
106		srv.SetKeepAlivesEnabled(false)
107		if err := srv.Shutdown(ctx); err != nil {
108			log.Fatalf("Could not gracefully shutdown the WebUI: %v\n", err)
109		}
110
111		// Teardown
112		err := graphqlHandler.Close()
113		if err != nil {
114			fmt.Println(err)
115		}
116
117		close(done)
118	}()
119
120	fmt.Printf("Web UI: %s\n", webUiAddr)
121	fmt.Printf("Graphql API: http://%s/graphql\n", addr)
122	fmt.Printf("Graphql Playground: http://%s/playground\n", addr)
123	fmt.Println("Press Ctrl+c to quit")
124
125	configOpen, err := repo.LocalConfig().ReadBool(webUIOpenConfigKey)
126	if err == repository.ErrNoConfigEntry {
127		// default to true
128		configOpen = true
129	} else if err != nil {
130		return err
131	}
132
133	shouldOpen := (configOpen && !webUINoOpen) || webUIOpen
134
135	if shouldOpen {
136		err = open.Run(webUiAddr)
137		if err != nil {
138			fmt.Println(err)
139		}
140	}
141
142	err = srv.ListenAndServe()
143	if err != nil && err != http.ErrServerClosed {
144		return err
145	}
146
147	<-done
148
149	fmt.Println("WebUI stopped")
150	return nil
151}
152
153// implement a http.FileSystem that will serve a default file when the looked up
154// file doesn't exist. Useful for Single-Page App that implement routing client
155// side, where the server has to return the root index.html file for every route.
156type fileSystemWithDefault struct {
157	http.FileSystem
158	defaultFile string
159}
160
161func (fswd *fileSystemWithDefault) Open(name string) (http.File, error) {
162	f, err := fswd.FileSystem.Open(name)
163	if os.IsNotExist(err) {
164		return fswd.FileSystem.Open(fswd.defaultFile)
165	}
166	return f, err
167}
168
169// implement a http.Handler that will read and server git blob.
170type gitFileHandler struct {
171	repo repository.Repo
172}
173
174func newGitFileHandler(repo repository.Repo) http.Handler {
175	return &gitFileHandler{
176		repo: repo,
177	}
178}
179
180func (gfh *gitFileHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
181	hash := git.Hash(mux.Vars(r)["hash"])
182
183	if !hash.IsValid() {
184		http.Error(rw, "invalid git hash", http.StatusBadRequest)
185		return
186	}
187
188	// TODO: this mean that the whole file will he buffered in memory
189	// This can be a problem for big files. There might be a way around
190	// that by implementing a io.ReadSeeker that would read and discard
191	// data when a seek is called.
192	data, err := gfh.repo.ReadData(git.Hash(hash))
193	if err != nil {
194		http.Error(rw, err.Error(), http.StatusInternalServerError)
195		return
196	}
197
198	http.ServeContent(rw, r, "", time.Now(), bytes.NewReader(data))
199}
200
201// implement a http.Handler that will accept and store content into git blob.
202type gitUploadFileHandler struct {
203	repo repository.Repo
204}
205
206func newGitUploadFileHandler(repo repository.Repo) http.Handler {
207	return &gitUploadFileHandler{
208		repo: repo,
209	}
210}
211
212func (gufh *gitUploadFileHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
213	if identity.ForContext(r.Context(), gufh.repo) == nil {
214		http.Error(rw, fmt.Sprintf("read-only mode or not logged in"), http.StatusForbidden)
215		return
216	}
217
218	// 100MB (github limit)
219	var maxUploadSize int64 = 100 * 1000 * 1000
220	r.Body = http.MaxBytesReader(rw, r.Body, maxUploadSize)
221	if err := r.ParseMultipartForm(maxUploadSize); err != nil {
222		http.Error(rw, "file too big (100MB max)", http.StatusBadRequest)
223		return
224	}
225
226	file, _, err := r.FormFile("uploadfile")
227	if err != nil {
228		http.Error(rw, "invalid file", http.StatusBadRequest)
229		return
230	}
231	defer file.Close()
232	fileBytes, err := ioutil.ReadAll(file)
233	if err != nil {
234		http.Error(rw, "invalid file", http.StatusBadRequest)
235		return
236	}
237
238	filetype := http.DetectContentType(fileBytes)
239	if filetype != "image/jpeg" && filetype != "image/jpg" &&
240		filetype != "image/gif" && filetype != "image/png" {
241		http.Error(rw, "invalid file type", http.StatusBadRequest)
242		return
243	}
244
245	hash, err := gufh.repo.StoreData(fileBytes)
246	if err != nil {
247		http.Error(rw, err.Error(), http.StatusInternalServerError)
248		return
249	}
250
251	type response struct {
252		Hash string `json:"hash"`
253	}
254
255	resp := response{Hash: string(hash)}
256
257	js, err := json.Marshal(resp)
258	if err != nil {
259		http.Error(rw, err.Error(), http.StatusInternalServerError)
260		return
261	}
262
263	rw.Header().Set("Content-Type", "application/json")
264	_, err = rw.Write(js)
265	if err != nil {
266		http.Error(rw, err.Error(), http.StatusInternalServerError)
267		return
268	}
269}
270
271var webUICmd = &cobra.Command{
272	Use:   "webui",
273	Short: "Launch the web UI.",
274	Long: `Launch the web UI.
275
276Available git config:
277  git-bug.webui.open [bool]: control the automatic opening of the web UI in the default browser
278`,
279	PreRunE: loadRepo,
280	RunE:    runWebUI,
281}
282
283func init() {
284	RootCmd.AddCommand(webUICmd)
285
286	webUICmd.Flags().SortFlags = false
287
288	webUICmd.Flags().BoolVar(&webUIOpen, "open", false, "Automatically open the web UI in the default browser")
289	webUICmd.Flags().BoolVar(&webUINoOpen, "no-open", false, "Prevent the automatic opening of the web UI in the default browser")
290	webUICmd.Flags().IntVarP(&webUIPort, "port", "p", 0, "Port to listen to (default is random)")
291	webUICmd.Flags().BoolVar(&webUIReadOnly, "read-only", false, "Whether to run the web UI in read-only mode")
292
293}