refactor(api): move Swagger UI to docs route

This commit is contained in:
2026-09-21 19:12:51 +03:30
parent 9328556c28
commit 099ec2150e
3 changed files with 13 additions and 8 deletions

View File

@@ -29,7 +29,7 @@ Open:
- App: `http://127.0.0.1:3000` - App: `http://127.0.0.1:3000`
- Health check: `http://127.0.0.1:8080/health` - Health check: `http://127.0.0.1:8080/health`
- Swagger UI: `http://127.0.0.1:8080/docs` - Swagger UI: `http://127.0.0.1:8080/docs/`
- OpenAPI document: `http://127.0.0.1:8080/openapi.json` - OpenAPI document: `http://127.0.0.1:8080/openapi.json`
After backend changes, restart it or run `docker compose up --build` again so the running container uses the latest code. After backend changes, restart it or run `docker compose up --build` again so the running container uses the latest code.

View File

@@ -34,8 +34,8 @@ const swaggerUI = `<!doctype html>
// RegisterDocumentation registers the OpenAPI document and its Swagger UI. // RegisterDocumentation registers the OpenAPI document and its Swagger UI.
func RegisterDocumentation(mux *http.ServeMux) { func RegisterDocumentation(mux *http.ServeMux) {
mux.HandleFunc("GET /openapi.json", serveOpenAPI) mux.HandleFunc("GET /openapi.json", serveOpenAPI)
mux.HandleFunc("GET /swagger/", serveSwaggerUI) mux.HandleFunc("GET /docs/", serveSwaggerUI)
mux.HandleFunc("GET /docs", redirectToSwaggerUI) mux.HandleFunc("GET /docs", redirectToDocumentation)
} }
func serveOpenAPI(w http.ResponseWriter, _ *http.Request) { func serveOpenAPI(w http.ResponseWriter, _ *http.Request) {
@@ -48,6 +48,6 @@ func serveSwaggerUI(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte(swaggerUI)) _, _ = w.Write([]byte(swaggerUI))
} }
func redirectToSwaggerUI(w http.ResponseWriter, r *http.Request) { func redirectToDocumentation(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/swagger/", http.StatusTemporaryRedirect) http.Redirect(w, r, "/docs/", http.StatusTemporaryRedirect)
} }

View File

@@ -26,16 +26,21 @@ func TestRegisterDocumentation(t *testing.T) {
}, },
{ {
name: "Swagger UI", name: "Swagger UI",
path: "/swagger/", path: "/docs/",
wantStatus: http.StatusOK, wantStatus: http.StatusOK,
wantContentType: "text/html; charset=utf-8", wantContentType: "text/html; charset=utf-8",
wantBody: `url: "/openapi.json"`, wantBody: `url: "/openapi.json"`,
}, },
{ {
name: "documentation shortcut", name: "documentation trailing slash",
path: "/docs", path: "/docs",
wantStatus: http.StatusTemporaryRedirect, wantStatus: http.StatusTemporaryRedirect,
wantLocation: "/swagger/", wantLocation: "/docs/",
},
{
name: "removed Swagger route",
path: "/swagger/",
wantStatus: http.StatusNotFound,
}, },
} }