This commit is contained in:
@@ -2,6 +2,8 @@ package http
|
||||
|
||||
import (
|
||||
"context"
|
||||
"mime"
|
||||
"path/filepath"
|
||||
|
||||
"quyun/v2/app/commands"
|
||||
"quyun/v2/app/errorx"
|
||||
@@ -21,7 +23,9 @@ import (
|
||||
"go.ipao.vip/atom/container"
|
||||
"go.ipao.vip/atom/contracts"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
"github.com/gofiber/fiber/v3/middleware/favicon"
|
||||
"github.com/rogeecn/fabfile"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cobra"
|
||||
"go.uber.org/dig"
|
||||
@@ -86,6 +90,38 @@ func Serve(cmd *cobra.Command, args []string) error {
|
||||
route.Register(group)
|
||||
}
|
||||
|
||||
svc.Http.Engine.Get("/admin*", checkStaticFile(svc.App.DistAdmin))
|
||||
svc.Http.Engine.Get("/*", checkStaticFile(svc.App.DistWeChat))
|
||||
|
||||
return svc.Http.Serve(ctx)
|
||||
})
|
||||
}
|
||||
|
||||
func checkStaticFile(rootPath string) func(ctx fiber.Ctx) error {
|
||||
return func(ctx fiber.Ctx) error {
|
||||
f := ctx.Params("*")
|
||||
if f == "/" || f == "" {
|
||||
f = "index.html"
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
ext := filepath.Ext(filePath)
|
||||
mime := mime.TypeByExtension(ext)
|
||||
|
||||
log.Infof("mime type: %s %s", ext, mime)
|
||||
ctx.Set(fiber.HeaderContentType, mime)
|
||||
|
||||
return ctx.SendFile(file)
|
||||
}
|
||||
return ctx.SendStatus(fiber.StatusNotFound)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user