2022-02-08 06:38:43 -06:00
|
|
|
package api
|
|
|
|
|
2022-04-15 07:01:58 -05:00
|
|
|
import (
|
|
|
|
"net/http"
|
2023-08-01 03:27:44 -05:00
|
|
|
"strings"
|
2022-04-15 07:01:58 -05:00
|
|
|
|
2023-11-15 08:42:35 -06:00
|
|
|
"github.com/grafana/grafana/pkg/api/routing"
|
2023-01-27 01:50:36 -06:00
|
|
|
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
|
2022-04-15 07:01:58 -05:00
|
|
|
)
|
2022-02-08 06:38:43 -06:00
|
|
|
|
2023-11-15 08:42:35 -06:00
|
|
|
func registerSwaggerUI(r routing.RouteRegister) {
|
|
|
|
// Deprecated
|
|
|
|
r.Get("/swagger-ui", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
http.Redirect(w, r, "swagger", http.StatusMovedPermanently)
|
|
|
|
})
|
|
|
|
// Deprecated
|
|
|
|
r.Get("/openapi3", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
http.Redirect(w, r, "swagger?show=v3", http.StatusMovedPermanently)
|
|
|
|
})
|
2023-08-01 03:27:44 -05:00
|
|
|
|
2023-11-15 08:42:35 -06:00
|
|
|
r.Get("/swagger", func(c *contextmodel.ReqContext) {
|
|
|
|
data := map[string]any{
|
|
|
|
"Nonce": c.RequestNonce,
|
|
|
|
}
|
2023-08-01 03:27:44 -05:00
|
|
|
|
2023-11-15 08:42:35 -06:00
|
|
|
// Add CSP for unpkg.com to allow loading of Swagger UI assets
|
|
|
|
if existingCSP := c.Resp.Header().Get("Content-Security-Policy"); existingCSP != "" {
|
|
|
|
newCSP := strings.Replace(existingCSP, "style-src", "style-src https://unpkg.com/", 1)
|
|
|
|
c.Resp.Header().Set("Content-Security-Policy", newCSP)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.HTML(http.StatusOK, "swagger", data)
|
|
|
|
})
|
2022-02-08 06:38:43 -06:00
|
|
|
}
|