-
Notifications
You must be signed in to change notification settings - Fork 38
/
new.go
79 lines (71 loc) · 2.05 KB
/
new.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
// Copyright 2017-present Kirill Danshin and Gramework contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
package gramework
import (
"os"
"sync"
"time"
"github.com/apex/log"
"github.com/apex/log/handlers/cli"
"github.com/valyala/fasthttp"
)
var defaultMaxHackAttempts int32 = 5
// New App
func New(opts ...func(*App)) *App {
logger := &log.Logger{
Level: Logger.Level,
Handler: cli.New(os.Stdout),
}
flags := &Flags{
values: make(map[string]Flag),
}
defFWLimit := int64(-1)
defBlockTimeout := int64(-1)
maxHackAttempts := defaultMaxHackAttempts
app := &App{
Flags: flags,
flagsQueue: flagsToRegister,
Logger: logger,
name: DefaultAppName,
domainListLock: new(sync.RWMutex),
firewall: &firewall{
blockList: make(map[string]int64),
MaxReqPerMin: &defFWLimit,
BlockTimeout: &defBlockTimeout,
requestCounter: make(map[string]int64),
},
firewallInit: new(sync.Once),
domains: make(map[string]*Router),
middlewaresMu: new(sync.RWMutex),
middlewaresAfterRequestMu: new(sync.RWMutex),
preMiddlewaresMu: new(sync.RWMutex),
middlewares: make([]func(*Context), 0),
middlewaresAfterRequest: make([]func(*Context), 0),
preMiddlewares: make([]func(*Context), 0),
seed: uintptr(time.Now().Nanosecond()),
maxHackAttempts: &maxHackAttempts,
runningServersMu: new(sync.Mutex),
internalLog: internalLog,
cookieExpire: 6 * time.Hour,
cookiePath: defaultCookiePath,
}
app.serverBase = &fasthttp.Server{
Handler: app.handler(),
Logger: NewFastHTTPLoggerAdapter(&app.Logger),
Name: app.name,
}
app.defaultRouter = &Router{
router: newRouter(),
app: app,
}
for _, opt := range opts {
opt(app)
}
return app
}