package http import ( "context" "mime" "path/filepath" "quyun/v2/app/commands" "quyun/v2/app/errorx" web "quyun/v2/app/http" "quyun/v2/app/jobs" "quyun/v2/app/middlewares" "quyun/v2/app/services" _ "quyun/v2/docs" "quyun/v2/providers/ali" "quyun/v2/providers/app" "quyun/v2/providers/http" "quyun/v2/providers/http/swagger" "quyun/v2/providers/job" "quyun/v2/providers/jwt" "go.ipao.vip/atom" "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" ) func defaultProviders() container.Providers { return commands.Default(container.Providers{ http.DefaultProvider(), jwt.DefaultProvider(), job.DefaultProvider(), ali.DefaultProvider(), }...) } func Command() atom.Option { return atom.Command( atom.Name("serve"), atom.Short("run http server"), atom.RunE(Serve), atom.Providers( defaultProviders(). WithProviders( web.Providers(), ). With( jobs.Provide, services.Provide, middlewares.Provide, ), ), ) } type Service struct { dig.In App *app.Config Job *job.Job Middleware *middlewares.Middlewares Http *http.Service Initials []contracts.Initial `group:"initials"` Routes []contracts.HttpRoute `group:"routes"` } func Serve(cmd *cobra.Command, args []string) error { return container.Container.Invoke(func(ctx context.Context, svc Service) error { log.SetFormatter(&log.JSONFormatter{}) if svc.App.Mode == app.AppModeDevelopment { log.SetLevel(log.DebugLevel) svc.Http.Engine.Get("/swagger/*", swagger.HandlerDefault) } svc.Http.Engine.Use(errorx.Middleware) svc.Http.Engine.Use(favicon.New(favicon.Config{ Data: []byte{}, })) svc.Http.Engine.Use(svc.Middleware.DebugMode) for _, route := range svc.Routes { group := svc.Http.Engine.Group(route.Path(), route.Middlewares()...).Name(route.Name()) 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) } }