亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 Vercel 無服務功能中使用 Go Gin?

如何在 Vercel 無服務功能中使用 Go Gin?

Go
慕娘9325324 2022-12-13 11:13:45
如何制作一個文件來處理 vercel 無服務器功能的所有路由?默認情況下它使用內置處理程序有沒有辦法使用 gin 模塊來做同樣的事情?package handlerimport "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!",    })}
查看完整描述

1 回答

?
慕容3067478

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()


查看完整回答
反對 回復 2022-12-13
  • 1 回答
  • 0 關注
  • 164 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號