2018-07-16 23:25:50 +03:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2018-08-02 23:33:45 +03:00
|
|
|
"bytes"
|
2018-08-21 19:46:53 +03:00
|
|
|
"context"
|
2018-08-02 23:33:45 +03:00
|
|
|
"encoding/json"
|
2018-07-16 23:25:50 +03:00
|
|
|
"fmt"
|
2018-08-13 19:32:11 +03:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2018-08-21 19:46:53 +03:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2018-08-13 19:32:11 +03:00
|
|
|
"time"
|
|
|
|
|
2018-09-14 13:40:31 +03:00
|
|
|
"github.com/99designs/gqlgen/handler"
|
2018-07-29 19:58:42 +03:00
|
|
|
"github.com/MichaelMure/git-bug/graphql"
|
2018-08-02 23:33:45 +03:00
|
|
|
"github.com/MichaelMure/git-bug/repository"
|
2018-09-11 23:04:16 +03:00
|
|
|
"github.com/MichaelMure/git-bug/util/git"
|
2018-07-16 23:25:50 +03:00
|
|
|
"github.com/MichaelMure/git-bug/webui"
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/phayes/freeport"
|
|
|
|
"github.com/skratchdot/open-golang/open"
|
2018-07-19 13:30:25 +03:00
|
|
|
"github.com/spf13/cobra"
|
2018-07-16 23:25:50 +03:00
|
|
|
)
|
|
|
|
|
2018-07-22 01:16:51 +03:00
|
|
|
var port int
|
|
|
|
|
2018-07-19 13:30:25 +03:00
|
|
|
func runWebUI(cmd *cobra.Command, args []string) error {
|
2018-07-22 01:16:51 +03:00
|
|
|
if port == 0 {
|
|
|
|
var err error
|
|
|
|
port, err = freeport.GetFreePort()
|
|
|
|
if err != nil {
|
2018-07-23 01:04:46 +03:00
|
|
|
return err
|
2018-07-22 01:16:51 +03:00
|
|
|
}
|
2018-07-16 23:25:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
addr := fmt.Sprintf("127.0.0.1:%d", port)
|
2018-07-19 15:15:50 +03:00
|
|
|
webUiAddr := fmt.Sprintf("http://%s", addr)
|
|
|
|
|
2018-07-16 23:25:50 +03:00
|
|
|
router := mux.NewRouter()
|
2018-07-19 15:15:50 +03:00
|
|
|
|
2018-08-21 19:46:53 +03:00
|
|
|
graphqlHandler, err := graphql.NewHandler(repo)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-09-21 12:53:31 +03:00
|
|
|
assetsHandler := &fileSystemWithDefault{
|
|
|
|
FileSystem: webui.WebUIAssets,
|
|
|
|
defaultFile: "index.html",
|
|
|
|
}
|
|
|
|
|
2018-07-19 15:15:50 +03:00
|
|
|
// Routes
|
2018-07-29 19:11:33 +03:00
|
|
|
router.Path("/playground").Handler(handler.Playground("git-bug", "/graphql"))
|
2018-08-21 19:46:53 +03:00
|
|
|
router.Path("/graphql").Handler(graphqlHandler)
|
2018-08-02 23:33:45 +03:00
|
|
|
router.Path("/gitfile/{hash}").Handler(newGitFileHandler(repo))
|
|
|
|
router.Path("/upload").Methods("POST").Handler(newGitUploadFileHandler(repo))
|
2018-09-21 12:53:31 +03:00
|
|
|
router.PathPrefix("/").Handler(http.FileServer(assetsHandler))
|
2018-07-16 23:25:50 +03:00
|
|
|
|
2018-08-21 19:46:53 +03:00
|
|
|
srv := &http.Server{
|
|
|
|
Addr: addr,
|
|
|
|
Handler: router,
|
|
|
|
}
|
|
|
|
|
|
|
|
done := make(chan bool)
|
|
|
|
quit := make(chan os.Signal, 1)
|
|
|
|
|
|
|
|
// register as handler of the interrupt signal to trigger the teardown
|
|
|
|
signal.Notify(quit, os.Interrupt)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
<-quit
|
|
|
|
fmt.Println("WebUI is shutting down...")
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
srv.SetKeepAlivesEnabled(false)
|
|
|
|
if err := srv.Shutdown(ctx); err != nil {
|
|
|
|
log.Fatalf("Could not gracefully shutdown the WebUI: %v\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Teardown
|
|
|
|
err := graphqlHandler.Close()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
|
|
|
|
fmt.Printf("Web UI: %s\n", webUiAddr)
|
|
|
|
fmt.Printf("Graphql API: http://%s/graphql\n", addr)
|
|
|
|
fmt.Printf("Graphql Playground: http://%s/playground\n", addr)
|
2018-09-19 22:12:37 +03:00
|
|
|
fmt.Println("Press Ctrl+c to quit")
|
2018-08-21 19:46:53 +03:00
|
|
|
|
2018-09-13 13:43:47 +03:00
|
|
|
err = open.Run(webUiAddr)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
2018-07-16 23:25:50 +03:00
|
|
|
|
2018-08-21 19:46:53 +03:00
|
|
|
err = srv.ListenAndServe()
|
|
|
|
if err != nil && err != http.ErrServerClosed {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
<-done
|
2018-07-16 23:25:50 +03:00
|
|
|
|
2018-08-21 19:46:53 +03:00
|
|
|
fmt.Println("WebUI stopped")
|
2018-07-16 23:25:50 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-21 12:53:31 +03:00
|
|
|
// implement a http.FileSystem that will serve a default file when the looked up
|
|
|
|
// file doesn't exist. Useful for Single-Page App that implement routing client
|
|
|
|
// side, where the server has to return the root index.html file for every route.
|
|
|
|
type fileSystemWithDefault struct {
|
|
|
|
http.FileSystem
|
|
|
|
defaultFile string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fswd *fileSystemWithDefault) Open(name string) (http.File, error) {
|
|
|
|
f, err := fswd.FileSystem.Open(name)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return fswd.FileSystem.Open(fswd.defaultFile)
|
|
|
|
}
|
|
|
|
return f, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// implement a http.Handler that will read and server git blob.
|
2018-08-02 23:33:45 +03:00
|
|
|
type gitFileHandler struct {
|
|
|
|
repo repository.Repo
|
|
|
|
}
|
|
|
|
|
|
|
|
func newGitFileHandler(repo repository.Repo) http.Handler {
|
|
|
|
return &gitFileHandler{
|
|
|
|
repo: repo,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gfh *gitFileHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
|
2018-09-11 23:04:16 +03:00
|
|
|
hash := git.Hash(mux.Vars(r)["hash"])
|
2018-08-02 23:33:45 +03:00
|
|
|
|
2018-08-03 00:37:49 +03:00
|
|
|
if !hash.IsValid() {
|
2018-08-02 23:33:45 +03:00
|
|
|
http.Error(rw, "invalid git hash", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: this mean that the whole file will he buffered in memory
|
|
|
|
// This can be a problem for big files. There might be a way around
|
|
|
|
// that by implementing a io.ReadSeeker that would read and discard
|
|
|
|
// data when a seek is called.
|
2018-09-11 23:04:16 +03:00
|
|
|
data, err := gfh.repo.ReadData(git.Hash(hash))
|
2018-08-02 23:33:45 +03:00
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
http.ServeContent(rw, r, "", time.Now(), bytes.NewReader(data))
|
|
|
|
}
|
|
|
|
|
2018-09-21 12:53:31 +03:00
|
|
|
// implement a http.Handler that will accept and store content into git blob.
|
2018-08-02 23:33:45 +03:00
|
|
|
type gitUploadFileHandler struct {
|
|
|
|
repo repository.Repo
|
|
|
|
}
|
|
|
|
|
|
|
|
func newGitUploadFileHandler(repo repository.Repo) http.Handler {
|
|
|
|
return &gitUploadFileHandler{
|
|
|
|
repo: repo,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gufh *gitUploadFileHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
// 100MB (github limit)
|
|
|
|
var maxUploadSize int64 = 100 * 1000 * 1000
|
|
|
|
r.Body = http.MaxBytesReader(rw, r.Body, maxUploadSize)
|
|
|
|
if err := r.ParseMultipartForm(maxUploadSize); err != nil {
|
|
|
|
http.Error(rw, "file too big (100MB max)", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
file, _, err := r.FormFile("uploadfile")
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, "invalid file", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
fileBytes, err := ioutil.ReadAll(file)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, "invalid file", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
filetype := http.DetectContentType(fileBytes)
|
|
|
|
if filetype != "image/jpeg" && filetype != "image/jpg" &&
|
|
|
|
filetype != "image/gif" && filetype != "image/png" {
|
|
|
|
http.Error(rw, "invalid file type", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
hash, err := gufh.repo.StoreData(fileBytes)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
type response struct {
|
|
|
|
Hash string `json:"hash"`
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := response{Hash: string(hash)}
|
|
|
|
|
|
|
|
js, err := json.Marshal(resp)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
rw.Header().Set("Content-Type", "application/json")
|
2018-09-13 13:43:47 +03:00
|
|
|
_, err = rw.Write(js)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2018-08-02 23:33:45 +03:00
|
|
|
}
|
|
|
|
|
2018-07-19 13:30:25 +03:00
|
|
|
var webUICmd = &cobra.Command{
|
|
|
|
Use: "webui",
|
|
|
|
Short: "Launch the web UI",
|
|
|
|
RunE: runWebUI,
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2018-07-20 16:46:14 +03:00
|
|
|
RootCmd.AddCommand(webUICmd)
|
2018-09-10 19:16:16 +03:00
|
|
|
|
|
|
|
webUICmd.Flags().SortFlags = false
|
|
|
|
|
2018-07-22 01:16:51 +03:00
|
|
|
webUICmd.Flags().IntVarP(&port, "port", "p", 0, "Port to listen to")
|
2018-07-16 23:25:50 +03:00
|
|
|
}
|