Debian Codegraph @

server/server_builder.go

raw
1 package server
2
3 import (
4 "io"
5 "net"
6 "net/http"
7 "net/http/httptest"
8 "os"
9 "time"
10
11 "github.com/gin-gonic/gin"
12 "github.com/henrybear327/go-proton-api"
13 "github.com/henrybear327/go-proton-api/server/backend"
14 )
15
16 type serverBuilder struct {
17 config *http.Server
18 listener net.Listener
19 withTLS bool
20 domain string
21 logger io.Writer
22 origin string
23 proxyTransport *http.Transport
24 cacher AuthCacher
25 rateLimiter *rateLimiter
26 enableDedup bool
27 }
28
29 func newServerBuilder() *serverBuilder {
30 var logger io.Writer
31
32 if os.Getenv("GO_PROTON_API_SERVER_LOGGER_ENABLED") != "" {
33 logger = gin.DefaultWriter
34 } else {
35 logger = io.Discard
36 }
37
38 return &serverBuilder{
39 config: &http.Server{},
40 withTLS: true,
41 domain: "proton.local",
42 logger: logger,
43 origin: proton.DefaultHostURL,
44 proxyTransport: &http.Transport{},
45 }
46 }
47
48 func (builder *serverBuilder) build() *Server {
49 gin.SetMode(gin.ReleaseMode)
50
51 s := &Server{
52 r: gin.New(),
53 b: backend.New(time.Hour, builder.domain, builder.enableDedup),
54
55 domain: builder.domain,
56 proxyOrigin: builder.origin,
57 authCacher: builder.cacher,
58 rateLimit: builder.rateLimiter,
59 proxyTransport: builder.proxyTransport,
60 }
61
62 s.r.Use(gin.CustomRecovery(func(c *gin.Context, recovered any) {
63 c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{
64 "Code": http.StatusInternalServerError,
65 "Error": "Internal server error",
66 "Details": recovered,
67 })
68 }))
69
70 var l net.Listener
71
72 if builder.listener == nil {
73 var err error
74
75 if l, err = net.Listen("tcp", "127.0.0.1:0"); err != nil {
76 panic(err)
77 }
78 } else {
79 l = builder.listener
80 }
81
82 // Create the test server.
83 s.s = &httptest.Server{
84 Listener: l,
85 Config: builder.config,
86 }
87
88 // Set the server to use the custom handler.
89 s.s.Config.Handler = s.r
90
91 // Start the server.
92 if builder.withTLS {
93 s.s.StartTLS()
94 } else {
95 s.s.Start()
96 }
97
98 s.r.Use(
99 gin.LoggerWithConfig(gin.LoggerConfig{Output: builder.logger}),
100 gin.Recovery(),
101 s.logCalls(),
102 s.handleOffline(),
103 )
104
105 initRouter(s)
106
107 return s
108 }
109
110 // Option represents a type that can be used to configure the server.
111 type Option interface {
112 config(*serverBuilder)
113 }
114
115 // WithTLS controls whether the server should serve over TLS.
116 func WithTLS(tls bool) Option {
117 return &withTLS{
118 withTLS: tls,
119 }
120 }
121
122 type withTLS struct {
123 withTLS bool
124 }
125
126 func (opt withTLS) config(builder *serverBuilder) {
127 builder.withTLS = opt.withTLS
128 }
129
130 // WithDomain controls the domain of the server.
131 func WithDomain(domain string) Option {
132 return &withDomain{
133 domain: domain,
134 }
135 }
136
137 type withDomain struct {
138 domain string
139 }
140
141 func (opt withDomain) config(builder *serverBuilder) {
142 builder.domain = opt.domain
143 }
144
145 // WithLogger controls where Gin logs to.
146 func WithLogger(logger io.Writer) Option {
147 return &withLogger{
148 logger: logger,
149 }
150 }
151
152 type withLogger struct {
153 logger io.Writer
154 }
155
156 func (opt withLogger) config(builder *serverBuilder) {
157 builder.logger = opt.logger
158 }
159
160 func WithProxyOrigin(origin string) Option {
161 return &withProxyOrigin{
162 origin: origin,
163 }
164 }
165
166 type withProxyOrigin struct {
167 origin string
168 }
169
170 func (opt withProxyOrigin) config(builder *serverBuilder) {
171 builder.origin = opt.origin
172 }
173
174 func WithAuthCacher(cacher AuthCacher) Option {
175 return &withAuthCache{
176 cacher: cacher,
177 }
178 }
179
180 type withAuthCache struct {
181 cacher AuthCacher
182 }
183
184 func (opt withAuthCache) config(builder *serverBuilder) {
185 builder.cacher = opt.cacher
186 }
187
188 func WithRateLimit(limit int, window time.Duration) Option {
189 return &withRateLimit{
190 limit: limit,
191 window: window,
192 statusCode: http.StatusTooManyRequests,
193 }
194 }
195
196 func WithRateLimitAndCustomStatusCode(limit int, window time.Duration, code int) Option {
197 return &withRateLimit{
198 limit: limit,
199 window: window,
200 statusCode: code,
201 }
202 }
203
204 type withRateLimit struct {
205 limit int
206 statusCode int
207 window time.Duration
208 }
209
210 func (opt withRateLimit) config(builder *serverBuilder) {
211 builder.rateLimiter = newRateLimiter(opt.limit, opt.window, opt.statusCode)
212 }
213
214 func WithProxyTransport(transport *http.Transport) Option {
215 return &withProxyTransport{
216 transport: transport,
217 }
218 }
219
220 type withProxyTransport struct {
221 transport *http.Transport
222 }
223
224 func (opt withProxyTransport) config(builder *serverBuilder) {
225 builder.proxyTransport = opt.transport
226 }
227
228 type withServerConfig struct {
229 cfg *http.Server
230 }
231
232 func (opt withServerConfig) config(builder *serverBuilder) {
233 builder.config = opt.cfg
234 }
235
236 // WithServerConfig allows you to configure the underlying HTTP server.
237 func WithServerConfig(cfg *http.Server) Option {
238 return withServerConfig{
239 cfg: cfg,
240 }
241 }
242
243 type withNetListener struct {
244 listener net.Listener
245 }
246
247 func (opt withNetListener) config(builder *serverBuilder) {
248 builder.listener = opt.listener
249 }
250
251 // WithListener allows you to set the net.Listener to use.
252 func WithListener(listener net.Listener) Option {
253 return withNetListener{
254 listener: listener,
255 }
256 }
257
258 type withMessageDedup struct{}
259
260 func (withMessageDedup) config(builder *serverBuilder) {
261 builder.enableDedup = true
262 }
263
264 func WithMessageDedup() Option {
265 return &withMessageDedup{}
266 }