-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswagger.go
More file actions
85 lines (73 loc) · 2.26 KB
/
swagger.go
File metadata and controls
85 lines (73 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package openapi_validator
import (
"embed"
"encoding/json"
"html/template"
"net/http"
"strings"
)
//go:embed swagger-ui/*
var swaggerUIFS embed.FS
var (
swaggerUITemplate = template.Must(template.ParseFS(swaggerUIFS, "swagger-ui/index.html"))
)
// Registrar is an interface that matches both http.ServeMux and gorilla/mux.Router.
type Registrar interface {
HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request))
}
// SwaggerUIHandler returns an http.Handler that serves the Swagger UI and the OpenAPI spec.
func (v *Validator) SwaggerUIHandler() http.Handler {
path := v.Options.SwaggerUIPath
if !strings.HasSuffix(path, "/") {
path += "/"
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Serve the current file from the swagger-ui directory
relPath := strings.TrimPrefix(r.URL.Path, path)
// Serve the spec file if requested
if relPath == "openapi.json" {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(v.Swagger)
return
}
// Serve static assets from the embedded filesystem
if relPath == "styles.css" || relPath == "main.js" {
content, err := swaggerUIFS.ReadFile("swagger-ui/" + relPath)
if err != nil {
http.Error(w, "Asset not found", http.StatusNotFound)
return
}
if strings.HasSuffix(relPath, ".css") {
w.Header().Set("Content-Type", "text/css")
} else {
w.Header().Set("Content-Type", "application/javascript")
}
w.Write(content)
return
}
// Serve the index HTML for the base path or index.html explicitly
if relPath == "" || relPath == "index.html" {
w.Header().Set("Content-Type", "text/html")
data := struct {
SpecURL string
}{
SpecURL: v.Options.SwaggerUIPath + "/openapi.json",
}
if err := swaggerUITemplate.Execute(w, data); err != nil {
http.Error(w, "Failed to render Swagger UI", http.StatusInternalServerError)
}
return
}
// Otherwise, return 404
http.NotFound(w, r)
})
}
// HandleSwaggerUI registers the necessary routes to serve the Swagger UI and the OpenAPI spec.
func (v *Validator) HandleSwaggerUI(mux Registrar) {
path := v.Options.SwaggerUIPath
if !strings.HasSuffix(path, "/") {
path += "/"
}
handler := v.SwaggerUIHandler()
mux.HandleFunc(path, handler.ServeHTTP)
}