diff --git a/cmd/main.go b/cmd/main.go index 6933aa5d..6300eb64 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -15,15 +15,10 @@ import ( ) func main() { - var ( - router *mux.Router = mux.NewRouter() - app = App{} - ) - server.Build(router, app) - Run(router, app) + Run(mux.NewRouter(), App{}) } -func Run(routes *mux.Router, app App) { +func Run(router *mux.Router, app App) { Log.Info("Filestash %s starting", APP_VERSION) check(InitLogger(), "Logger init failed. err=%s") check(InitConfig(), "Config init failed. err=%s") @@ -33,17 +28,22 @@ func Run(routes *mux.Router, app App) { check(ErrNotFound, "Missing starter plugin. err=%s") } for _, obj := range Hooks.Get.HttpEndpoint() { - obj(routes, &app) + obj(router, &app) } for _, fn := range Hooks.Get.Onload() { fn() } - server.CatchAll(routes, app) + server.Build(router, app) + server.PluginRoutes(router) + server.CatchAll(router, app) + if os.Getenv("DEBUG") == "true" { + server.DebugRoutes(router) + } var wg sync.WaitGroup for _, obj := range Hooks.Get.Starter() { wg.Add(1) go func() { - obj(routes) + obj(router) wg.Done() }() } diff --git a/server/ctrl/static.go b/server/ctrl/static.go index c7b99361..c1064088 100644 --- a/server/ctrl/static.go +++ b/server/ctrl/static.go @@ -153,7 +153,6 @@ func ServeFile(chroot string) func(*App, http.ResponseWriter, *http.Request) { ), ) head := res.Header() - if f := applyPatch(filePath); f != nil { head.Set("Content-Type", GetMimeType(filepath.Ext(filePath))) head.Set("Cache-Control", "no-cache") @@ -447,7 +446,7 @@ func applyPatch(filePath string) (file *bytes.Buffer) { bytes.NewReader(outputBuffer.Bytes()), patchFiles[i], ); err != nil { - Log.Debug("ctrl::static cannot apply patch - %s", err.Error()) + Log.Debug("ctrl::static err=cannot_apply_patch path=%s err=%s", patchFiles[i].NewName, err.Error()) return nil } outputBuffer = patched diff --git a/server/routes.go b/server/routes.go index 1003d98d..df62a5be 100644 --- a/server/routes.go +++ b/server/routes.go @@ -4,7 +4,6 @@ import ( "fmt" "net/http" "net/http/pprof" - "os" "runtime" "runtime/debug" "strconv" @@ -113,11 +112,6 @@ func Build(r *mux.Router, a App) { r.HandleFunc(WithBase("/healthz"), NewMiddlewareChain(HealthHandler, []Middleware{}, a)).Methods("GET", "HEAD") r.HandleFunc(WithBase("/custom.css"), NewMiddlewareChain(CustomCssHandler, []Middleware{}, a)).Methods("GET") r.PathPrefix(WithBase("/doc")).Handler(NewMiddlewareChain(DocPage, []Middleware{}, a)).Methods("GET", "POST", "PUT", "DELETE", "OPTIONS") - - if os.Getenv("DEBUG") == "true" { - initDebugRoutes(r) - } - initPluginsRoutes(r, &a) } func CatchAll(r *mux.Router, a App) { @@ -127,7 +121,7 @@ func CatchAll(r *mux.Router, a App) { r.PathPrefix("/").Handler(http.HandlerFunc(NewMiddlewareChain(ServeFrontofficeHandler, middlewares, a))).Methods("GET", "POST") } -func initDebugRoutes(r *mux.Router) { +func DebugRoutes(r *mux.Router) { r.HandleFunc("/debug/pprof/", pprof.Index) r.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline) r.HandleFunc("/debug/pprof/profile", pprof.Profile) @@ -158,7 +152,7 @@ func initDebugRoutes(r *mux.Router) { }) } -func initPluginsRoutes(r *mux.Router, a *App) { +func PluginRoutes(r *mux.Router) { // frontoffice overrides: it is the mean by which plugin can interact with the frontoffice for _, obj := range Hooks.Get.FrontendOverrides() { r.HandleFunc(obj, func(res http.ResponseWriter, req *http.Request) {