-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
165 lines (139 loc) · 5.04 KB
/
config.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
package govalin
import (
"time"
"github.com/pkkummermo/govalin/internal/session"
)
const (
defaultPort = 6060 // govalin default port.
defaultMaxReadTimeout = 10 // maximum read timeout for requests.
defaultMaxBodyReadSize int64 = 4096 // Default max body read size.
defaultShutdownTimeoutInMS = 200 // Max time for shutdown.
defaultSessionExpireTime = 3600 * time.Second // Default session expire time.
)
// ConfigFunc gives a config function that will generate a Config
// for the Govalin object.
type ConfigFunc func(config *Config)
type serverConfig struct {
port uint16
maxReadTimeout int64
maxBodyReadSize int64
shutdownTimeoutInMS int64
accessLogEnabled bool
startupLogEnabled bool
plugins []Plugin
sessionsEnabled bool
sessionStore session.Store
sessionExpireTime time.Duration
events ServerEvents
}
type ServerEvents struct {
onServerStartup []OnServerStartup
onServerShutdown []OnServerShutdown
onRouteAdded []OnRouteAdded
}
type OnServerStartup func()
type OnServerShutdown func()
type OnRouteAdded func(method string, path string, handler HandlerFunc)
func (events *ServerEvents) AddOnServerStartup(event OnServerStartup) {
events.onServerStartup = append(events.onServerStartup, event)
}
func (events *ServerEvents) AddOnServerShutdown(event OnServerShutdown) {
events.onServerShutdown = append(events.onServerShutdown, event)
}
func (events *ServerEvents) AddOnRouteAdded(event OnRouteAdded) {
events.onRouteAdded = append(events.onRouteAdded, event)
}
// Config contains configuration for a Govalin instance.
type Config struct {
server serverConfig
}
// Plugin lets you to provide a Plugin that can interact on the Govalin
// instance.
func (config *Config) Plugin(plugin Plugin) *Config {
config.server.plugins = append(config.server.plugins, plugin)
return config
}
// Events allows you to subscribe to server events such as startup and shutdown on the Govalin instance.
func (config *Config) Events(eventFunc func(serverEvents *ServerEvents)) *Config {
eventFunc(&config.server.events)
return config
}
// Port sets the default port of the Govalin instance.
func (config *Config) Port(port uint16) *Config {
config.server.port = port
return config
}
// ServerMaxBodyReadSize sets the max read size to accept from POST requests.
//
// The server will error if the body size is too big and refuse to handle the
// request further. This is to control DDoS attacks using big body sizes.
func (config *Config) ServerMaxBodyReadSize(maxReadSize int64) *Config {
config.server.maxBodyReadSize = maxReadSize
return config
}
// ServerMaxReadTimeout sets the max read timeout for requests towards the Govalin server.
func (config *Config) ServerMaxReadTimeout(timeout int64) *Config {
config.server.maxReadTimeout = timeout
return config
}
// EnableSessions configures govalin to use sessions for all requests.
func (config *Config) EnableSessions(confFunc ...SessionConfigFunc) *Config {
configuredSession := SessionConfiguration{
sessionExpireTime: defaultSessionExpireTime,
sessionStore: session.NewInMemoryStore(),
}
if (len(confFunc)) > 0 {
confFunc[0](&configuredSession)
}
config.server.sessionsEnabled = true
config.server.sessionExpireTime = configuredSession.sessionExpireTime
config.server.sessionStore = configuredSession.sessionStore
return config
}
// ServerShutdownTimeout sets the max timeout for before forcefully shutting the server down.
func (config *Config) ServerShutdownTimeout(timeout int64) *Config {
config.server.shutdownTimeoutInMS = timeout
return config
}
// EnableAccessLog enables access logging for the server. Default is enabled.
func (config *Config) EnableAccessLog(enabled bool) *Config {
config.server.accessLogEnabled = enabled
return config
}
func (config *Config) EnableStartupLog(enabled bool) *Config {
config.server.startupLogEnabled = enabled
return config
}
func newConfig() *Config {
return &Config{
server: serverConfig{
port: defaultPort,
maxReadTimeout: defaultMaxReadTimeout,
maxBodyReadSize: defaultMaxBodyReadSize,
shutdownTimeoutInMS: defaultShutdownTimeoutInMS,
sessionsEnabled: false,
accessLogEnabled: true,
startupLogEnabled: true,
events: ServerEvents{
onServerStartup: []OnServerStartup{},
onServerShutdown: []OnServerShutdown{},
onRouteAdded: []OnRouteAdded{},
},
},
}
}
type SessionConfiguration struct {
sessionExpireTime time.Duration
sessionStore session.Store
}
// SessionExpireTime sets the expire time for sessions.
func (config *SessionConfiguration) SessionExpireTime(expireTime time.Duration) *SessionConfiguration {
config.sessionExpireTime = expireTime
return config
}
// SessionStore sets the session store to use.
func (config *SessionConfiguration) SessionStore(store session.Store) *SessionConfiguration {
config.sessionStore = store
return config
}
type SessionConfigFunc func(sessionConfig *SessionConfiguration)