aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2018-09-21 11:53:31 +0200
committerMichael Muré <batolettre@gmail.com>2018-09-21 11:57:45 +0200
commit7c63417e8f1b3556b524b6f5bd66d93e534381cd (patch)
tree0c713fdff4434cc03df61c32923b8144c27f38ad
parent8a03853869136f4984f7f9e0457cccfaa629b3ea (diff)
downloadgit-bug-7c63417e8f1b3556b524b6f5bd66d93e534381cd.tar.gz
webui: serve the index.html file by default to deal with the SPA router requirements
fix #50
-rw-r--r--commands/webui.go25
1 files changed, 24 insertions, 1 deletions
diff --git a/commands/webui.go b/commands/webui.go
index aea8295a..b1bccd5c 100644
--- a/commands/webui.go
+++ b/commands/webui.go
@@ -44,12 +44,17 @@ func runWebUI(cmd *cobra.Command, args []string) error {
return err
}
+ assetsHandler := &fileSystemWithDefault{
+ FileSystem: webui.WebUIAssets,
+ defaultFile: "index.html",
+ }
+
// Routes
router.Path("/playground").Handler(handler.Playground("git-bug", "/graphql"))
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))
+ router.PathPrefix("/").Handler(http.FileServer(assetsHandler))
srv := &http.Server{
Addr: addr,
@@ -104,6 +109,23 @@ func runWebUI(cmd *cobra.Command, args []string) error {
return nil
}
+// 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.
type gitFileHandler struct {
repo repository.Repo
}
@@ -135,6 +157,7 @@ func (gfh *gitFileHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
http.ServeContent(rw, r, "", time.Now(), bytes.NewReader(data))
}
+// implement a http.Handler that will accept and store content into git blob.
type gitUploadFileHandler struct {
repo repository.Repo
}