/
server.go
162 lines (142 loc) · 4.14 KB
/
server.go
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
package rest
import (
"bytes"
_ "embed"
"html/template"
"net/http"
"strings"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/swaggest/openapi-go/openapi3"
)
//go:embed swagger.tmpl
var swagger string
type Scheme string
const (
SchemeBearer = Scheme("bearer")
SchemeBasic = Scheme("basic")
)
type Service struct {
*echo.Echo
baseUrl string
group *Group
reflector *openapi3.Reflector
OpenAPI *openapi3.Spec
}
func customHTTPErrorHandler(err error, c echo.Context) {
if c.Response().Committed {
return
}
code, errRes := Err(err)
// Send response
if c.Request().Method == http.MethodHead { // Issue #608
c.NoContent(code)
} else {
c.JSON(code, errRes)
}
}
func NewService(baseUrl ...string) *Service {
s := &Service{}
s.reflector = &openapi3.Reflector{}
s.OpenAPI = &openapi3.Spec{Openapi: "3.0.3"}
if len(baseUrl) > 0 {
s.baseUrl = baseUrl[0]
s.OpenAPI.WithServers(openapi3.Server{
URL: s.baseUrl,
})
}
s.reflector.Spec = s.OpenAPI
e := echo.New()
e.HideBanner = true
e.Binder = &CustomBinder{}
e.Validator = &CustomValidator{}
e.HTTPErrorHandler = customHTTPErrorHandler
// Root level middleware
e.Use(middleware.Logger())
e.Use(middleware.Recover())
s.Echo = e
s.group = s.Group("")
return s
}
func (s *Service) GET(pattern string, h Interactor, middleware ...echo.MiddlewareFunc) *echo.Route {
return s.group.GET(pattern, h, middleware...)
}
func (s *Service) POST(pattern string, h Interactor, middleware ...echo.MiddlewareFunc) *echo.Route {
return s.group.POST(pattern, h, middleware...)
}
func (s *Service) PATCH(pattern string, h Interactor, middleware ...echo.MiddlewareFunc) *echo.Route {
return s.group.PATCH(pattern, h, middleware...)
}
func (s *Service) PUT(pattern string, h Interactor, middleware ...echo.MiddlewareFunc) *echo.Route {
return s.group.PUT(pattern, h, middleware...)
}
func (s *Service) DELETE(pattern string, h Interactor, middleware ...echo.MiddlewareFunc) *echo.Route {
return s.group.DELETE(pattern, h, middleware...)
}
func (s *Service) Group(prefix string, ops ...option) *Group {
group := &Group{}
group.Group = s.Echo.Group(s.baseUrl + parenthesesToColon(prefix))
group.reflector = s.reflector
group.prefix = prefix
group.ops = ops
return group
}
func (s *Service) Docs(pattern string, config ...map[string]any) {
pattern = strings.TrimRight(pattern, "/")
s.Echo.GET(s.baseUrl+pattern+"/openapi.json", func(c echo.Context) error {
schema, err := s.OpenAPI.MarshalJSON()
if err != nil {
return err
}
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
return c.String(http.StatusOK, string(schema))
})
s.Echo.Any(s.baseUrl+pattern+"*", func(c echo.Context) error {
t := template.Must(template.New("swagger").Parse(swagger))
var html bytes.Buffer
var setting map[string]any
if len(config) > 0 {
setting = config[0]
}
t.Execute(&html, map[string]any{
"AssetBase": "https://cdn.jsdelivr.net/npm/swagger-ui-dist",
"SwaggerJson": s.baseUrl + pattern + "/openapi.json",
"Setting": setting,
})
return c.HTML(http.StatusOK, html.String())
})
}
func (s *Service) WithSecurity(key string, securityScheme *openapi3.SecurityScheme) {
s.OpenAPI.ComponentsEns().SecuritySchemesEns().WithMapOfSecuritySchemeOrRefValuesItem(key,
openapi3.SecuritySchemeOrRef{
SecurityScheme: securityScheme,
})
}
func (s *Service) WithHttpBearerSecurity(key string) {
s.WithHttpSecurity(key, SchemeBearer)
}
func (s *Service) WithHttpBasicSecurity(key string) {
s.WithHttpSecurity(key, SchemeBasic)
}
func (s *Service) WithHttpSecurity(key string, scheme Scheme) {
s.WithSecurity(key, &openapi3.SecurityScheme{
HTTPSecurityScheme: &openapi3.HTTPSecurityScheme{
Scheme: string(scheme),
},
})
}
func (s *Service) WithAPIKeySecurity(key, name string, in openapi3.APIKeySecuritySchemeIn) {
s.WithSecurity(key, &openapi3.SecurityScheme{
APIKeySecurityScheme: &openapi3.APIKeySecurityScheme{
In: in,
Name: name,
},
})
}
func (s *Service) WithTags(val ...string) {
tags := make([]openapi3.Tag, 0)
for _, v := range val {
tags = append(tags, openapi3.Tag{Name: v})
}
s.OpenAPI.WithTags(tags...)
}