@@ -15,6 +15,7 @@ import (
15
15
"runtime"
16
16
"strconv"
17
17
"time"
18
+ "code.google.com/p/go.net/websocket"
18
19
)
19
20
20
21
// ServerConfig is configuration for server objects.
@@ -56,10 +57,11 @@ func (s *Server) initServer() {
56
57
}
57
58
58
59
type route struct {
59
- r string
60
- cr * regexp.Regexp
61
- method string
62
- handler reflect.Value
60
+ r string
61
+ cr * regexp.Regexp
62
+ method string
63
+ handler reflect.Value
64
+ httpHandler http.Handler
63
65
}
64
66
65
67
func (s * Server ) addRoute (r string , method string , handler interface {}) {
@@ -69,11 +71,15 @@ func (s *Server) addRoute(r string, method string, handler interface{}) {
69
71
return
70
72
}
71
73
72
- if fv , ok := handler .(reflect.Value ); ok {
73
- s .routes = append (s .routes , route {r , cr , method , fv })
74
- } else {
75
- fv := reflect .ValueOf (handler )
76
- s .routes = append (s .routes , route {r , cr , method , fv })
74
+ switch handler .(type ) {
75
+ case http.Handler :
76
+ s .routes = append (s .routes , route {r : r , cr : cr , method : method , httpHandler : handler .(http.Handler )})
77
+ case reflect.Value :
78
+ fv := handler .(reflect.Value )
79
+ s .routes = append (s .routes , route {r : r , cr : cr , method : method , handler : fv })
80
+ default :
81
+ fv := reflect .ValueOf (handler )
82
+ s .routes = append (s .routes , route {r : r , cr : cr , method : method , handler : fv })
77
83
}
78
84
}
79
85
@@ -84,7 +90,10 @@ func (s *Server) ServeHTTP(c http.ResponseWriter, req *http.Request) {
84
90
85
91
// Process invokes the routing system for server s
86
92
func (s * Server ) Process (c http.ResponseWriter , req * http.Request ) {
87
- s .routeHandler (req , c )
93
+ route := s .routeHandler (req , c )
94
+ if route != nil {
95
+ route .httpHandler .ServeHTTP (c , req )
96
+ }
88
97
}
89
98
90
99
// Get adds a handler for the 'GET' http method for server s.
@@ -112,6 +121,16 @@ func (s *Server) Match(method string, route string, handler interface{}) {
112
121
s .addRoute (route , method , handler )
113
122
}
114
123
124
+ //Adds a custom handler. Only for webserver mode. Will have no effect when running as FCGI or SCGI.
125
+ func (s * Server ) Handler (route string , method string , httpHandler http.Handler ) {
126
+ s .addRoute (route , method , httpHandler )
127
+ }
128
+
129
+ //Adds a handler for websockets. Only for webserver mode. Will have no effect when running as FCGI or SCGI.
130
+ func (s * Server ) Websocket (route string , httpHandler websocket.Handler ) {
131
+ s .addRoute (route , "GET" , httpHandler )
132
+ }
133
+
115
134
// Run starts the web application and serves HTTP requests for s
116
135
func (s * Server ) Run (addr string ) {
117
136
s .initServer ()
@@ -244,7 +263,12 @@ func (s *Server) tryServingFile(name string, req *http.Request, w http.ResponseW
244
263
}
245
264
246
265
// the main route handler in web.go
247
- func (s * Server ) routeHandler (req * http.Request , w http.ResponseWriter ) {
266
+ // Tries to handle the given request.
267
+ // Finds the route matching the request, and execute the callback associated
268
+ // with it. In case of custom http handlers, this function returns an "unused"
269
+ // route. The caller is then responsible for calling the httpHandler associated
270
+ // with the returned route.
271
+ func (s * Server ) routeHandler (req * http.Request , w http.ResponseWriter ) (unused * route ) {
248
272
requestPath := req .URL .Path
249
273
ctx := Context {req , map [string ]string {}, s , w }
250
274
@@ -293,6 +317,12 @@ func (s *Server) routeHandler(req *http.Request, w http.ResponseWriter) {
293
317
continue
294
318
}
295
319
320
+ if route .httpHandler != nil {
321
+ unused = & route
322
+ // We can not handle custom http handlers here, give back to the caller.
323
+ return
324
+ }
325
+
296
326
var args []reflect.Value
297
327
handlerType := route .handler .Type ()
298
328
if requiresContext (handlerType ) {
@@ -337,6 +367,7 @@ func (s *Server) routeHandler(req *http.Request, w http.ResponseWriter) {
337
367
}
338
368
}
339
369
ctx .Abort (404 , "Page not found" )
370
+ return
340
371
}
341
372
342
373
// SetLogger sets the logger for server s
0 commit comments