chore: stabilize lint and verify builds

This commit is contained in:
2026-02-06 11:51:32 +08:00
parent edede17880
commit 1782f64417
114 changed files with 3032 additions and 1345 deletions

View File

@@ -11,7 +11,7 @@ import (
"quyun/v2/providers/jwt"
"github.com/gofiber/fiber/v3"
log "github.com/sirupsen/logrus"
logrus "github.com/sirupsen/logrus"
"go.ipao.vip/gen/types"
"gorm.io/gorm"
)
@@ -21,37 +21,40 @@ import (
// @provider
type Middlewares struct {
// log is the module logger injected by the framework.
log *log.Entry `inject:"false"`
log *logrus.Entry `inject:"false"`
// jwt is the JWT provider used by auth-related middlewares.
jwt *jwt.JWT
}
func (f *Middlewares) Prepare() error {
f.log = log.WithField("module", "middleware")
func (mw *Middlewares) Prepare() error {
mw.log = logrus.WithField("module", "middleware")
return nil
}
func (m *Middlewares) AuthOptional(ctx fiber.Ctx) error {
return m.authenticate(ctx, false)
func (mw *Middlewares) AuthOptional(ctx fiber.Ctx) error {
return mw.authenticate(ctx, false)
}
func (m *Middlewares) AuthRequired(ctx fiber.Ctx) error {
func (mw *Middlewares) AuthRequired(ctx fiber.Ctx) error {
if isPublicRoute(ctx) {
return m.AuthOptional(ctx)
return mw.AuthOptional(ctx)
}
return m.authenticate(ctx, true)
return mw.authenticate(ctx, true)
}
func (m *Middlewares) authenticate(ctx fiber.Ctx, requireToken bool) error {
func (mw *Middlewares) authenticate(ctx fiber.Ctx, requireToken bool) error {
authHeader := ctx.Get("Authorization")
if authHeader == "" {
if requireToken {
return errorx.ErrUnauthorized.WithMsg("Missing token")
}
return ctx.Next()
}
claims, err := m.jwt.Parse(authHeader)
claims, err := mw.jwt.Parse(authHeader)
if err != nil {
return errorx.ErrUnauthorized.WithCause(err).WithMsg("Invalid token")
}
@@ -83,7 +86,7 @@ func (m *Middlewares) authenticate(ctx fiber.Ctx, requireToken bool) error {
return ctx.Next()
}
func (m *Middlewares) SuperAuth(ctx fiber.Ctx) error {
func (mw *Middlewares) SuperAuth(ctx fiber.Ctx) error {
if isSuperPublicRoute(ctx) {
return ctx.Next()
}
@@ -92,7 +95,7 @@ func (m *Middlewares) SuperAuth(ctx fiber.Ctx) error {
return errorx.ErrUnauthorized.WithMsg("Missing token")
}
claims, err := m.jwt.Parse(authHeader)
claims, err := mw.jwt.Parse(authHeader)
if err != nil {
return errorx.ErrUnauthorized.WithCause(err).WithMsg("Invalid token")
}
@@ -110,10 +113,11 @@ func (m *Middlewares) SuperAuth(ctx fiber.Ctx) error {
}
ctx.Locals(consts.CtxKeyUser, user)
return ctx.Next()
}
func (m *Middlewares) TenantResolver(ctx fiber.Ctx) error {
func (mw *Middlewares) TenantResolver(ctx fiber.Ctx) error {
tenantCode := strings.TrimSpace(ctx.Params("tenantCode"))
if tenantCode == "" {
return errorx.ErrMissingParameter.WithMsg("缺少租户编码")
@@ -125,10 +129,12 @@ func (m *Middlewares) TenantResolver(ctx fiber.Ctx) error {
if errors.Is(err, gorm.ErrRecordNotFound) {
return errorx.ErrRecordNotFound.WithMsg("租户不存在")
}
return errorx.ErrDatabaseError.WithCause(err)
}
ctx.Locals(consts.CtxKeyTenant, tenant)
return ctx.Next()
}
@@ -138,6 +144,7 @@ func hasRole(roles types.Array[consts.Role], role consts.Role) bool {
return true
}
}
return false
}
@@ -164,7 +171,7 @@ func isPublicRoute(ctx fiber.Ctx) bool {
}
}
if method == fiber.MethodPost && (path == "/v1/webhook/payment/notify" || path == "/v1/auth/otp" || path == "/v1/auth/login") {
if method == fiber.MethodPost && (path == "/v1/auth/otp" || path == "/v1/auth/login") {
return true
}
@@ -205,6 +212,7 @@ func normalizeTenantPath(path string) string {
if strings.HasPrefix(rest, "/v1") {
return rest
}
return path
}
if strings.HasPrefix(path, "/v1/t/") {
@@ -213,7 +221,9 @@ func normalizeTenantPath(path string) string {
if slash == -1 {
return path
}
return "/v1" + rest[slash:]
}
return path
}

View File

@@ -46,6 +46,7 @@ func Test_Middlewares(t *testing.T) {
testx.Serve(providers, t, func(p MiddlewaresTestSuiteInjectParams) {
suite.Run(t, &MiddlewaresTestSuite{MiddlewaresTestSuiteInjectParams: p})
})
}
func (s *MiddlewaresTestSuite) newTestApp() *fiber.App {