forked from jdpedrie/go-redoc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredoc.go
More file actions
108 lines (92 loc) · 2.02 KB
/
redoc.go
File metadata and controls
108 lines (92 loc) · 2.02 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package redoc
import (
"bytes"
"errors"
"io/fs"
"net/http"
"os"
"strings"
"text/template"
_ "embed"
)
// ErrSpecNotFound error for when spec file not found
var ErrSpecNotFound = errors.New("spec not found")
// Redoc configuration
type Redoc struct {
DocsPath string
SpecPath string
SpecFile string
SpecFS fs.ReadFileFS
Title string
Description string
}
// HTML represents the redoc index.html page
//
//go:embed assets/index.html
var HTML string
// JavaScript represents the redoc standalone javascript
//
//go:embed assets/redoc.standalone.js
var JavaScript string
// Body returns the final html with the js in the body
func (r Redoc) Body() ([]byte, error) {
buf := bytes.NewBuffer(nil)
tpl, err := template.New("redoc").Parse(HTML)
if err != nil {
return nil, err
}
if err = tpl.Execute(buf, map[string]string{
"body": JavaScript,
"title": r.Title,
"url": r.SpecPath,
"description": r.Description,
}); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// Handler sets some defaults and returns a HandlerFunc
func (r Redoc) Handler() http.HandlerFunc {
data, err := r.Body()
if err != nil {
panic(err)
}
specFile := r.SpecFile
if specFile == "" {
panic(ErrSpecNotFound)
}
if r.SpecPath == "" {
r.SpecPath = "/openapi.json"
}
var spec []byte
if r.SpecFS == nil {
spec, err = os.ReadFile(specFile)
if err != nil {
panic(err)
}
} else {
spec, err = r.SpecFS.ReadFile(specFile)
if err != nil {
panic(err)
}
}
docsPath := r.DocsPath
return func(w http.ResponseWriter, req *http.Request) {
method := strings.ToLower(req.Method)
if method != "get" && method != "head" {
return
}
header := w.Header()
if strings.HasSuffix(req.URL.Path, r.SpecPath) {
header.Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = w.Write(spec)
return
}
if docsPath == "" || docsPath == req.URL.Path {
header.Set("Content-Type", "text/html")
w.WriteHeader(http.StatusOK)
_, _ = w.Write(data)
}
}
}