mirror of
https://github.com/m1k1o/neko-rooms.git
synced 2026-11-02 22:51:34 -06:00
join static, api and proxy in server.
This commit is contained in:
@@ -1,16 +1,14 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"path"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
type Admin struct {
|
||||
Static string
|
||||
Username string
|
||||
Password string
|
||||
PathPrefix string
|
||||
}
|
||||
|
||||
type Server struct {
|
||||
@@ -56,14 +54,13 @@ func (Server) Init(cmd *cobra.Command) error {
|
||||
return err
|
||||
}
|
||||
|
||||
cmd.PersistentFlags().String("admin.password", "", "admin password")
|
||||
if err := viper.BindPFlag("admin.password", cmd.PersistentFlags().Lookup("admin.password")); err != nil {
|
||||
cmd.PersistentFlags().String("admin.username", "admin", "require auth: admin username")
|
||||
if err := viper.BindPFlag("admin.username", cmd.PersistentFlags().Lookup("admin.username")); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// TODO: Default in v2 will be '/admin'.
|
||||
cmd.PersistentFlags().String("admin.path_prefix", "", "set custom path prefix for admin")
|
||||
if err := viper.BindPFlag("admin.path_prefix", cmd.PersistentFlags().Lookup("admin.path_prefix")); err != nil {
|
||||
cmd.PersistentFlags().String("admin.password", "", "require auth: admin password")
|
||||
if err := viper.BindPFlag("admin.password", cmd.PersistentFlags().Lookup("admin.password")); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -78,6 +75,6 @@ func (s *Server) Set() {
|
||||
s.PProf = viper.GetBool("pprof")
|
||||
|
||||
s.Admin.Static = viper.GetString("admin.static")
|
||||
s.Admin.Username = viper.GetString("admin.username")
|
||||
s.Admin.Password = viper.GetString("admin.password")
|
||||
s.Admin.PathPrefix = path.Join("/", path.Clean(viper.GetString("admin.path_prefix")))
|
||||
}
|
||||
|
||||
+48
-30
@@ -3,8 +3,9 @@ package server
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/go-chi/chi"
|
||||
@@ -59,46 +60,63 @@ func New(ApiManager types.ApiManager, roomConfig *config.Room, config *config.Se
|
||||
// admin page
|
||||
//
|
||||
|
||||
// in v1 default location was at / with traefik overriding
|
||||
// the actual room address. in order to keep this setting
|
||||
// we set new default path prefix only without traefik
|
||||
if !roomConfig.Traefik.Enabled && config.Admin.PathPrefix == "" {
|
||||
config.Admin.PathPrefix = "/admin"
|
||||
protected := func(next http.Handler) http.Handler {
|
||||
// if auth is disabled
|
||||
if config.Admin.Username == "" || config.Admin.Password == "" {
|
||||
return next
|
||||
}
|
||||
|
||||
router.Group(func(r chi.Router) {
|
||||
// handle authorization
|
||||
if config.Admin.Password != "" {
|
||||
r.Use(middleware.BasicAuth("neko-rooms admin", map[string]string{
|
||||
"admin": config.Admin.Password,
|
||||
}))
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
user, pass, ok := r.BasicAuth()
|
||||
if !ok || user != config.Admin.Username || pass != config.Admin.Password {
|
||||
w.Header().Add("WWW-Authenticate", `Basic realm="neko-rooms admin"`)
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
// bind API
|
||||
apiPath := path.Join("/", config.Admin.PathPrefix, "/api")
|
||||
router.Route(apiPath, ApiManager.Mount)
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
// cache static file paths
|
||||
staticFiles := map[string]struct{}{}
|
||||
if config.Admin.Static != "" {
|
||||
filepath.Walk(config.Admin.Static,
|
||||
func(p string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
staticFiles[path.Clean(p)] = struct{}{}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// serve static files
|
||||
if config.Admin.Static != "" {
|
||||
fs := http.FileServer(http.Dir(config.Admin.Static))
|
||||
fs = http.StripPrefix(config.Admin.PathPrefix, fs)
|
||||
|
||||
router.Handle(path.Join("/", config.Admin.PathPrefix, "*"), fs)
|
||||
router.Use(func(next http.Handler) http.Handler {
|
||||
// if static files are disabled
|
||||
if config.Admin.Static == "" {
|
||||
return next
|
||||
}
|
||||
})
|
||||
|
||||
if config.Admin.PathPrefix != "/" {
|
||||
// redirect / to admin path prefix
|
||||
router.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
http.Redirect(w, r, strings.TrimPrefix(config.Admin.PathPrefix, "/")+"/", http.StatusTemporaryRedirect)
|
||||
})
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
filePath := path.Join(config.Admin.Static, r.URL.Path)
|
||||
|
||||
// redirect force admin path prefix ending with /
|
||||
router.Get(config.Admin.PathPrefix, func(w http.ResponseWriter, r *http.Request) {
|
||||
http.Redirect(w, r, strings.TrimPrefix(config.Admin.PathPrefix, "/")+"/", http.StatusTemporaryRedirect)
|
||||
})
|
||||
// check if file exists to serve it
|
||||
if _, ok := staticFiles[filePath]; ok {
|
||||
// serve protected assets
|
||||
protected(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
http.ServeFile(w, r, filePath)
|
||||
})).ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
})
|
||||
|
||||
// serve protected API
|
||||
router.With(protected).Route("/api", ApiManager.Mount)
|
||||
|
||||
// handle all remaining paths with proxy
|
||||
router.Handle("/*", proxyHandler)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user