feat: add static file serving and refactor route registration
This commit is contained in:
@@ -3,15 +3,12 @@ package http
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"sort"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"quyun/v2/app/commands"
|
"quyun/v2/app/commands"
|
||||||
"quyun/v2/app/errorx"
|
"quyun/v2/app/errorx"
|
||||||
"quyun/v2/app/http/super"
|
"quyun/v2/app/http/super"
|
||||||
"quyun/v2/app/jobs"
|
"quyun/v2/app/jobs"
|
||||||
"quyun/v2/app/services"
|
"quyun/v2/app/services"
|
||||||
"quyun/v2/app/tenancy"
|
|
||||||
"quyun/v2/database"
|
"quyun/v2/database"
|
||||||
_ "quyun/v2/docs"
|
_ "quyun/v2/docs"
|
||||||
"quyun/v2/providers/app"
|
"quyun/v2/providers/app"
|
||||||
@@ -85,17 +82,8 @@ func Serve(cmd *cobra.Command, args []string) error {
|
|||||||
}))
|
}))
|
||||||
|
|
||||||
rootGroup := svc.Http.Engine.Group("")
|
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 {
|
for _, route := range svc.Routes {
|
||||||
if strings.HasPrefix(route.Name(), "super") {
|
route.Register(rootGroup)
|
||||||
route.Register(rootGroup)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
route.Register(tenantGroup)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return svc.Http.Serve(ctx)
|
return svc.Http.Serve(ctx)
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ func (ctl *authController) login(ctx fiber.Ctx, form *dto.LoginForm) (*dto.Login
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !m.Roles.Contains(consts.RoleSuperAdmin) {
|
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{
|
token, err := ctl.jwt.CreateToken(ctl.jwt.CreateClaims(jwt.BaseClaims{
|
||||||
|
|||||||
@@ -26,11 +26,13 @@ func Provide(opts ...opt.Option) error {
|
|||||||
}
|
}
|
||||||
if err := container.Container.Provide(func(
|
if err := container.Container.Provide(func(
|
||||||
authController *authController,
|
authController *authController,
|
||||||
|
staticController *staticController,
|
||||||
tenant *tenant,
|
tenant *tenant,
|
||||||
) (contracts.HttpRoute, error) {
|
) (contracts.HttpRoute, error) {
|
||||||
obj := &Routes{
|
obj := &Routes{
|
||||||
authController: authController,
|
authController: authController,
|
||||||
tenant: tenant,
|
staticController: staticController,
|
||||||
|
tenant: tenant,
|
||||||
}
|
}
|
||||||
if err := obj.Prepare(); err != nil {
|
if err := obj.Prepare(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -40,6 +42,13 @@ func Provide(opts ...opt.Option) error {
|
|||||||
}, atom.GroupRoutes); err != nil {
|
}, atom.GroupRoutes); err != nil {
|
||||||
return err
|
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) {
|
if err := container.Container.Provide(func() (*tenant, error) {
|
||||||
obj := &tenant{}
|
obj := &tenant{}
|
||||||
|
|
||||||
|
|||||||
@@ -17,12 +17,13 @@ import (
|
|||||||
// Routes implements the HttpRoute contract and provides route registration
|
// Routes implements the HttpRoute contract and provides route registration
|
||||||
// for all controllers in the super module.
|
// for all controllers in the super module.
|
||||||
//
|
//
|
||||||
// @provider contracts.HttpRoute atom.GroupRoutes
|
// @provider contracts.HttpRoute atom.GroupRoutes
|
||||||
type Routes struct {
|
type Routes struct {
|
||||||
log *log.Entry `inject:"false"`
|
log *log.Entry `inject:"false"`
|
||||||
// Controller instances
|
// Controller instances
|
||||||
authController *authController
|
authController *authController
|
||||||
tenant *tenant
|
staticController *staticController
|
||||||
|
tenant *tenant
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prepare initializes the routes provider with logging configuration.
|
// Prepare initializes the routes provider with logging configuration.
|
||||||
@@ -59,5 +60,7 @@ func (r *Routes) Register(router fiber.Router) {
|
|||||||
Body[dto.TenantExpireUpdateForm]("form"),
|
Body[dto.TenantExpireUpdateForm]("form"),
|
||||||
))
|
))
|
||||||
|
|
||||||
|
router.Get("/super/*", Func(r.staticController.static))
|
||||||
|
|
||||||
r.log.Info("Successfully registered all routes")
|
r.log.Info("Successfully registered all routes")
|
||||||
}
|
}
|
||||||
|
|||||||
20
backend/app/http/super/static.go
Normal file
20
backend/app/http/super/static.go
Normal 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)
|
||||||
|
}
|
||||||
@@ -16,7 +16,6 @@ import (
|
|||||||
"github.com/gofiber/fiber/v3/middleware/compress"
|
"github.com/gofiber/fiber/v3/middleware/compress"
|
||||||
"github.com/gofiber/fiber/v3/middleware/cors"
|
"github.com/gofiber/fiber/v3/middleware/cors"
|
||||||
"github.com/gofiber/fiber/v3/middleware/helmet"
|
"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/logger"
|
||||||
"github.com/gofiber/fiber/v3/middleware/recover"
|
"github.com/gofiber/fiber/v3/middleware/recover"
|
||||||
"github.com/gofiber/fiber/v3/middleware/requestid"
|
"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)
|
// 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)
|
// static files (Fiber v3 Static helper moved; enable via filesystem middleware later)
|
||||||
// if config.StaticRoute != nil && config.StaticPath != nil { ... }
|
// if config.StaticRoute != nil && config.StaticPath != nil { ... }
|
||||||
|
|||||||
1
gen
Submodule
1
gen
Submodule
Submodule gen added at 755f8cbcf5
Reference in New Issue
Block a user