-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_constructor.go
More file actions
242 lines (202 loc) · 7.97 KB
/
server_constructor.go
File metadata and controls
242 lines (202 loc) · 7.97 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
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
package server
import (
"context"
"github.com/gin-gonic/gin"
"github.com/gookit/color"
"github.com/kyaxcorp/go-helper/certs"
"github.com/kyaxcorp/go-helper/conv"
"github.com/kyaxcorp/go-helper/errors2/define"
"github.com/kyaxcorp/go-helper/file"
"github.com/kyaxcorp/go-helper/filesystem"
"github.com/kyaxcorp/go-helper/sync/_bool"
"github.com/kyaxcorp/go-helper/sync/_map_string_interface"
"github.com/kyaxcorp/go-helper/sync/_time"
"github.com/kyaxcorp/go-helper/sync/_uint64"
"github.com/kyaxcorp/go-http/config"
"github.com/kyaxcorp/go-http/middlewares/connection"
"github.com/kyaxcorp/go-http/middlewares/request_timing"
"github.com/kyaxcorp/go-http/routes/ping"
"github.com/kyaxcorp/go-logger"
"github.com/kyaxcorp/go-logger/appLog"
loggerConfig "github.com/kyaxcorp/go-logger/config"
loggerPaths "github.com/kyaxcorp/go-logger/paths"
"github.com/rs/zerolog"
)
// You can use the default constructor!
func New(
ctx context.Context,
config config.Config,
) (*Server, error) {
info := func() *zerolog.Event {
return appLog.InfoF("New HTTP Server")
}
warn := func() *zerolog.Event {
return appLog.WarnF("New HTTP Server")
}
_debug := func() *zerolog.Event {
return appLog.DebugF("New HTTP Server")
}
_error := func() *zerolog.Event {
return appLog.ErrorF("New HTTP Server")
}
info().Msg("entering...")
defer info().Msg("leaving...")
if !conv.ParseBool(config.IsEnabled) {
_error().Str("instance_name", config.Name).Msg("http server is disabled, check your config")
return nil, define.Err(0, "http server is disabled, check your config", config.Name)
}
info().Msg("configuring logger")
var loggerDirPath string
// Setting default values for logger
if config.Logger.Name == "" {
config.Logger.Name = config.Name
}
// If DirLogPath is not defined, it will set the default folder!
if config.Logger.DirLogPath == "" {
loggerDirPath = loggerPaths.GetLogsPathForServers("http/" + config.Logger.Name)
config.Logger.DirLogPath = loggerDirPath + filesystem.DirSeparator() + "server" + filesystem.DirSeparator()
_debug().Str("generated_dir_log_path", config.Logger.DirLogPath).Msg("logger DirLogPath empty, generating...")
} else {
loggerDirPath = config.Logger.DirLogPath
// Correct the path!
config.Logger.DirLogPath = file.FilterPath(loggerDirPath + filesystem.DirSeparator() + "server" + filesystem.DirSeparator())
_debug().Str("generated_dir_log_path", config.Logger.DirLogPath).Msg("correcting dir log path")
}
// Set Module Name
if config.Logger.ModuleName == "" {
config.Logger.ModuleName = "HTTP Server=" + config.Name
}
if conv.ParseBool(config.EnableUnsecure) && len(config.ListeningAddresses) == 0 {
_error().Msg(color.Style{color.LightRed}.Render("unsecure connections are enabled but no listening addresses"))
return nil, define.Err(0, "no listening addresses are provided", config.Name)
}
if conv.ParseBool(config.EnableSSL) && len(config.ListeningAddressesSSL) == 0 {
_error().Msg(color.Style{color.LightRed}.Render("secure connections are enabled but no listening addresses"))
return nil, define.Err(0, "no listening ssl addresses are provided", config.Name)
}
if conv.ParseBool(config.EnableSSL) {
info().Msg("checking certificates...")
// if ssl enabled, check certificates
sslKeyFilePathEmpty := false
sslCertFilePathEmpty := false
if config.SSLKeyFilePath == "" {
sslKeyFilePathEmpty = true
warn().Msg("param SSLKeyFilePath empty...")
}
if config.SSLCertFilePath == "" {
sslCertFilePathEmpty = true
warn().Msg("param SSLCertFilePath empty...")
}
// Missing paths...
if sslKeyFilePathEmpty && sslCertFilePathEmpty {
warn().Msg("params SSLCertFilePath & SSLKeyFilePath are empty, checking auto generation...")
// Auto Generating certificates
if conv.ParseBool(config.SSLAutoGenerateCerts) {
info().Msg("auto generating ssl certificates")
// Auto Generate
certsConfig := &certs.CertGeneration{
Host: "localhost",
}
certificatesInstanceName := "http_" + config.Name
info().Str("certs_instance_name", certificatesInstanceName).Msg("generating instance name")
// TODO: should we filter the naming... it's important filtration for files!
_err := certs.GenerateCerts(certificatesInstanceName, certsConfig)
if _err != nil {
return nil, define.Err(0, "failed to generate http certificates", _err.Error(), config.Name)
}
info().Msg(color.Style{color.LightGreen}.Render("certificates generated successfully"))
config.SSLKeyFilePath = certsConfig.KeyPath
config.SSLCertFilePath = certsConfig.CertPath
}
} else {
// Error?!
return nil, define.Err(0, "http ssl key file or certificate is empty", config.Name)
}
_debug().Str("ssl_cert_file_path", config.SSLCertFilePath).Msg("ssl certificate")
_debug().Str("ssl_key_file_path", config.SSLKeyFilePath).Msg("ssl key")
if config.SSLCertFilePath == "" && config.SSLKeyFilePath == "" {
// If still empty... then let's throw an error!
_err := define.Err(0, "both certificate and key are empty, server will not start", config.Name)
_error().Err(_err).Msg("")
return nil, _err
}
}
// Set the default values for the config... that's in case something is missed
loggerDefaultConfig, _err := loggerConfig.DefaultConfig(&config.Logger)
if _err != nil {
_error().Msg(color.Style{color.LightRed}.Render("failed to set default config for logger"))
return nil, define.Err(0, "failed to set default config for logger", _err.Error())
}
info().Msg("creating server instance")
s := &Server{
Name: config.Name,
Description: config.Description,
connectionID: _uint64.New(),
startTime: _time.New(),
stopTime: _time.New(),
isStopped: _bool.New(),
isStopCalled: _bool.New(),
isStartCalled: _bool.New(),
isStarted: _bool.New(),
LoggerDirPath: loggerDirPath,
Logger: logger.New(loggerDefaultConfig),
//
enableSSL: conv.ParseBool(config.EnableSSL),
sslCertPath: config.SSLCertFilePath,
sslKeyPath: config.SSLKeyFilePath,
//
enableUnsecure: conv.ParseBool(config.EnableUnsecure),
//
ListeningAddresses: config.ListeningAddresses,
ListeningAddressesSSL: config.ListeningAddressesSSL,
// Events/Callbacks - they are common for all routes!
onRequest: _map_string_interface.New(),
onResponse: _map_string_interface.New(),
// Stop
//onStop map[string]OnStop
onStop: _map_string_interface.New(),
onBeforeStop: _map_string_interface.New(),
onStopped: _map_string_interface.New(),
// Start
onStart: _map_string_interface.New(),
onBeforeStart: _map_string_interface.New(),
onStarted: _map_string_interface.New(),
HttpServer: nil,
enableServerStatus: _bool.New(),
}
infoServer := func() *zerolog.Event {
return s.LInfoF("New HTTP Server")
}
infoServer().Msg("setting context")
s.SetContext(ctx)
// By Default we set server as Stopped!
s.isStopped.Set(true)
infoServer().Msg("setting http server to release mode")
// Set in Release mode, in this way the debugging messages are off!
// We should not control here the Mode of the GIN,
// We should not even use gin debugging, use a middleware for better logging!
gin.SetMode(gin.ReleaseMode)
//gin.SetMode(gin.DebugMode)
infoServer().Msg("creating new gin server")
// TODO: use HTTP/2?
// Create the HTTP SERVER
s.HttpServer = gin.New()
infoServer().Msg("enabling auto recovery")
// Set Auto Recovery!
s.HttpServer.Use(gin.Recovery())
infoServer().Msg("setting default middleware for connections")
// We set as default middle related to connections
// TODO: maybe we should also log into a folder the arriving connections!
s.HttpServer.Use(connection.Middleware(s.Logger))
// Latency in processing
s.HttpServer.Use(request_timing.GetMiddleware(s.Logger))
// Set ping listener
ping.Ping(s.HttpServer)
if conv.ParseBool(config.EnableServerStatus) {
infoServer().Msg("enabling server status")
s.SetStatusCredentials(config.ServerStatusUsername, config.ServerStatusPassword)
s.EnableServerStatus()
}
infoServer().Msg("leaving http constructor")
return s, nil
}