/
server.go
155 lines (135 loc) · 3.46 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
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
package wtk
import (
"crypto/tls"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/http/fcgi"
"os"
"strconv"
"strings"
"sync"
)
type Server struct {
Id int
RunMode string
listener net.Listener
router *wtkRouter
hook *wtkHook
session *wtkSessionManager
}
func (this *Server) init(id int) *Server {
this.Id = id
this.router = &wtkRouter{
server: this,
Routes: []*Route{},
StaticRoutes: make(map[string]*Route),
StaticFileDir: make(map[string]int),
StaticFileType: make(map[string]int),
lock: new(sync.Mutex),
routeCache: make(map[string]*wtkRouteCache),
}
this.hook = &wtkHook{server: this}
this.session = new(wtkSessionManager)
this.session.RegisterStorage(new(wtkDefaultSessionStorage))
return this
}
func (this *Server) AddRoute(pattern string, c HandlerInterface) *Route {
return this.router.AddRoute(pattern, c)
}
func (this *Server) RemoveRoute(pattern string) {
this.router.RemoveRoute(pattern)
}
func (this *Server) SetPrefixPath(prefix string) {
this.router.SetPrefixPath(prefix)
}
func (this *Server) AddHandlerHook(event string, hookFunc HookHandlerFunc) {
this.hook.AddHandlerHook(event, hookFunc)
}
func (this *Server) AddHttpStatusHook(statusCode int, hookFunc HookHandlerFunc) {
this.hook.AddHandlerHook("HttpStatus"+strconv.Itoa(statusCode), hookFunc)
}
func (this *Server) SetHttpStatusPage(statusCode int, pageFile string) {
this.AddHttpStatusHook(statusCode, func(h *HookHandler) {
s, err := ioutil.ReadFile(pageFile)
if err == nil {
h.Context.WriteBytes(s)
}
})
}
func (this *Server) AddStaticFileDir(dirs ...string) {
this.router.AddStaticFileDir(dirs...)
}
func (this *Server) RemoveStaticFileDir(dirs ...string) {
this.router.RemoveStaticFileDir(dirs...)
}
func (this *Server) AddStaticFileType(ext ...string) {
this.router.AddStaticFileType(ext...)
}
func (this *Server) RemoveStaticFileType(ext ...string) {
this.router.RemoveStaticFileType(ext...)
}
func (this *Server) RegisterSessionStorage(storage SessionStorageInterface) {
this.session.RegisterStorage(storage)
}
func (this *Server) Run(mode string, addr string, port int) error {
var tlsConfig *tls.Config
var err error
if mode == "https" {
tlsConfig = &tls.Config{}
if tlsConfig.NextProtos == nil {
tlsConfig.NextProtos = []string{"http/1.1"}
}
tlsConfig.Certificates = make([]tls.Certificate, 1)
tlsConfig.Certificates[0], err = tls.LoadX509KeyPair(SslCertificate, SslCertificateKey)
if err != nil {
return err
}
}
listenUnix := false
if strings.HasPrefix(addr, "unix:") {
listenUnix = true
addr = addr[5:]
}
if listenUnix {
os.Remove(addr)
this.listener, err = net.Listen("unix", addr)
if err == nil {
os.Chmod(addr, os.FileMode(0666))
defer os.Remove(addr)
}
} else {
listenAddr := net.JoinHostPort(addr, fmt.Sprintf("%d", port))
this.listener, err = net.Listen("tcp", listenAddr)
}
if err != nil {
return err
}
// defer this.listener.Close()
this.RunMode = mode
switch mode {
case "http":
http.Serve(this.listener, this.router)
case "fcgi":
fcgi.Serve(this.listener, this.router)
case "https":
http.Serve(tls.NewListener(this.listener, tlsConfig), this.router)
default:
http.Serve(this.listener, this.router)
}
return nil
}
func (this *Server) Close() {
delete(servers, this.Id)
if this.listener != nil {
this.listener.Close()
}
}
func (this *Server) Clone() *Server {
a := NewServer()
a.router = this.router
a.hook = this.hook
a.session = this.session
return a
}