-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
177 lines (158 loc) · 4.09 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package wserver
import (
"errors"
. "github.com/fitmewell/wserver/log"
"github.com/fitmewell/wserver/wsession"
"net/http"
"os"
"os/signal"
"sync"
"syscall"
"time"
)
func New(filePath string) (wServer *Server, err error) {
Debug("loading config file: " + filePath)
config, err := NewConfig(filePath)
if err != nil {
return nil, err
}
return NewServer(config), nil
}
func NewPortServer(port string) (wServer *Server) {
config := &ServerConfig{Port: port}
return NewServer(config)
}
func NewServer(config *ServerConfig) *Server {
server := &Server{
config: config,
sessionManager: wsession.NewDefaultSessionManager(config.Session.CookieName),
aftermaths: map[string]func(){},
}
server.handler = newDefaultHandler(server)
server.context = NewContextFrom(server.config)
return server
}
type Server struct {
config *ServerConfig
handler *wHandler
context ServerContext
lock sync.Mutex
started bool
sessionManager wsession.SessionManager
aftermaths map[string]func()
}
func (ws *Server) Start() {
ws.lock.Lock()
defer func() {
ws.started = false
Debug("end")
ws.lock.Unlock()
}()
if ws.started {
Fatal("Server already started")
}
ws.started = true
ws.context.Init()
ws.handler.init()
Debug("started")
ws.aftermath()
ws.listen()
}
func (ws *Server) listen() {
var err error
config := ws.config
if config.UseSSL {
go func() {
httpServerMux := http.NewServeMux()
httpServerMux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "https:"+r.Host+":443"+r.RequestURI, http.StatusMovedPermanently)
})
s := &http.Server{Addr: ":" + config.Port, Handler: httpServerMux}
if err := s.ListenAndServe(); err != nil {
FatalF("ListenAndServe error: %v", err)
}
}()
s := &http.Server{Addr: ":" + config.SSLConfig.SSLPort, Handler: ws.handler}
err = s.ListenAndServeTLS(config.SSLConfig.CertFile, config.SSLConfig.KeyFile)
} else {
s := &http.Server{Addr: ":" + config.Port, Handler: ws.handler}
err = s.ListenAndServe()
}
if err != nil {
Debug(err)
}
}
func (ws *Server) aftermath() {
s := make(chan os.Signal, 2)
signal.Notify(s)
go func() {
for {
cs := <-s
Debug("caught system signal:" + cs.String())
switch cs {
case os.Interrupt:
fallthrough
case syscall.SIGTERM:
fallthrough
case os.Kill:
Debug("closing")
ws.started = false
go func() {
time.Sleep(5 * time.Second)
Debug("end")
ws.lock.Unlock()
os.Exit(1)
}()
c := make(chan string, len(ws.aftermaths))
for k, v := range ws.aftermaths {
go func(name string, method func()) {
Debug("[aftermath][" + name + "]start")
method()
c <- name
}(k, v)
}
amount := 0
for {
k := <-c
Debug("[aftermath][" + k + "]end")
amount++
if amount == len(ws.aftermaths) {
Debug("end")
ws.lock.Unlock()
os.Exit(1)
}
}
}
}
}()
}
//'method' support method , use * to support all method
//'path' path
//'e' handler method , the server will auto handle the return value , the method parameter support *http.Request ,http.ResponseWriter, custom struct server context
func (ws *Server) AddHandler(method string, path string, e interface{}) *Server {
ws.handler.addHandler(method, path, e)
DebugF("Listening:[%s]\t[%s]\t{%v}", path, method, e)
return ws
}
func (ws *Server) AddStaticSource(path, fileLocate string) *Server {
ws.config.StaticResources = append(ws.config.StaticResources, StaticResource{Path: path, FileLocate: fileLocate})
return ws
}
func (ws *Server) AddTemplate(name, dir string) *Server {
ws.config.Templates = append(ws.config.Templates, Template{Name: name, Dir: dir})
return ws
}
func (ws *Server) AddAspectHandler(handler AspectHandler) *Server {
ws.handler.addAspect(handler)
return ws
}
func (ws *Server) AddAftermath(name string, method func()) error {
if _, ok := ws.aftermaths[name]; ok {
return errors.New("duplicate aftermatch found")
}
ws.aftermaths[name] = method
return nil
}
func (ws *Server) GetProperties(i string) string {
return ws.context.GetProperty(i)
}