package web
import (
"encoding/json"
"html/template"
"net/http"
)
type apiDocsData struct {
Version string
Nav Nav
SignedIn bool
SignIn bool
// Specs are the documents the page offers, as Swagger UI's urls: the
// client API's to everyone, the admin API's to staff.
Specs template.JS
}
// APIDocs renders the APIs' reference: their OpenAPI documents in Swagger
// UI, served from the gateway like every other script here.
func APIDocs(version string, signIn bool) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
d := apiDocsData{Version: version, SignIn: signIn}
if n, ok := r.Context().Value(navKey{}).(Nav); ok {
n.Current = "docs"
d.Nav, d.SignedIn = n, true
}
specs := []map[string]string{{"name": "client API (/v1)", "url": "/v1/openapi.json"}}
if d.Nav.Admin {
// The admin API is on its own listener, which a browser here
// does not reach: its document is offered to read, not to try.
specs = append(specs, map[string]string{"name": "admin API (admin listener)", "url": "/docs/api/admin.json"})
}
b, _ := json.Marshal(specs)
d.Specs = template.JS(b) //nolint:gosec // our own JSON
render(w, r, "apidocs", d)
}
}