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 關注
- 164 瀏覽
添加回答
舉報