对于以下代码:
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?路径前缀更改
查看中间件。Redoc方法和关联的模板SpecURL选项直接使用,并且不以BasePath为前缀。
为/swagger.yaml添加一个处理程序似乎有效。
type Items struct {}
type Item struct {
Name string `json:"name"`
}
func (Items) ServeHTTP(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
encoder := json.NewEncoder(w)
items := []Item{{"Hello"}, {"All"}}
_ = encoder.Encode(items)
}
func main() {
m := mux.NewRouter()
v1routes := m.Methods(http.MethodGet).PathPrefix("/v1").Subrouter()
opts := middleware.RedocOpts{SpecURL: "/swagger.yaml", BasePath: "/v1"}
sh := middleware.Redoc(opts, nil)
v1routes.Handle("/docs", sh)
// Assumes that the swagger file is in the current working directory and not ./v1/swagger.yaml
v1routes.Handle("/swagger.yaml", http.StripPrefix("/v1/", http.FileServer(http.Dir("./"))))
err := http.ListenAndServe(":8000", m)
fmt.Println(err)
}
http。FileServer需要匹配的路径。所以swagger文件要么需要在中/v1文件夹或/v1前缀需要删除。
您已接近目标。你只需要:
- 从路径中删除
/v1
前缀 - 将
BasePath
选项添加到middleware.RedocOpts
(请参阅github上的源代码( - 将
PathPrefix
方法添加到getRouter
定义中:
// handlers for API
getRouter := m.Methods(http.MethodGet).PathPrefix("/v1").Subrouter()
// 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("./")))
来自文件:
// BasePath for the UI path, defaults to: /
BasePath string
// Path combines with BasePath for the full UI path, defaults to: docs
Path string
完整示例:https://play.golang.org/p/U4A60KQ0lD8
我忘记更新之前共享的代码中的SpecUrl。以下对我有效。您需要在当前工作目录中有一个swagger.yaml文件。
func main() {
m := mux.NewRouter()
v1routes := m.Methods(http.MethodGet).PathPrefix("/v1").Subrouter()
opts := middleware.RedocOpts{SpecURL: "/v1/swagger.yaml", BasePath: "/v1"}
sh := middleware.Redoc(opts, nil)
v1routes.Handle("/docs", sh)
// Assumes that the swagger file is in the current working directory and not ./v1/swagger.yaml
v1routes.Handle("/swagger.yaml", http.StripPrefix("/v1/", http.FileServer(http.Dir("./"))))
err := http.ListenAndServe(":8000", m)
fmt.Println(err)
}