1 parent
53bb8b7b4d
commit
98c28520ef
5 files changed
+129
-7
No files matched your search
+8
-7
@@ -45,10 +45,7 @@ type TrustedField func(ctx *gin.Context) (any, error)
|
||||
// 入参:method 为大写 HTTP 方法,routePath 为完整路径,handlers 为中间件和处理器。
|
||||
// 返回:路由冲突、配置无效或服务已冻结时返回错误,不部分注册。
|
||||
func (s *Server) Handle(method string, routePath string, handlers ...gin.HandlerFunc) error {
|
||||
if len(handlers) == 0 {
|
||||
return failure.Wrap(codeRouteHandler, "validate native route handler failed", errors.New("route handler is required"))
|
||||
}
|
||||
return s.commit([]route{{meta: Metadata{Method: method, Path: routePath}, handlers: append([]gin.HandlerFunc(nil), handlers...)}})
|
||||
return s.HandleBatch(NativeRoute{Metadata: Metadata{Method: method, Path: routePath}, Handlers: handlers})
|
||||
}
|
||||
|
||||
// Group 解析 Controller 的 Form/Meta 方法并作为一个批次注册。
|
||||
@@ -277,9 +274,13 @@ func (s *Server) buildEngine(routes []route) (engine *gin.Engine, err error) {
|
||||
}
|
||||
engine.Use(s.initialize)
|
||||
engine.Use(s.options.Middleware...)
|
||||
engine.NoRoute(func(ctx *gin.Context) {
|
||||
s.writeResult(ctx, nil, &PublicError{Code: 404, Message: "Not Found", Status: 404}, 0)
|
||||
})
|
||||
if s.options.NoRoute != nil {
|
||||
engine.NoRoute(s.options.NoRoute)
|
||||
} else {
|
||||
engine.NoRoute(func(ctx *gin.Context) {
|
||||
s.writeResult(ctx, nil, &PublicError{Code: 404, Message: "Not Found", Status: 404}, 0)
|
||||
})
|
||||
}
|
||||
for _, item := range routes {
|
||||
if item.meta.Path == "" || !strings.HasPrefix(item.meta.Path, "/") || item.meta.Method == "" {
|
||||
return nil, failure.Wrap(codeRoutePath, "validate HTTP route path failed", errors.New("method and absolute route path are required"))
|
||||
|
||||