1 回答

TA贡献1773条经验 获得超3个赞
如果我正确理解了你的问题,你只需要创建 struct Handler 并创建一个方法“InitRoutes”返回带有所有 handleFuncs 的路由器
handleFuncs 也应该是 Handler 的方法
例如:
type Handler struct {
// here you can inject services
}
func NewHandler(services *service.Service) *Handler {
return &Handler{}
}
func (h *Handler) InitRoutes() *gin.Engine {
router := gin.New()
auth := router.Group("/group")
{
auth.POST("/path", h.handleFunc)
auth.POST("/path", h.handleFunc)
}
return router
}
之后你应该将它注入你的 httpServer
srv := http.Server{
Addr: ":" + port,
Handler: Handler.InitRoutes(),
MaxHeaderBytes: 1 << 20,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
}
srv.ListenAndServe()
- 1 回答
- 0 关注
- 209 浏览
添加回答
举报