add support for static web

This commit is contained in:
eugene
2017-12-22 13:43:51 -06:00
parent 983606ecd6
commit 3a26eec1fc
2 changed files with 31 additions and 0 deletions
+25
View File
@@ -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) {
+6
View File
@@ -0,0 +1,6 @@
<html>
<title>remark</title>
<body>
blah blah blah
</body>
</html>