diff --git a/backend/app/service/http/http.go b/backend/app/service/http/http.go index f6a9332..44054c1 100644 --- a/backend/app/service/http/http.go +++ b/backend/app/service/http/http.go @@ -112,51 +112,8 @@ func Serve(cmd *cobra.Command, args []string) error { } // statics - svc.Http.Engine.Get("/admin*", func(ctx fiber.Ctx) error { - f := ctx.Params("*") - if f == "/" { - f = "index.html" - } - - ext := filepath.Ext(f) - mime := mime.TypeByExtension(ext) - log.Infof("admin mime type: %s %s", ext, mime) - ctx.Set(fiber.HeaderContentType, mime) - - log.Infof("check file: %s", filepath.Join(svc.App.DistAdmin, f)) - file, err := fabfile.Find(filepath.Join(svc.App.DistAdmin, f)) - if err != nil { - - log.Infof("check file: %s", filepath.Join(svc.App.DistAdmin, "index.html")) - file, err = fabfile.Find(filepath.Join(svc.App.DistAdmin, "index.html")) - if err != nil { - return ctx.SendStatus(fiber.StatusNotFound) - } - } - return ctx.SendFile(file) - }) - - svc.Http.Engine.Get("*", func(ctx fiber.Ctx) error { - f := ctx.Params("*") - if f == "/" || f == "" { - f = "index.html" - } - ext := filepath.Ext(f) - mime := mime.TypeByExtension(ext) - log.Infof("front mime type: %s %s", ext, mime) - ctx.Set(fiber.HeaderContentType, mime) - - log.Infof("check file: %s", filepath.Join(svc.App.DistWeChat, f)) - file, err := fabfile.Find(filepath.Join(svc.App.DistWeChat, f)) - if err != nil { - log.Infof("check file: %s", filepath.Join(svc.App.DistWeChat, "index.html")) - file, err = fabfile.Find(filepath.Join(svc.App.DistWeChat, "index.html")) - if err != nil { - return ctx.SendStatus(fiber.StatusNotFound) - } - } - return ctx.SendFile(file) - }) + svc.Http.Engine.Get("/admin*", checkStaticFile(svc.App.DistAdmin)) + svc.Http.Engine.Get("/*", checkStaticFile(svc.App.DistWeChat)) // job if err := svc.Job.Start(ctx); err != nil { @@ -168,3 +125,30 @@ func Serve(cmd *cobra.Command, args []string) error { return svc.Http.Serve() }) } + +func checkStaticFile(rootPath string) func(ctx fiber.Ctx) error { + return func(ctx fiber.Ctx) error { + f := ctx.Params("*") + if f == "/" || f == "" { + f = "index.html" + } + + ext := filepath.Ext(f) + mime := mime.TypeByExtension(ext) + log.Infof("admin mime type: %s %s", ext, mime) + ctx.Set(fiber.HeaderContentType, mime) + + checkFiles := []string{f, "index.html"} + for _, checkFile := range checkFiles { + filePath := filepath.Join(rootPath, checkFile) + log.Infof("check file: %s", filePath) + file, err := fabfile.Find(filePath) + if err != nil { + log.Warnf("file not found: %s", filePath) + continue + } + return ctx.SendFile(file) + } + return ctx.SendStatus(fiber.StatusNotFound) + } +} diff --git a/backend/go.mod b/backend/go.mod index 19df2af..c4f19ef 100644 --- a/backend/go.mod +++ b/backend/go.mod @@ -27,7 +27,7 @@ require ( github.com/riverqueue/river v0.15.0 github.com/riverqueue/river/riverdriver/riverpgxv5 v0.15.0 github.com/riverqueue/river/rivertype v0.15.0 - github.com/rogeecn/fabfile v1.5.0 + github.com/rogeecn/fabfile v1.6.0 github.com/rogeecn/swag v1.0.1 github.com/samber/lo v1.49.1 github.com/sirupsen/logrus v1.9.3 diff --git a/backend/go.sum b/backend/go.sum index f4eab1c..52dcf5c 100644 --- a/backend/go.sum +++ b/backend/go.sum @@ -258,6 +258,8 @@ github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs= github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro= github.com/rogeecn/fabfile v1.5.0 h1:I4yxvNRzsjEKMyq3opc32mmaBlfnZBjBUpjdMRR2zOM= github.com/rogeecn/fabfile v1.5.0/go.mod h1:EPwX7TtVcIWSLJkJAqxSzYjM/aV1Q0wymcaXqnMgzas= +github.com/rogeecn/fabfile v1.6.0 h1:i0+Koa8yp3lsgEM8opcnya85cEohpGeGpm21xEekBmQ= +github.com/rogeecn/fabfile v1.6.0/go.mod h1:EPwX7TtVcIWSLJkJAqxSzYjM/aV1Q0wymcaXqnMgzas= github.com/rogeecn/swag v1.0.1 h1:s1yxLgopqO1m8sqGjVmt6ocMBRubMPIh2JtIPG4xjQE= github.com/rogeecn/swag v1.0.1/go.mod h1:flG2NXERPxlRl2VdpU2VXTO8iBnQiERyowOXSkZVMOc= github.com/rogpeppe/clock v0.0.0-20190514195947-2896927a307a h1:3QH7VyOaaiUHNrA9Se4YQIRkDTCw1EJls9xTUCaCeRM=