From 3a26eec1fc787ddf02cea23c054e5e8e54deff58 Mon Sep 17 00:00:00 2001 From: eugene Date: Fri, 22 Dec 2017 13:43:51 -0600 Subject: [PATCH] add support for static web --- app/rest/server.go | 25 +++++++++++++++++++++++++ web/test.html | 6 ++++++ 2 files changed, 31 insertions(+) create mode 100644 web/test.html diff --git a/app/rest/server.go b/app/rest/server.go index 9d5bcb81..de18356e 100644 --- a/app/rest/server.go +++ b/app/rest/server.go @@ -8,6 +8,8 @@ import ( "strings" "time" + "path/filepath" + "github.com/go-chi/chi" "github.com/go-chi/chi/middleware" "github.com/go-chi/render" @@ -55,9 +57,32 @@ func (s *Server) Run() { }) }) + s.addFileServer(router, "/web", http.Dir(filepath.Join(".", "web"))) + log.Fatal(http.ListenAndServe(":8080", router)) } +func (s *Server) addFileServer(r chi.Router, path string, root http.FileSystem) { + fs := http.StripPrefix(path, http.FileServer(root)) + + if path != "/" && path[len(path)-1] != '/' { + r.Get(path, http.RedirectHandler(path+"/", 301).ServeHTTP) + path += "/" + } + path += "*" + + r.Get(path, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + log.Print(r.URL.Path) + // don't show dirs, just serve files + if strings.HasSuffix(r.URL.Path, "/") { + http.NotFound(w, r) + return + } + + fs.ServeHTTP(w, r) + })) +} + // POST /comment func (s *Server) createCommentCtrl(w http.ResponseWriter, r *http.Request) { diff --git a/web/test.html b/web/test.html new file mode 100644 index 00000000..f80af096 --- /dev/null +++ b/web/test.html @@ -0,0 +1,6 @@ + + remark + + blah blah blah + +