-
Notifications
You must be signed in to change notification settings - Fork 2
/
evo.go
319 lines (286 loc) · 7.73 KB
/
evo.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package evo
import (
"fmt"
"github.com/getevo/evo-ng/lib/file"
"github.com/getevo/evo-ng/lib/generic"
websocket2 "github.com/getevo/evo-ng/websocket"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/compress"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/gofiber/fiber/v2/middleware/etag"
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/gofiber/fiber/v2/middleware/recover"
"github.com/pkg/errors"
"os"
"time"
)
type ContextInterface interface {
New() ContextInterface
Get(*Context, interface{}) error
WebSocket(*Context, interface{}, *websocket2.Conn) error
Post(*Context, interface{}) error
Put(*Context, interface{}) error
Patch(*Context, interface{}) error
All(*Context, interface{}) error
Delete(*Context, interface{}) error
Options(*Context, interface{}) error
Head(*Context, interface{}) error
Connect(*Context, interface{}) error
Use(*Context, interface{}) error
}
var app *fiber.App
var contextInstance ContextInterface
type Version struct {
Major int `json:"major"`
Minor int `json:"minor"`
Build int `json:"build"`
Hash string `json:"hash"`
Date time.Time `json:"date"`
}
type Context struct {
fiber *fiber.Ctx
}
type AssetConfig fiber.Static
type Handler func(*fiber.Ctx) error
var Config = Configuration{}
func Engine() {
var err = ParseConfig(&Config)
if err != nil {
panic(errors.WithStack(err))
}
Events.Register()
Events.On("exit", func() {
go func() {
var j = 7
for {
time.Sleep(1 * time.Second)
if j == 5 {
fmt.Println("Something prevents the app from graceful shutdown")
}
if j < 6 {
fmt.Printf("Force shutdown after %d seconds \n", j)
}
j--
if j == 0 {
os.Exit(1)
}
}
}()
})
var config = fiber.Config{
CaseSensitive: true,
StrictRouting: false,
Immutable: Config.WebServer.Immutable,
ServerHeader: Config.WebServer.ServerHeader,
UnescapePath: Config.WebServer.UnescapePath,
BodyLimit: int(generic.Parse(Config.WebServer.BodyLimit).SizeInBytes()),
DisableStartupMessage: true,
DisableKeepalive: Config.WebServer.DisableKeepalive,
ReadTimeout: Config.WebServer.ReadTimeout,
WriteTimeout: Config.WebServer.WriteTimeout,
IdleTimeout: Config.WebServer.IdleTimeout,
ReadBufferSize: Config.WebServer.ReadBufferSize,
WriteBufferSize: Config.WebServer.WriteBufferSize,
}
if config.ReadTimeout < 1 {
config.ReadTimeout = 0
}
if config.WriteTimeout < 1 {
config.WriteTimeout = 0
}
if config.IdleTimeout < 1 {
config.IdleTimeout = 0
}
if config.ReadBufferSize < 1 {
config.ReadBufferSize = 4096
}
if config.WriteBufferSize < 1 {
config.WriteBufferSize = 4096
}
if config.BodyLimit < 1 {
config.WriteBufferSize = 4 * 1024 * 1024
}
app = fiber.New(config)
if config.ETag {
app.Use(etag.New())
}
if Config.WebServer.Debug {
app.Use(logger.New())
}
if Config.WebServer.AllowOrigins != "" {
app.Use(cors.New(cors.Config{
AllowOrigins: Config.WebServer.AllowOrigins,
AllowMethods: Config.WebServer.AllowMethods,
AllowHeaders: Config.WebServer.AllowHeaders,
AllowCredentials: Config.WebServer.AllowCredentials,
ExposeHeaders: Config.WebServer.ExposeHeaders,
MaxAge: Config.WebServer.PreflightMaxCacheAge,
}))
}
if Config.WebServer.CompressLevel > -1 {
if Config.WebServer.CompressLevel > 2 {
Config.WebServer.CompressLevel = 2
}
app.Use(compress.New(compress.Config{
Level: compress.Level(Config.WebServer.CompressLevel),
}))
}
var recoverConfig = recover.Config{
EnableStackTrace: Config.WebServer.Debug,
}
app.Use(recover.New(recoverConfig))
/* app.Use("/webs",func(c *fiber.Ctx) error {
if websocket.IsWebSocketUpgrade(c) {
return c.Next()
}
return fiber.ErrUpgradeRequired
})
app.Get("/webs",websocket.New(func(c *websocket.Conn) {
fmt.Println("here")
// websocket.Conn bindings https://pkg.go.dev/github.com/fasthttp/websocket?tab=doc#pkg-index
var (
mt int
msg []byte
err error
)
for {
if mt, msg, err = c.ReadMessage(); err != nil {
log.Println("read:", err)
break
}
fmt.Printf("recv: %s \n", msg)
if err = c.WriteMessage(mt, []byte(c.Fiber.BaseURL()+":"+string(msg))); err != nil {
log.Println("write:", err)
break
}
}
}))*/
}
func UseContext(context ContextInterface) {
contextInstance = context
}
func WebSocket(url string, callback interface{}, params ...interface{}) {
app.Get(url, func(c *fiber.Ctx) error {
if websocket2.IsWebSocketUpgrade(c) {
c.Locals("allowed", true)
return c.Next()
}
return fiber.ErrUpgradeRequired
}, websocket2.New(func(c *websocket2.Conn) {
var ct = Context{
fiber: c.Fiber,
}
err := contextInstance.New().WebSocket(&ct, callback, c)
if err != nil {
return
}
}))
}
func Get(url string, callback interface{}, params ...interface{}) {
app.Get(url, func(ctx *fiber.Ctx) error {
var ct = Context{
fiber: ctx,
}
return contextInstance.New().Get(&ct, callback)
})
}
func Post(url string, callback interface{}, params ...interface{}) {
app.Post(url, func(ctx *fiber.Ctx) error {
var ct = Context{
fiber: ctx,
}
return contextInstance.New().Get(&ct, callback)
})
}
func Patch(url string, callback interface{}, params ...interface{}) {
app.Patch(url, func(ctx *fiber.Ctx) error {
var ct = Context{
fiber: ctx,
}
return contextInstance.New().Get(&ct, callback)
})
}
func Put(url string, callback interface{}, params ...interface{}) {
app.Put(url, func(ctx *fiber.Ctx) error {
var ct = Context{
fiber: ctx,
}
return contextInstance.New().Get(&ct, callback)
})
}
func All(url string, callback interface{}, params ...interface{}) {
app.All(url, func(ctx *fiber.Ctx) error {
var ct = Context{
fiber: ctx,
}
return contextInstance.New().Get(&ct, callback)
})
}
func Delete(url string, callback interface{}, params ...interface{}) {
app.Delete(url, func(ctx *fiber.Ctx) error {
var ct = Context{
fiber: ctx,
}
return contextInstance.New().Get(&ct, callback)
})
}
func Options(url string, callback interface{}, params ...interface{}) {
app.Options(url, func(ctx *fiber.Ctx) error {
var ct = Context{
fiber: ctx,
}
return contextInstance.New().Get(&ct, callback)
})
}
func Connect(url string, callback interface{}, params ...interface{}) {
app.Connect(url, func(ctx *fiber.Ctx) error {
var ct = Context{
fiber: ctx,
}
return contextInstance.New().Get(&ct, callback)
})
}
func Head(url string, callback interface{}, params ...interface{}) {
app.Head(url, func(ctx *fiber.Ctx) error {
var ct = Context{
fiber: ctx,
}
return contextInstance.New().Get(&ct, callback)
})
}
func Use(url string, callback interface{}, params ...interface{}) {
app.Use(url, func(ctx *fiber.Ctx) error {
var ct = Context{
fiber: ctx,
}
return contextInstance.New().Use(&ct, callback)
})
}
// Asset set static file path.
// first arg takes url to match
// second arg takes local path
// returns error if the localPath does not exists
func Asset(url, localPath string, config ...AssetConfig) error {
if !file.IsDir(localPath) {
return fmt.Errorf("static file path %s does not exists", localPath)
}
if len(config) > 0 {
app.Static(url, localPath, fiber.Static(config[0]))
} else {
app.Static(url, localPath)
}
app.Use(url, func(c *fiber.Ctx) error {
c.Status(fiber.StatusNotFound)
return fmt.Errorf("requested URL %s was not found on this server", c.Path())
})
return nil
}
func Run(ready func()) {
ready()
Events.Trigger("ready")
fmt.Println("Starting server", Config.WebServer.Bind+":"+Config.WebServer.Port, "...")
var err = app.Listen(Config.WebServer.Bind + ":" + Config.WebServer.Port)
if err != nil {
fmt.Println(err)
}
}