Skip to content

Commit 81cad02

Browse files
committed
Refactoring patch from hoisie#58.
1 parent 673c0e6 commit 81cad02

File tree

2 files changed

+53
-11
lines changed

2 files changed

+53
-11
lines changed

server.go

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"runtime"
1616
"strconv"
1717
"time"
18+
"code.google.com/p/go.net/websocket"
1819
)
1920

2021
// ServerConfig is configuration for server objects.
@@ -56,10 +57,11 @@ func (s *Server) initServer() {
5657
}
5758

5859
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
6365
}
6466

6567
func (s *Server) addRoute(r string, method string, handler interface{}) {
@@ -69,11 +71,15 @@ func (s *Server) addRoute(r string, method string, handler interface{}) {
6971
return
7072
}
7173

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})
7783
}
7884
}
7985

@@ -84,7 +90,10 @@ func (s *Server) ServeHTTP(c http.ResponseWriter, req *http.Request) {
8490

8591
// Process invokes the routing system for server s
8692
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+
}
8897
}
8998

9099
// 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{}) {
112121
s.addRoute(route, method, handler)
113122
}
114123

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+
115134
// Run starts the web application and serves HTTP requests for s
116135
func (s *Server) Run(addr string) {
117136
s.initServer()
@@ -244,7 +263,12 @@ func (s *Server) tryServingFile(name string, req *http.Request, w http.ResponseW
244263
}
245264

246265
// 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) {
248272
requestPath := req.URL.Path
249273
ctx := Context{req, map[string]string{}, s, w}
250274

@@ -293,6 +317,12 @@ func (s *Server) routeHandler(req *http.Request, w http.ResponseWriter) {
293317
continue
294318
}
295319

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+
296326
var args []reflect.Value
297327
handlerType := route.handler.Type()
298328
if requiresContext(handlerType) {
@@ -337,6 +367,7 @@ func (s *Server) routeHandler(req *http.Request, w http.ResponseWriter) {
337367
}
338368
}
339369
ctx.Abort(404, "Page not found")
370+
return
340371
}
341372

342373
// SetLogger sets the logger for server s

web.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"strconv"
2020
"strings"
2121
"time"
22+
"code.google.com/p/go.net/websocket"
2223
)
2324

2425
// A Context object is created for every incoming HTTP request, and is
@@ -237,6 +238,16 @@ func Match(method string, route string, handler interface{}) {
237238
mainServer.addRoute(route, method, handler)
238239
}
239240

241+
//Adds a custom handler. Only for webserver mode. Will have no effect when running as FCGI or SCGI.
242+
func Handler(route string, method string, httpHandler http.Handler) {
243+
mainServer.Handler(route, method, httpHandler)
244+
}
245+
246+
//Adds a handler for websockets. Only for webserver mode. Will have no effect when running as FCGI or SCGI.
247+
func Websocket(route string, httpHandler websocket.Handler) {
248+
mainServer.Websocket(route, httpHandler)
249+
}
250+
240251
// SetLogger sets the logger for the main server.
241252
func SetLogger(logger *log.Logger) {
242253
mainServer.Logger = logger

0 commit comments

Comments
 (0)