为什么在Vercel生产环境中,Swagger与Golang不起作用?

huangapple go评论91阅读模式
英文:

Why Swagger with Golang is not working in Vercel production?

问题

我有一个使用Golang编写的项目,部署在Vercel环境中。一切都正常工作,但是Swagger不起作用。Swagger在开发环境中运行良好,但在生产环境中同样的路由不起作用。我使用了gin-swaggerswaggin-gonicvercel。这是Vercel的配置文件:

vercel.json:

{
"$schema": "https://openapi.vercel.sh/vercel.json",
"github": {
"silent": true
},
"trailingSlash": false,
"redirects": [
{
"source": "/",
"destination": "/api",
"permanent": false
},
{
"source": "/api",
"destination": "/api/ping",
"permanent": false
}
],
"rewrites": [
{
"source": "/api/(.*)",
"destination": "/api/index.go"
}
]
}

这是index.go文件:

package api

import (
"net/http"

_ "WerkenServer/docs"
"WerkenServer/handler"

"github.com/gin-gonic/gin"
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"

)

var (
app *gin.Engine
)

func registerRouter(r *gin.RouterGroup) {
r.GET("/ping", handler.Ping)
r.GET("/doc/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
ginSwagger.WrapHandler(swaggerFiles.Handler,
ginSwagger.URL("https://werken-server.vercel.app/api/doc/doc.json"),
ginSwagger.DefaultModelsExpandDepth(-1))
}

func init() {
app = gin.New()

app.NoRoute(handler.ErrRouter)
r := app.Group("/api")
registerRouter(r)

}

// Entrypoint
func Handler(w http.ResponseWriter, r *http.Request) {
app.ServeHTTP(w, r)
}

英文:

I have a Golang project, deployed in vercel environment. Everything is working fine but swagger. Swagger implementation is working fine in dev environment but in the production same route doesn't work. I have used gin-swagger, swag, gin-gonic, vercel. Here is the vercel config file:

vercel.json:

{
    "$schema": "https://openapi.vercel.sh/vercel.json",
    "github": {
    "silent": true
    },
    "trailingSlash": false,
    "redirects": [
    {
        "source": "/",
        "destination": "/api",
        "permanent": false
    },
    {
        "source": "/api",
        "destination": "/api/ping",
        "permanent": false
    }
    ],
    "rewrites": [
    {
        "source": "/api/(.*)",
        "destination": "/api/index.go"
    }
    ]
}

Here is the index.go file:

package api

import (
	"net/http"

	_ "WerkenServer/docs"
	"WerkenServer/handler"

	"github.com/gin-gonic/gin"
	swaggerFiles "github.com/swaggo/files"
	ginSwagger "github.com/swaggo/gin-swagger"
)

var (
	app *gin.Engine
)

func registerRouter(r *gin.RouterGroup) {
	r.GET("/ping", handler.Ping)
    r.GET("/doc/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
	ginSwagger.WrapHandler(swaggerFiles.Handler,
		ginSwagger.URL("https://werken-server.vercel.app/api/doc/doc.json"),
		ginSwagger.DefaultModelsExpandDepth(-1))
}

func init() {
	app = gin.New()

	app.NoRoute(handler.ErrRouter)
	r := app.Group("/api")
	registerRouter(r)
}

// Entrypoint
func Handler(w http.ResponseWriter, r *http.Request) {
	app.ServeHTTP(w, r)
}

答案1

得分: 0

我已经与Vercel团队进行了交流,他们在他们的系统中发现了一个问题。c.Request.RequestURI有时会返回空值。他们确保他们将解决这个问题,并为我提供了一个解决方法以解除阻塞。以下是解决方法的代码示例:

func init() {
    app = gin.New()
    app.GET("/doc/*any", FixRequestUri)
}

func FixRequestUri(c *gin.Context) {
    if c.Request.RequestURI == "" {
        c.Request.RequestURI = c.Request.URL.Path
    }

    ginSwagger.WrapHandler(swaggerFiles.Handler)(c)
}

你可以在这里获取完整的可工作示例代码:Golang vercel example template

英文:

I have talked with vercel team and they found an issue in their system. c.Request.RequestURI is returning sometimes empty. They ensured they will solve the issue, and provided me a workaround to unblock. Here is the workaround,

func init() {
	app = gin.New()
	app.GET("/doc/*any", FixRequestUri)
}

func FixRequestUri(c *gin.Context) {
	if c.Request.RequestURI == "" {
		c.Request.RequestURI = c.Request.URL.Path
	}

	ginSwagger.WrapHandler(swaggerFiles.Handler)(c)
}

You can get the full workable example code here: Golang vercel example template

huangapple
  • 本文由 发表于 2022年9月26日 19:58:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/73853819.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定