在文档中https://github.com/swaggo/swag使用gin初始化服务器,但在我的应用程序中我使用http.ServeMux以及如何在不使用gin服务器的情况下初始化swaggo
在Docs中使用
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
我该怎么用...
mu.Handle("/swagger/*any", swaggerFiles.Handler)
......
跟随我最初的想法,但不工作... rsrs
func Server() *http.ServeMux {
docs.SwaggerInfo.Title = "Swagger Example API"
docs.SwaggerInfo.Description = "This is a sample server Petstore server."
docs.SwaggerInfo.Version = "1.0"
docs.SwaggerInfo.Host = "petstore.swagger.io"
mu := http.NewServeMux()
mu.Handle("/metrics", promhttp.Handler())
mu.Handle("/swagger/*any", swaggerFiles.Handler)
mu.HandleFunc("/helloWorld", handlers.NewHelloWorldHandler().HelloWorldHandler)
mu.HandleFunc("/production/", handlers.NewProductionHandler().ProductionHandler)
return mu
}
如果您有为分发而构建的swagger文件(即静态文件),并且在目录中:/some/dir/path/static/swagger
这应该适用于go
的http路由器:
staticFilesPath := "/some/dir/path/static"
staticRoute := "/static/"
h := http.NewServeMux()
// static file handler for route '/static/*'
h.Handle(
staticRoute,
http.StripPrefix(
staticRoute,
http.FileServer(http.Dir(staticFilesPath)),
),
)
我发现添加它也很有帮助:
// (redirect a route if not recognized - remove in production)
//
// unrecognized routes will redirect to Swagger API docs
h.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, staticRoute + "swagger/", http.StatusSeeOther)
})