【发布时间】:2021-03-23 23:02:06
【问题描述】:
以下代码:
import (
"github.com/go-openapi/runtime/middleware"
"github.com/gorilla/mux"
)
m := mux.NewRouter()
// handlers for API
getRouter := m.Methods(http.MethodGet).Subrouter()
getRouter.HandleFunc("/v1/items", someHandler.ListAll)
// handler for documentation
opts := middleware.RedocOpts{SpecURL: "/swagger.yaml"}
sh := middleware.Redoc(opts, nil)
getRouter.Handle("/docs", sh)
getRouter.Handle("/swagger.yaml", http.FileServer(http.Dir("./")))
http://localhost:8080/docs & http://localhost:8080/swagger.yaml 呈现文档。 Api 处理程序也适用于 uri /v1/items
要为http://localhost:8080/v1/docs 和http://localhost:8080/v1/swagger.yaml 呈现文档,以下是所做的更改:
m := mux.NewRouter()
// handlers for API
getRouter := m.Methods(http.MethodGet).PathPrefix("/v1").Subrouter()
getRouter.HandleFunc("/items", someHandler.ListAll)
// handler for documentation
opts := middleware.RedocOpts{SpecURL: "/swagger.yaml",BasePath: "/v1"}
sh := middleware.Redoc(opts, nil)
getRouter.Handle("/docs", sh)
getRouter.Handle("/swagger.yaml", http.FileServer(http.Dir("./")))
但不起作用。 api 处理程序和文档处理程序都失败了
如何在http://localhost:8080/v1/docs 和http://localhost:8080/v1/swagger.yaml 上呈现文档?
如何在http://localhost:8080/v1/items 上渲染 api?随着路径前缀的变化
【问题讨论】:
标签: go swagger router gorilla go-swagger