-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.go
More file actions
172 lines (151 loc) · 3.93 KB
/
handler.go
File metadata and controls
172 lines (151 loc) · 3.93 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package elements_echo
import (
"bytes"
"embed"
_ "embed"
"fmt"
"github.com/labstack/echo/v4"
htmlt "html/template"
"net/http"
"os"
"path/filepath"
"strings"
)
//go:embed template/index.html
var template string
//go:embed template/favicon.png
var icon string
//go:embed template/styles.min.css
var styles string
//go:embed template/web-components.min.js
var script string
//go:embed template/styles.override.css
var overrides string
type StopLightMiddleware struct {
urlPrefix string
specContent []byte
specFormat string
}
func (m *StopLightMiddleware) UseSpecFile(path string) (*StopLightMiddleware, error) {
ext := filepath.Ext(path)
if ext == ".json" {
m.specFormat = "json"
} else if ext == ".yaml" || ext == ".yml" {
m.specFormat = "yaml"
}
content, err := os.ReadFile(path)
if err != nil {
return nil, err
}
m.specContent = content
return m, nil
}
func (m *StopLightMiddleware) UseEmbed(fs embed.FS, path string) (*StopLightMiddleware, error) {
ext := filepath.Ext(path)
if ext == ".json" {
m.specFormat = "json"
} else if ext == ".yaml" || ext == ".yml" {
m.specFormat = "yaml"
}
content, err := fs.ReadFile(path)
if err != nil {
return nil, err
}
m.specContent = content
return m, nil
}
func (m *StopLightMiddleware) UseContent(content []byte, format string) *StopLightMiddleware {
m.specFormat = format
m.specContent = content
return m
}
func New(urlPrefix string) *StopLightMiddleware {
return &StopLightMiddleware{urlPrefix: urlPrefix}
}
func (m *StopLightMiddleware) Handle() echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(ctx echo.Context) error {
url := ctx.Request().URL.Path
stripped := strings.TrimPrefix(url, m.urlPrefix)
if len(stripped) == len(url) {
return next(ctx)
}
m.httpHandler(ctx.Response(), ctx.Request())
if ctx.Response().Committed {
return nil
}
return next(ctx)
}
}
}
func (m *StopLightMiddleware) template() ([]byte, error) {
var t, err = htmlt.New("index.html").Parse(template)
if err != nil {
return nil, err
}
buf := new(bytes.Buffer)
err = t.Execute(buf, map[string]string{"Path": m.urlPrefix, "SwaggerFile": fmt.Sprintf("swagger.%s", m.specFormat)})
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func (m *StopLightMiddleware) httpHandler(w http.ResponseWriter, req *http.Request) {
data, err := m.template()
if err != nil {
return
}
method := strings.ToLower(req.Method)
if method != "get" && method != "head" {
return
}
header := w.Header()
strippedPrefix := strings.TrimPrefix(req.URL.Path, m.urlPrefix)
if strippedPrefix == "/" || strippedPrefix == "/index.html" || strippedPrefix == "" {
header.Set("Content-Type", "text/html")
w.WriteHeader(http.StatusOK)
_, _ = w.Write(data)
return
}
if strippedPrefix == "/swagger.yaml" && m.specFormat == "yaml" {
header.Set("Content-Type", "application/yaml")
w.WriteHeader(http.StatusOK)
_, _ = w.Write(m.specContent)
return
}
if strippedPrefix == "/swagger.yml" && m.specFormat == "yaml" {
header.Set("Content-Type", "application/yaml")
w.WriteHeader(http.StatusOK)
_, _ = w.Write(m.specContent)
return
}
if strippedPrefix == "/swagger.json" && m.specFormat == "json" {
header.Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = w.Write(m.specContent)
}
if strippedPrefix == "/script.js" {
header.Set("Content-Type", "application/javascript")
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(script))
return
}
if strippedPrefix == "/styles.css" {
header.Set("Content-Type", "text/css")
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(styles))
return
}
if strippedPrefix == "/overrides.css" {
header.Set("Content-Type", "text/css")
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(overrides))
return
}
if strippedPrefix == "/favicon.png" {
header.Set("Content-Type", "image/png")
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(icon))
return
}
}