-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.go
66 lines (54 loc) · 1.58 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package pine
import (
"os"
"os/signal"
"strings"
"syscall"
"github.com/gookit/color"
"github.com/valyala/fasthttp"
)
type ServerHandler func(*Application) error
func (a *Application) setupInfo(addr string) {
if pos := strings.Index(addr, ":"); pos > 0 {
a.hostname = addr[:pos]
}
scheme := "http"
if len(a.configuration.tlsKeyFile) > 0 && len(a.configuration.tlsSecretFile) > 0 {
scheme += "s"
}
if !a.configuration.withoutStartupLog {
a.DumpRouteTable()
color.Green.Println(logo)
color.Red.Printf("pine server now listening on: %s://%s\n", scheme, addr)
}
}
func Addr(addr string) ServerHandler {
return func(a *Application) error {
s := &fasthttp.Server{
Name: a.configuration.serverName,
Handler: dispatchRequest(a),
ErrorHandler: func(ctx *fasthttp.RequestCtx, err error) {
Logger().Error("server error: %s", err)
},
}
a.setupInfo(addr)
if a.configuration.maxMultipartMemory > 0 {
s.MaxRequestBodySize = int(a.configuration.maxMultipartMemory)
}
if a.configuration.compressGzip {
s.Handler = fasthttp.CompressHandler(s.Handler)
}
if conf := a.configuration.timeout; conf.Enable {
s.Handler = fasthttp.TimeoutHandler(s.Handler, conf.Duration, conf.Msg)
}
if a.configuration.gracefulShutdown {
a.quitCh = make(chan os.Signal)
signal.Notify(a.quitCh, os.Interrupt, syscall.SIGTERM)
go a.gracefulShutdown(s, a.quitCh)
}
return s.ListenAndServe(addr)
}
}