Swagger: Show k8s APIs (#78091)

This commit is contained in:
Ryan McKinley
2023-11-15 06:42:35 -08:00
committed by GitHub
parent e4d41e878f
commit b8e8d84ef7
8 changed files with 106 additions and 136 deletions

View File

@@ -79,4 +79,4 @@ make swagger-clean && make openapi3-gen
They can observe its output into the `public/api-merged.json` and `public/openapi3.json` files.
Finally, they can browser and try out both the OpenAPI v2 and v3 via the Swagger UI editor (served by the grafana server) by navigating to `/swagger-ui` and `/openapi3` respectively.
Finally, they can browser and try out both the OpenAPI v2 and v3 via the Swagger UI editor (served by the grafana server) by navigating to `/swagger`.

View File

@@ -213,8 +213,8 @@ func (hs *HTTPServer) registerRoutes() {
// expose plugin file system assets
r.Get("/public/plugins/:pluginId/*", hs.getPluginAssets)
r.Get("/swagger-ui", swaggerUI)
r.Get("/openapi3", openapi3)
// add swagger support
registerSwaggerUI(r)
if hs.Features.IsEnabledGlobally(featuremgmt.FlagClientTokenRotation) {
r.Post("/api/user/auth-tokens/rotate", routing.Wrap(hs.RotateUserAuthToken))

View File

@@ -1,22 +0,0 @@
package api
import (
"net/http"
"strings"
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
)
func openapi3(c *contextmodel.ReqContext) {
data := map[string]any{
"Nonce": c.RequestNonce,
}
// 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, "openapi3", data)
}

View File

@@ -4,19 +4,31 @@ import (
"net/http"
"strings"
"github.com/grafana/grafana/pkg/api/routing"
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
)
func swaggerUI(c *contextmodel.ReqContext) {
data := map[string]any{
"Nonce": c.RequestNonce,
}
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)
})
// 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)
}
r.Get("/swagger", func(c *contextmodel.ReqContext) {
data := map[string]any{
"Nonce": c.RequestNonce,
}
c.HTML(http.StatusOK, "swagger", data)
// 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)
})
}