ugent-library / old-people-service

People service
Apache License 2.0
0 stars 0 forks source link

[OpenAPI] Add Swagger documentation for the RESTful API #25

Closed netsensei closed 1 year ago

netsensei commented 1 year ago

Story

I should be able to see a Swagger documentation when I navigate to /swagger/. The Swagger documentation allows me to interact with the REST API.

Success criteria

Implementation suggestion

First, copy the directory public/swagger-ui-5.1.0 from the OAI service to the public/ directory of the people service.

Next, check the public/swagger-ui-5.1.0/swagger/swagger-initializer.js file. The url property should point to the api/v1/openapi.yaml file. Ignore any and all comments in that file: this is part from a ZIP file downloaded from the Swagger website.

window.ui = SwaggerUIBundle({
    url: "../api/v1/openapi.yaml",
    ...
}

Next, hook up the swagger folder and it's code in the mux router in cmd/api.go like this:

// mount api
mux.Mount("/api/v1", http.StripPrefix("/api/v1", apiServer))
mux.Get("/api/v1/openapi.yaml", func(w http.ResponseWriter, r *http.Request) {
    http.ServeFile(w, r, "api/v1/openapi.yaml")
})
mux.Mount("/swagger/", http.StripPrefix("/swagger/", http.FileServer(http.Dir("public/swagger-ui-5.1.0"))))

When you navigate to /swagger/ path, you should see the documentation. Don't forget the trailing / at the end!

Automatic testing scenario

n/a

Related issues