refactor(api): move Swagger UI to docs route
This commit is contained in:
@@ -34,8 +34,8 @@ const swaggerUI = `<!doctype html>
|
||||
// RegisterDocumentation registers the OpenAPI document and its Swagger UI.
|
||||
func RegisterDocumentation(mux *http.ServeMux) {
|
||||
mux.HandleFunc("GET /openapi.json", serveOpenAPI)
|
||||
mux.HandleFunc("GET /swagger/", serveSwaggerUI)
|
||||
mux.HandleFunc("GET /docs", redirectToSwaggerUI)
|
||||
mux.HandleFunc("GET /docs/", serveSwaggerUI)
|
||||
mux.HandleFunc("GET /docs", redirectToDocumentation)
|
||||
}
|
||||
|
||||
func serveOpenAPI(w http.ResponseWriter, _ *http.Request) {
|
||||
@@ -48,6 +48,6 @@ func serveSwaggerUI(w http.ResponseWriter, _ *http.Request) {
|
||||
_, _ = w.Write([]byte(swaggerUI))
|
||||
}
|
||||
|
||||
func redirectToSwaggerUI(w http.ResponseWriter, r *http.Request) {
|
||||
http.Redirect(w, r, "/swagger/", http.StatusTemporaryRedirect)
|
||||
func redirectToDocumentation(w http.ResponseWriter, r *http.Request) {
|
||||
http.Redirect(w, r, "/docs/", http.StatusTemporaryRedirect)
|
||||
}
|
||||
|
||||
@@ -26,16 +26,21 @@ func TestRegisterDocumentation(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "Swagger UI",
|
||||
path: "/swagger/",
|
||||
path: "/docs/",
|
||||
wantStatus: http.StatusOK,
|
||||
wantContentType: "text/html; charset=utf-8",
|
||||
wantBody: `url: "/openapi.json"`,
|
||||
},
|
||||
{
|
||||
name: "documentation shortcut",
|
||||
name: "documentation trailing slash",
|
||||
path: "/docs",
|
||||
wantStatus: http.StatusTemporaryRedirect,
|
||||
wantLocation: "/swagger/",
|
||||
wantLocation: "/docs/",
|
||||
},
|
||||
{
|
||||
name: "removed Swagger route",
|
||||
path: "/swagger/",
|
||||
wantStatus: http.StatusNotFound,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user