添加运行模式配置,优化调试和生产环境的日志输出

This commit is contained in:
2025-05-07 19:03:44 +08:00
parent 0a16f9923c
commit 0e0affb008
5 changed files with 61 additions and 11 deletions

View File

@@ -66,6 +66,11 @@ func Protect(c *fiber.Ctx, types []services.PayloadType, permissions []string) (
return nil, fiber.NewError(fiber.StatusForbidden, "没有权限")
}
// 保存到上下文
c.Locals("auth", auth)
c.Locals("authid", auth.Payload.Id)
c.Locals("authtype", auth.Payload.Type.Name())
return auth, nil
}

View File

@@ -237,6 +237,20 @@ const (
PayloadClientConfidential
)
func (t PayloadType) Name() string {
switch t {
case PayloadUser:
return "user"
case PayloadAdmin:
return "admn"
case PayloadClientPublic:
return "cpub"
case PayloadClientConfidential:
return "ccnf"
}
return "unknown"
}
type Agent struct {
Id int32 `json:"id,omitempty"`
Addr string `json:"addr,omitempty"`

View File

@@ -51,15 +51,17 @@ func (s *Server) Run() error {
ErrorHandler: ErrorHandler,
})
s.fiber.Use(logger.New(logger.Config{
DisableColors: false,
}))
s.fiber.Use(requestid.New(requestid.Config{
Generator: func() string {
binary, _ := uuid.New().MarshalBinary()
return base62.EncodeToString(binary)
},
}))
s.fiber.Use(logger.New(logger.Config{
Format: "🚀 ${time} | ${locals:authtype} ${locals:authid} | ${method} ${path} | ${status} | ${latency} | ${error}\n",
TimeFormat: "2006-01-02 15:04:05",
TimeZone: "Asia/Shanghai",
}))
ApplyRouters(s.fiber)