forked from hoisie/web
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
295 lines (256 loc) · 7.38 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package web
import (
"bytes"
"golang.org/x/net/websocket"
"crypto/tls"
"fmt"
"github.com/gorilla/mux"
"log"
"net"
"net/http"
"net/http/pprof"
"os"
"path"
"reflect"
"regexp"
"runtime"
"strings"
"time"
)
// ServerConfig is configuration for server objects.
type ServerConfig struct {
StaticDir string
Addr string
Port int
CookieSecret string
RecoverPanic bool
Profiler bool
}
// Server represents a web.go server.
type Server struct {
Config *ServerConfig
routes []route
muxrouter *mux.Router
Logger *log.Logger
Env map[string]interface{}
//save the listener so it can be closed
l net.Listener
}
func NewServer() *Server {
return &Server{
Config: Config,
muxrouter: mux.NewRouter(),
Logger: log.New(os.Stdout, "", log.Ldate|log.Ltime),
Env: map[string]interface{}{},
}
}
func (s *Server) initServer() {
if s.Config == nil {
s.Config = &ServerConfig{}
}
if s.Logger == nil {
s.Logger = log.New(os.Stdout, "", log.Ldate|log.Ltime)
}
}
func (s *Server) Router() *mux.Router {
if s.muxrouter == nil {
s.muxrouter = mux.NewRouter()
}
return s.muxrouter
}
type route struct {
r string
cr *regexp.Regexp
method string
handler reflect.Value
httpHandler http.Handler
}
func (s *Server) addRoute(r string, method string, handler interface{}) {
if s.muxrouter == nil {
s.muxrouter = mux.NewRouter()
}
switch f := handler.(type) {
case func(http.ResponseWriter, *http.Request):
s.muxrouter.NewRoute().Path(r).HandlerFunc(f).Methods(method)
default:
log.Println("ERROR: ", reflect.TypeOf(handler))
panic("incorrect handler signature: ")
}
}
/*
func webio_handler(s *Server, f1 func(*Context, string)) func(http.ResponseWriter, *http.Request) {
f2 := func(w http.ResponseWriter, r *http.Request) {
s.ServeHTTP(w, r)
}
return f2
}
*/
// ServeHTTP is the interface method for Go's http server package
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.muxrouter.ServeHTTP(w, r)
}
// Get adds a handler for the 'GET' http method for server s.
func (s *Server) Get(route string, handler interface{}) {
s.addRoute(route, "GET", handler)
}
// Post adds a handler for the 'POST' http method for server s.
func (s *Server) Post(route string, handler interface{}) {
s.addRoute(route, "POST", handler)
}
// Put adds a handler for the 'PUT' http method for server s.
func (s *Server) Put(route string, handler interface{}) {
s.addRoute(route, "PUT", handler)
}
// Delete adds a handler for the 'DELETE' http method for server s.
func (s *Server) Delete(route string, handler interface{}) {
s.addRoute(route, "DELETE", handler)
}
// Match adds a handler for an arbitrary http method for server s.
func (s *Server) Match(method string, route string, handler interface{}) {
s.addRoute(route, method, handler)
}
//Adds a custom handler. Only for webserver mode. Will have no effect when running as FCGI or SCGI.
func (s *Server) Handler(route string, method string, httpHandler http.Handler) {
s.addRoute(route, method, httpHandler)
}
//Adds a handler for websockets. Only for webserver mode. Will have no effect when running as FCGI or SCGI.
func (s *Server) Websocket(route string, httpHandler websocket.Handler) {
s.addRoute(route, "GET", httpHandler)
}
// Run starts the web application and serves HTTP requests for s
func (s *Server) Run(addr string) {
s.initServer()
if s.muxrouter == nil {
s.muxrouter = mux.NewRouter()
}
if s.Config.Profiler {
s.muxrouter.Handle("/debug/pprof", http.HandlerFunc(pprof.Index))
s.muxrouter.Handle("/debug/pprof/cmdline", http.HandlerFunc(pprof.Cmdline))
s.muxrouter.Handle("/debug/pprof/profile", http.HandlerFunc(pprof.Profile))
s.muxrouter.Handle("/debug/pprof/symbol", http.HandlerFunc(pprof.Symbol))
s.muxrouter.Handle("/debug/pprof/heap", pprof.Handler("heap"))
s.muxrouter.Handle("/debug/pprof/goroutine", pprof.Handler("goroutine"))
s.muxrouter.Handle("/debug/pprof/threadcreate", pprof.Handler("threadcreate"))
s.muxrouter.Handle("/debug/pprof/block", pprof.Handler("block"))
}
s.muxrouter.Handle("/", s)
s.Logger.Printf("serving %s\n", addr)
l, err := net.Listen("tcp", addr)
if err != nil {
log.Fatal("ListenAndServe:", err)
}
s.l = l
err = http.Serve(s.l, s.muxrouter)
s.l.Close()
}
func (s *Server) Shutdown() {
s.l.Close()
//TODO: Existing connections as well must be closed gracefully
}
// RunFcgi starts the web application and serves FastCGI requests for s.
func (s *Server) RunFcgi(addr string) {
s.initServer()
s.Logger.Printf("serving fcgi %s\n", addr)
s.listenAndServeFcgi(addr)
}
// RunScgi starts the web application and serves SCGI requests for s.
func (s *Server) RunScgi(addr string) {
s.initServer()
s.Logger.Printf("serving scgi %s\n", addr)
s.listenAndServeScgi(addr)
}
// RunTLS starts the web application and serves HTTPS requests for s.
func (s *Server) RunTLS(addr string, config *tls.Config) error {
s.initServer()
mux := http.NewServeMux()
mux.Handle("/", s)
s.Logger.Printf("serving %s\n", addr)
l, err := tls.Listen("tcp", addr, config)
if err != nil {
log.Fatal("Listen:", err)
return err
}
s.l = l
return http.Serve(s.l, mux)
}
// Close stops server s.
func (s *Server) Close() {
if s.l != nil {
s.l.Close()
}
}
// safelyCall invokes `function` in recover block
func (s *Server) safelyCall(function reflect.Value, args []reflect.Value) (resp []reflect.Value, e interface{}) {
defer func() {
if err := recover(); err != nil {
if !s.Config.RecoverPanic {
// go back to panic
panic(err)
} else {
e = err
resp = nil
s.Logger.Println("Handler crashed with error", err)
for i := 1; ; i += 1 {
_, file, line, ok := runtime.Caller(i)
if !ok {
break
}
s.Logger.Println(file, line)
}
}
}
}()
return function.Call(args), nil
}
// tryServingFile attempts to serve a static file, and returns
// whether or not the operation is successful.
// It checks the following directories for the file, in order:
// 1) Config.StaticDir
// 2) The 'static' directory in the parent directory of the executable.
// 3) The 'static' directory in the current working directory
func (s *Server) tryServingFile(name string, req *http.Request, w http.ResponseWriter) bool {
//try to serve a static file
if s.Config.StaticDir != "" {
staticFile := path.Join(s.Config.StaticDir, name)
if fileExists(staticFile) {
http.ServeFile(w, req, staticFile)
return true
}
} else {
for _, staticDir := range defaultStaticDirs {
staticFile := path.Join(staticDir, name)
if fileExists(staticFile) {
http.ServeFile(w, req, staticFile)
return true
}
}
}
return false
}
func (s *Server) logRequest(r *http.Request, sTime time.Time) {
//log the request
var logEntry bytes.Buffer
req := r
requestPath := req.URL.Path
duration := time.Now().Sub(sTime)
var client string
// We suppose RemoteAddr is of the form Ip:Port as specified in the Request
// documentation at http://golang.org/pkg/net/http/#Request
pos := strings.LastIndex(req.RemoteAddr, ":")
if pos > 0 {
client = req.RemoteAddr[0:pos]
} else {
client = req.RemoteAddr
}
fmt.Fprintf(&logEntry, "%s - %s %s - %v", client, req.Method, requestPath, duration)
/*
if len(ctx.Params) > 0 {
fmt.Fprintf(&logEntry, " - Params: %v\n", ctx.Params)
}
*/
s.Logger.Print(logEntry.String())
}
// SetLogger sets the logger for server s
func (s *Server) SetLogger(logger *log.Logger) {
s.Logger = logger
}