From 099ec2150ec1517352cf4d30ce747d4b5afaa23b Mon Sep 17 00:00:00 2001 From: Amirhossein Khalili Date: Mon, 21 Sep 2026 19:12:51 +0330 Subject: [PATCH] refactor(api): move Swagger UI to docs route --- README.md | 2 +- backend/internal/httpapi/docs.go | 8 ++++---- backend/internal/httpapi/docs_test.go | 11 ++++++++--- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index bfc981e..dea797e 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ Open: - App: `http://127.0.0.1:3000` - 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` After backend changes, restart it or run `docker compose up --build` again so the running container uses the latest code. diff --git a/backend/internal/httpapi/docs.go b/backend/internal/httpapi/docs.go index b9014de..b5b7a67 100644 --- a/backend/internal/httpapi/docs.go +++ b/backend/internal/httpapi/docs.go @@ -34,8 +34,8 @@ const swaggerUI = ` // 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) } diff --git a/backend/internal/httpapi/docs_test.go b/backend/internal/httpapi/docs_test.go index fcfbdee..3964c82 100644 --- a/backend/internal/httpapi/docs_test.go +++ b/backend/internal/httpapi/docs_test.go @@ -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, }, }