feat: update static fetch issues

This commit is contained in:
Rogee
2025-05-06 15:07:20 +08:00
parent 01d6c40fa2
commit 4b08a8199b
3 changed files with 32 additions and 46 deletions

View File

@@ -112,51 +112,8 @@ func Serve(cmd *cobra.Command, args []string) error {
} }
// statics // statics
svc.Http.Engine.Get("/admin*", func(ctx fiber.Ctx) error { svc.Http.Engine.Get("/admin*", checkStaticFile(svc.App.DistAdmin))
f := ctx.Params("*") svc.Http.Engine.Get("/*", checkStaticFile(svc.App.DistWeChat))
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)
})
// job // job
if err := svc.Job.Start(ctx); err != nil { if err := svc.Job.Start(ctx); err != nil {
@@ -168,3 +125,30 @@ func Serve(cmd *cobra.Command, args []string) error {
return svc.Http.Serve() 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)
}
}

View File

@@ -27,7 +27,7 @@ require (
github.com/riverqueue/river v0.15.0 github.com/riverqueue/river v0.15.0
github.com/riverqueue/river/riverdriver/riverpgxv5 v0.15.0 github.com/riverqueue/river/riverdriver/riverpgxv5 v0.15.0
github.com/riverqueue/river/rivertype 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/rogeecn/swag v1.0.1
github.com/samber/lo v1.49.1 github.com/samber/lo v1.49.1
github.com/sirupsen/logrus v1.9.3 github.com/sirupsen/logrus v1.9.3

View File

@@ -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/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 h1:I4yxvNRzsjEKMyq3opc32mmaBlfnZBjBUpjdMRR2zOM=
github.com/rogeecn/fabfile v1.5.0/go.mod h1:EPwX7TtVcIWSLJkJAqxSzYjM/aV1Q0wymcaXqnMgzas= 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 h1:s1yxLgopqO1m8sqGjVmt6ocMBRubMPIh2JtIPG4xjQE=
github.com/rogeecn/swag v1.0.1/go.mod h1:flG2NXERPxlRl2VdpU2VXTO8iBnQiERyowOXSkZVMOc= github.com/rogeecn/swag v1.0.1/go.mod h1:flG2NXERPxlRl2VdpU2VXTO8iBnQiERyowOXSkZVMOc=
github.com/rogpeppe/clock v0.0.0-20190514195947-2896927a307a h1:3QH7VyOaaiUHNrA9Se4YQIRkDTCw1EJls9xTUCaCeRM= github.com/rogpeppe/clock v0.0.0-20190514195947-2896927a307a h1:3QH7VyOaaiUHNrA9Se4YQIRkDTCw1EJls9xTUCaCeRM=