英文:
Why Swagger with Golang is not working in Vercel production?
问题
我有一个使用Golang编写的项目,部署在Vercel环境中。一切都正常工作,但是Swagger不起作用。Swagger在开发环境中运行良好,但在生产环境中同样的路由不起作用。我使用了gin-swagger、swag、gin-gonic和vercel。这是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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论