aboutsummaryrefslogtreecommitdiffstats
path: root/commands
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2018-08-21 18:46:53 +0200
committerMichael Muré <batolettre@gmail.com>2018-08-21 19:13:08 +0200
commit6d7dc465d881d0d04b01dfb6e09870346216d2d0 (patch)
treefbbce01c6b7634556303a3d917d1036767280d17 /commands
parent942178288d657202b3f7afd386b319a245afbb7e (diff)
downloadgit-bug-6d7dc465d881d0d04b01dfb6e09870346216d2d0.tar.gz
cache: lock the repo with a pid file; automatic cleaning
Diffstat (limited to 'commands')
-rw-r--r--commands/root.go9
-rw-r--r--commands/webui.go58
2 files changed, 61 insertions, 6 deletions
diff --git a/commands/root.go b/commands/root.go
index bbf7d6de..9435ce64 100644
--- a/commands/root.go
+++ b/commands/root.go
@@ -5,6 +5,7 @@ import (
"os"
"github.com/MichaelMure/git-bug/bug"
+ "github.com/MichaelMure/git-bug/cache"
"github.com/MichaelMure/git-bug/repository"
"github.com/spf13/cobra"
)
@@ -68,5 +69,13 @@ func loadRepo(cmd *cobra.Command, args []string) error {
return err
}
+ // Prevent the command from running when the cache has locked the repo
+ // Todo: make it more fine-grained at first
+ // Todo: make the running cache available for other processes
+ err = cache.RepoIsAvailable(repo)
+ if err != nil {
+ return err
+ }
+
return nil
}
diff --git a/commands/webui.go b/commands/webui.go
index 9ebced09..f3f9c184 100644
--- a/commands/webui.go
+++ b/commands/webui.go
@@ -2,11 +2,14 @@ package commands
import (
"bytes"
+ "context"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
+ "os"
+ "os/signal"
"time"
"github.com/MichaelMure/git-bug/graphql"
@@ -34,23 +37,66 @@ func runWebUI(cmd *cobra.Command, args []string) error {
addr := fmt.Sprintf("127.0.0.1:%d", port)
webUiAddr := fmt.Sprintf("http://%s", addr)
- 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)
-
router := mux.NewRouter()
+ graphqlHandler, err := graphql.NewHandler(repo)
+ if err != nil {
+ return err
+ }
+
// Routes
router.Path("/playground").Handler(handler.Playground("git-bug", "/graphql"))
- router.Path("/graphql").Handler(graphql.NewHandler(repo))
+ router.Path("/graphql").Handler(graphqlHandler)
router.Path("/gitfile/{hash}").Handler(newGitFileHandler(repo))
router.Path("/upload").Methods("POST").Handler(newGitUploadFileHandler(repo))
router.PathPrefix("/").Handler(http.FileServer(webui.WebUIAssets))
+ 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)
+
open.Run(webUiAddr)
- log.Fatal(http.ListenAndServe(addr, router))
+ err = srv.ListenAndServe()
+ if err != nil && err != http.ErrServerClosed {
+ return err
+ }
+
+ <-done
+ fmt.Println("WebUI stopped")
return nil
}