feat: add static file serving and refactor route registration

This commit is contained in:
2025-12-16 22:40:31 +08:00
parent 55c40c4601
commit 2a12e1c3a9
7 changed files with 41 additions and 21 deletions

View File

@@ -3,15 +3,12 @@ package http
import (
"context"
"database/sql"
"sort"
"strings"
"quyun/v2/app/commands"
"quyun/v2/app/errorx"
"quyun/v2/app/http/super"
"quyun/v2/app/jobs"
"quyun/v2/app/services"
"quyun/v2/app/tenancy"
"quyun/v2/database"
_ "quyun/v2/docs"
"quyun/v2/providers/app"
@@ -85,17 +82,8 @@ func Serve(cmd *cobra.Command, args []string) error {
}))
rootGroup := svc.Http.Engine.Group("")
tenantGroup := svc.Http.Engine.Group("/t/:tenant_code", tenancy.Middleware(svc.DB))
sort.SliceStable(svc.Routes, func(i, j int) bool {
return svc.Routes[i].Name() < svc.Routes[j].Name()
})
for _, route := range svc.Routes {
if strings.HasPrefix(route.Name(), "super") {
route.Register(rootGroup)
continue
}
route.Register(tenantGroup)
route.Register(rootGroup)
}
return svc.Http.Serve(ctx)

View File

@@ -38,7 +38,7 @@ func (ctl *authController) login(ctx fiber.Ctx, form *dto.LoginForm) (*dto.Login
}
if !m.Roles.Contains(consts.RoleSuperAdmin) {
return nil, errorx.Wrap(err).WithMsg("用户名或密码错误")
return nil, errorx.Wrap(errorx.ErrInvalidCredentials).WithMsg("用户名或密码错误")
}
token, err := ctl.jwt.CreateToken(ctl.jwt.CreateClaims(jwt.BaseClaims{

View File

@@ -26,11 +26,13 @@ func Provide(opts ...opt.Option) error {
}
if err := container.Container.Provide(func(
authController *authController,
staticController *staticController,
tenant *tenant,
) (contracts.HttpRoute, error) {
obj := &Routes{
authController: authController,
tenant: tenant,
authController: authController,
staticController: staticController,
tenant: tenant,
}
if err := obj.Prepare(); err != nil {
return nil, err
@@ -40,6 +42,13 @@ func Provide(opts ...opt.Option) error {
}, atom.GroupRoutes); err != nil {
return err
}
if err := container.Container.Provide(func() (*staticController, error) {
obj := &staticController{}
return obj, nil
}); err != nil {
return err
}
if err := container.Container.Provide(func() (*tenant, error) {
obj := &tenant{}

View File

@@ -17,12 +17,13 @@ import (
// Routes implements the HttpRoute contract and provides route registration
// for all controllers in the super module.
//
// @provider contracts.HttpRoute atom.GroupRoutes
// @provider contracts.HttpRoute atom.GroupRoutes
type Routes struct {
log *log.Entry `inject:"false"`
// Controller instances
authController *authController
tenant *tenant
authController *authController
staticController *staticController
tenant *tenant
}
// Prepare initializes the routes provider with logging configuration.
@@ -59,5 +60,7 @@ func (r *Routes) Register(router fiber.Router) {
Body[dto.TenantExpireUpdateForm]("form"),
))
router.Get("/super/*", Func(r.staticController.static))
r.log.Info("Successfully registered all routes")
}

View File

@@ -0,0 +1,20 @@
package super
import (
"github.com/gofiber/fiber/v3"
)
// @provider
type staticController struct{}
// Static
//
// @Tags Super
// @Router /super/*
func (ctl *staticController) static(ctx fiber.Ctx) error {
param := ctx.Params("*")
if param == "" {
param = "index.html"
}
return ctx.SendFile("/home/rogee/Projects/quyun_v2/frontend/superadmin/dist/" + param)
}

View File

@@ -16,7 +16,6 @@ import (
"github.com/gofiber/fiber/v3/middleware/compress"
"github.com/gofiber/fiber/v3/middleware/cors"
"github.com/gofiber/fiber/v3/middleware/helmet"
"github.com/gofiber/fiber/v3/middleware/limiter"
"github.com/gofiber/fiber/v3/middleware/logger"
"github.com/gofiber/fiber/v3/middleware/recover"
"github.com/gofiber/fiber/v3/middleware/requestid"
@@ -139,7 +138,7 @@ func Provide(opts ...opt.Option) error {
}))
// rate limit (enable standard headers; adjust Max via config if needed)
engine.Use(limiter.New(limiter.Config{Max: 0}))
// engine.Use(limiter.New(limiter.Config{Max: 0}))
// static files (Fiber v3 Static helper moved; enable via filesystem middleware later)
// if config.StaticRoute != nil && config.StaticPath != nil { ... }

1
gen Submodule

Submodule gen added at 755f8cbcf5