如何制作單個檔案來處理 vercel 無服務器功能的所有路由?
默認情況下它使用內置處理程式有沒有辦法使用 gin 模塊做同樣的事情?
package handler
import "github.com/gin-gonic/gin"
/* get the post data and send the same data as response */
func Hi(c *gin.Context) {
c.JSON(200, gin.H{
"message": "Hello World!",
})
}
uj5u.com熱心網友回復:
如果我正確理解了您的問題,您只需要創建 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()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/473042.html
下一篇:HCL解碼:具有多個標簽的塊
