mirror of
https://github.com/m1k1o/neko-rooms.git
synced 2026-11-03 07:01:35 -06:00
internal proxy & admin path prefix.
This commit is contained in:
@@ -12,6 +12,9 @@ type Server struct {
|
||||
Static string
|
||||
Proxy bool
|
||||
PProf bool
|
||||
|
||||
InternalProxy bool
|
||||
AdminPathPrefix string
|
||||
}
|
||||
|
||||
func (Server) Init(cmd *cobra.Command) error {
|
||||
@@ -45,6 +48,17 @@ func (Server) Init(cmd *cobra.Command) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// WIP.
|
||||
cmd.PersistentFlags().Bool("internal_proxy", false, "use internal proxy instead of traefik")
|
||||
if err := viper.BindPFlag("internal_proxy", cmd.PersistentFlags().Lookup("internal_proxy")); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
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 {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -55,4 +69,7 @@ func (s *Server) Set() {
|
||||
s.Static = viper.GetString("static")
|
||||
s.Proxy = viper.GetBool("proxy")
|
||||
s.PProf = viper.GetBool("pprof")
|
||||
|
||||
s.InternalProxy = viper.GetBool("internal_proxy")
|
||||
s.AdminPathPrefix = viper.GetString("admin_path_prefix")
|
||||
}
|
||||
|
||||
+28
-21
@@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"time"
|
||||
|
||||
"github.com/go-chi/chi"
|
||||
@@ -24,7 +23,7 @@ type ServerManagerCtx struct {
|
||||
config *config.Server
|
||||
}
|
||||
|
||||
func New(ApiManager types.ApiManager, pathPrefix string, config *config.Server, handler http.Handler) *ServerManagerCtx {
|
||||
func New(ApiManager types.ApiManager, pathPrefix string, config *config.Server, proxyHandler http.Handler) *ServerManagerCtx {
|
||||
logger := log.With().Str("module", "server").Logger()
|
||||
|
||||
router := chi.NewRouter()
|
||||
@@ -49,31 +48,39 @@ func New(ApiManager types.ApiManager, pathPrefix string, config *config.Server,
|
||||
MaxAge: 300, // Maximum value not ignored by any of major browsers
|
||||
}))
|
||||
|
||||
router.Route("/api", ApiManager.Mount)
|
||||
|
||||
// serve static files
|
||||
if config.Static != "" {
|
||||
fs := http.FileServer(http.Dir(config.Static))
|
||||
router.Get("/*", func(w http.ResponseWriter, r *http.Request) {
|
||||
if _, err := os.Stat(config.Static + r.URL.Path); !os.IsNotExist(err) {
|
||||
fs.ServeHTTP(w, r)
|
||||
} else {
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// add simple lobby room
|
||||
router.Get(path.Join("/", pathPrefix, "{roomName}"), ApiManager.RoomLobby)
|
||||
router.Get(path.Join("/", pathPrefix, "{roomName}")+"/", ApiManager.RoomLobby)
|
||||
|
||||
// mount pprof endpoint
|
||||
if config.PProf {
|
||||
withPProf(router)
|
||||
logger.Info().Msgf("with pprof endpoint at %s", pprofPath)
|
||||
}
|
||||
|
||||
router.Handle("/*", handler)
|
||||
// admin page
|
||||
router.Route(config.AdminPathPrefix, func(r chi.Router) {
|
||||
r.Route("/api", ApiManager.Mount)
|
||||
|
||||
// serve static files
|
||||
if config.Static != "" {
|
||||
fs := http.FileServer(http.Dir(config.Static))
|
||||
r.Get("/*", func(w http.ResponseWriter, r *http.Request) {
|
||||
if _, err := os.Stat(config.Static + r.URL.Path); !os.IsNotExist(err) {
|
||||
fs.ServeHTTP(w, r)
|
||||
} else {
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
// rooms page
|
||||
router.Route(pathPrefix, func(r chi.Router) {
|
||||
if config.InternalProxy {
|
||||
r.Handle("/*", proxyHandler)
|
||||
} else {
|
||||
// add simple lobby room
|
||||
r.Get("/{roomName}", ApiManager.RoomLobby)
|
||||
r.Get("/{roomName}/", ApiManager.RoomLobby)
|
||||
}
|
||||
})
|
||||
|
||||
// we could use custom 404
|
||||
router.NotFound(http.NotFound)
|
||||
|
||||
Reference in New Issue
Block a user