forked from gramework/gramework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnew.go
More file actions
94 lines (83 loc) · 2.58 KB
/
Copy pathnew.go
File metadata and controls
94 lines (83 loc) · 2.58 KB
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
// Copyright 2017-present Kirill Danshin and Gramework contributors
// Copyright 2019-present Highload LTD (UK CN: 11893420)
//
// 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/microcosm-cc/bluemonday"
"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,
sanitizerPolicy: bluemonday.StrictPolicy(),
}
for _, opt := range opts {
opt(app)
}
if app.serverBase == nil {
app.serverBase = newDefaultServerBaseFor(app)
}
// avoid race condition then OptUseServer becomes before OptAppName
// or OptUseCustomLogger becomes before OptUseServer
app.serverBase.Name = app.name
app.serverBase.Logger = NewFastHTTPLoggerAdapter(&app.Logger)
app.defaultRouter = &Router{
router: newRouter(),
app: app,
}
return app
}
// Common code used with `gramework.New()` itself and `Opt*` functions.
func newDefaultServerBaseFor(app *App) *fasthttp.Server {
return &fasthttp.Server{
Handler: app.handler(),
Logger: NewFastHTTPLoggerAdapter(&app.Logger),
}
}