Add routing for basic web ui

This commit is contained in:
Deluan
2020-01-19 19:34:54 -05:00
parent 5bc1551b09
commit 3a03284c59
8 changed files with 112 additions and 29 deletions
+4 -22
View File
@@ -4,7 +4,6 @@ import (
"net/http"
"os"
"path/filepath"
"strings"
"time"
"github.com/cloudsonic/sonic-server/conf"
@@ -34,7 +33,7 @@ func New(scanner *scanner.Scanner) *Server {
}
func (a *Server) MountRouter(path string, subRouter http.Handler) {
log.Info("Mounting API", "path", path)
log.Info("Mounting routes", "path", path)
a.router.Group(func(r chi.Router) {
r.Use(middleware.Logger)
r.Mount(path, subRouter)
@@ -58,11 +57,12 @@ func (a *Server) initRoutes() {
r.Use(InjectLogger)
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/Jamstash", 302)
http.Redirect(w, r, "/app", 302)
})
workDir, _ := os.Getwd()
filesDir := filepath.Join(workDir, "Jamstash-master/dist")
FileServer(r, "/Jamstash", http.Dir(filesDir))
FileServer(r, "/Jamstash", "/Jamstash", http.Dir(filesDir))
a.router = r
}
@@ -81,24 +81,6 @@ func (a *Server) initScanner() {
}()
}
func FileServer(r chi.Router, path string, root http.FileSystem) {
if strings.ContainsAny(path, "{}*") {
panic("FileServer does not permit URL parameters.")
}
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, func(w http.ResponseWriter, r *http.Request) {
fs.ServeHTTP(w, r)
})
}
func InjectLogger(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()