-
Notifications
You must be signed in to change notification settings - Fork 19
/
server.go
50 lines (45 loc) · 1.2 KB
/
server.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
package http
import (
"github.com/baetyl/baetyl-go/log"
"github.com/valyala/fasthttp"
)
type Server struct {
conf ServerConfig
*fasthttp.Server
}
// NewServer new server
func NewServer(cfg ServerConfig, handler fasthttp.RequestHandler) *Server {
return &Server{
conf: cfg,
Server: &fasthttp.Server{
Handler: handler,
Concurrency: cfg.Concurrency,
DisableKeepalive: cfg.DisableKeepalive,
TCPKeepalive: cfg.TCPKeepalive,
MaxRequestBodySize: cfg.MaxRequestBodySize,
ReadTimeout: cfg.ReadTimeout,
WriteTimeout: cfg.WriteTimeout,
IdleTimeout: cfg.IdleTimeout,
},
}
}
func (s *Server) Start() {
go func() {
logger := log.With(log.Any("http", "server"))
logger.Info("server is running", log.Any("address", s.conf.Address))
if s.conf.Cert != "" || s.conf.Key != "" {
if err := s.ListenAndServeTLS(s.conf.Address, s.conf.Cert, s.conf.Key); err != nil {
logger.Error("https server shutdown", log.Error(err))
}
} else {
if err := s.ListenAndServe(s.conf.Address); err != nil {
logger.Error("http server shutdown", log.Error(err))
}
}
}()
}
func (s *Server) Close() {
if s.Server != nil {
s.Server.Shutdown()
}
}