-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.go
225 lines (188 loc) · 5.54 KB
/
app.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package azugo
import (
"fmt"
"sync"
"azugo.io/azugo/config"
"azugo.io/azugo/internal/radix"
"azugo.io/core"
"azugo.io/core/cert"
"github.com/dgrr/http2"
"github.com/spf13/cobra"
"github.com/valyala/fasthttp"
"go.uber.org/zap"
)
type App struct {
*core.App
// Routing tree
trees []*radix.Tree
treeMutable bool
customMethodsIndex map[string]int
registeredPaths map[string][]string
// Router middlewares
middlewares []RequestHandlerFunc
// Cached value of global (*) allowed methods
globalAllowed string
// Request context pool
ctxPool sync.Pool
// Pointer to the originally set base path in RouterOptions
originalBasePath *string
// Cached value of base path
fixedBasePath string
pathLock sync.RWMutex
// Router options
RouterOptions RouterOptions
// Configuration
config *config.Configuration
// Metrics options
MetricsOptions MetricsOptions
// Server options
ServerOptions ServerOptions
}
type ServerOptions struct {
// Per-connection buffer size for requests' reading.
// This also limits the maximum header size.
//
// Increase this buffer if your clients send multi-KB RequestURIs
// and/or multi-KB headers (for example, BIG cookies).
//
// Default buffer size 8K is used if not set.
RequestReadBufferSize int
// Per-connection buffer size for responses' writing.
//
// Default buffer size 8K is used if not set.
ResponseWriteBufferSize int
}
func New() *App {
a := &App{
App: core.New(),
trees: make([]*radix.Tree, 10),
customMethodsIndex: make(map[string]int),
registeredPaths: make(map[string][]string),
middlewares: make([]RequestHandlerFunc, 0, 10),
RouterOptions: RouterOptions{
Proxy: defaultProxyOptions,
CORS: defaultCORSOptions,
RedirectTrailingSlash: true,
RedirectFixedPath: true,
HandleMethodNotAllowed: true,
HandleOPTIONS: true,
PanicHandler: func(ctx *Context, err any) {
ctx.Log().Error("Unhandled error", zap.Any("error", err))
},
GlobalOPTIONS: func(ctx *Context) {
ctx.StatusCode(fasthttp.StatusNoContent)
},
},
ServerOptions: ServerOptions{
RequestReadBufferSize: 8192,
ResponseWriteBufferSize: 8192,
},
MetricsOptions: defaultMetricsOptions,
}
return a
}
// basePath returns base path of the application
func (a *App) basePath() string {
a.pathLock.RLock()
defer a.pathLock.RUnlock()
if a.originalBasePath == nil || *a.originalBasePath != a.Config().Server.Path {
a.pathLock.RUnlock()
a.pathLock.Lock()
a.originalBasePath = &a.Config().Server.Path
a.fixedBasePath = a.Config().Server.Path
// Add leading slash
if len(a.fixedBasePath) > 0 && a.fixedBasePath[0] != '/' {
a.fixedBasePath = "/" + a.fixedBasePath
}
// Strip trailing slash
if len(a.fixedBasePath) > 0 && a.fixedBasePath[len(a.fixedBasePath)-1] == '/' {
a.fixedBasePath = a.fixedBasePath[:len(a.fixedBasePath)-1]
}
a.pathLock.Unlock()
a.pathLock.RLock()
}
return a.fixedBasePath
}
// SetConfig binds application configuration to the application
func (a *App) SetConfig(cmd *cobra.Command, conf *config.Configuration) {
if a.config != nil && a.config.Ready() {
return
}
a.config = conf
}
// Config returns application configuration.
//
// Panics if configuration is not loaded.
func (a *App) Config() *config.Configuration {
if a.config == nil || !a.config.Ready() {
panic("configuration is not loaded")
}
return a.config
}
// Start web application.
func (a *App) Start() error {
if err := a.App.Start(); err != nil {
return err
}
server := &fasthttp.Server{
NoDefaultServerHeader: true,
Handler: a.Handler,
Logger: zap.NewStdLog(a.Log().Named("http")),
StreamRequestBody: true,
DisablePreParseMultipartForm: true,
ReadBufferSize: a.ServerOptions.RequestReadBufferSize,
WriteBufferSize: a.ServerOptions.ResponseWriteBufferSize,
}
conf := a.Config().Server
// HTTP2 is supported only over HTTPS
if conf.HTTPS != nil && conf.HTTPS.Enabled {
http2.ConfigureServer(server, http2.ServerConfig{})
}
var wg sync.WaitGroup
if conf.HTTP != nil && conf.HTTP.Enabled {
addr := conf.HTTP.Address
if addr == "0.0.0.0" {
addr = ""
}
wg.Add(1)
go func() {
defer wg.Done()
a.Log().Info(fmt.Sprintf("Listening on http://%s:%d%s...", conf.HTTP.Address, conf.HTTP.Port, conf.Path))
if err := server.ListenAndServe(fmt.Sprintf("%s:%d", addr, conf.HTTP.Port)); err != nil {
a.Log().Error("failed to start HTTP server", zap.Error(err))
}
}()
}
if conf.HTTPS != nil && conf.HTTPS.Enabled {
addr := conf.HTTPS.Address
if addr == "0.0.0.0" {
addr = ""
}
var certData []byte
var keyData []byte
var err error
if len(conf.HTTPS.CertificatePEMFile) > 0 {
certData, keyData, err = cert.LoadTLSCertificate(conf.HTTPS.CertificatePEMFile)
if err != nil {
a.Log().Error("failed to load TLS certificate", zap.Error(err))
return err
}
} else {
certData, keyData, err = cert.DevTLSCertificate("azugo", "localhost")
if err != nil {
a.Log().Error("failed to load or generate self-signed TLS certificate", zap.Error(err))
return err
}
}
wg.Add(1)
go func() {
defer wg.Done()
a.Log().Info(fmt.Sprintf("Listening on https://%s:%d%s...", conf.HTTPS.Address, conf.HTTPS.Port, conf.Path))
if err := server.ListenAndServeTLSEmbed(fmt.Sprintf("%s:%d", addr, conf.HTTPS.Port), certData, keyData); err != nil {
a.Log().Error("failed to start HTTPS server", zap.Error(err))
}
}()
}
wg.Wait()
return nil
}