-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
73 lines (66 loc) · 1.91 KB
/
app.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
package gosocket
import (
"crypto/tls"
)
var (
TcpApp *App
)
func init() {
TcpApp = NewApp()
}
// AppConfig Server configuration
// 服务器配置
type AppConfig struct {
TcpAddr string //The address which the app is listening to 地址
TcpPort int //The port which the app is listening to 端口号
TlsEnable bool //Whether to enable tls 是否开启TLS
TlsCert string //The certification used by tls TLS证书
TlsKey string //The key used by tls TLS密钥
}
// App is the entry class to start the server
type App struct {
Server *Server //Responsible for listening and serving requests
Config *AppConfig //Same as appConfig in the Run function
Log ILogger //Same as log in the Run function
FastLog IFastLogger //Same as fastLog in the Run function
}
func NewApp() *App {
app := &App{}
return app
}
// Run start the app server
// 启动服务器
// The appConfig is used to configure the server
// The log is used to log any errors happens during the process
// The fastLog is used to log any requests received by the server
// The requests can be very frequent therefore using the fast logger will make sure the performance is high
func (app *App) Run(appConfig *AppConfig, log ILogger, fastLog IFastLogger) {
defer func() {
if e := recover(); e != nil {
TcpApp.Log.Error(e)
}
}()
if log == nil || fastLog == nil {
panic("log or fastLog can't be nil")
}
app.Config = appConfig
app.Log = log
app.FastLog = fastLog
//Initialize graceful restart
InitGracefulRestart()
//创建一个server
app.Server = NewServer(app.Config.TcpAddr)
//Whether to enable tls
if app.Config.TlsEnable {
//tls certificate
config := &tls.Config{}
certificate, err := tls.LoadX509KeyPair(app.Config.TlsCert, app.Config.TlsKey)
if err != nil {
panic(err)
}
config.Certificates = []tls.Certificate{certificate}
app.Server.ListenAndServe(config)
} else {
app.Server.ListenAndServe(nil)
}
}