package api import ( "fmt" "html/template" "net/http" "github.com/glueops/autoglue/docs" "github.com/go-chi/chi/v5" ) func mountSwaggerRoutes(r chi.Router) { r.Get("/swagger", RapidDocHandler("/swagger/swagger.yaml")) r.Get("/swagger/index.html", RapidDocHandler("/swagger/swagger.yaml")) r.Get("/swagger/swagger.json", serveSwaggerFromEmbed(docs.SwaggerJSON, "application/json")) r.Get("/swagger/swagger.yaml", serveSwaggerFromEmbed(docs.SwaggerYAML, "application/x-yaml")) } var rapidDocTmpl = template.Must(template.New("redoc").Parse(` AutoGlue API Docs `)) func RapidDocHandler(specURL string) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { scheme := "http" if r.TLS != nil || r.Header.Get("X-Forwarded-Proto") == "https" { scheme = "https" } host := r.Host defaultServer := fmt.Sprintf("%s://%s/api/v1", scheme, host) w.Header().Set("Content-Type", "text/html; charset=utf-8") if err := rapidDocTmpl.Execute(w, map[string]string{ "SpecURL": specURL, "DefaultServer": defaultServer, }); err != nil { http.Error(w, "failed to render docs", http.StatusInternalServerError) return } } }