-
Notifications
You must be signed in to change notification settings - Fork 0
/
dispatcher.go
60 lines (47 loc) · 1.71 KB
/
dispatcher.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
package carbonate
import (
"net/http"
"reflect"
"bitbucket.org/carbonate/carbonate/router"
)
// Dispatch the request to the designated module and method.
// This function is invoked for each request
func DispatchRequestViaURL(url string, c Controller, w *http.ResponseWriter, r **http.Request) {
// Register the request and response object
c.registerRequestAndResponse(w, r)
// Get the string response for the current reqest
response := dispatchRequestAndGetResponse(c, url)
// Write the response to the default response channel
c.respond(response)
}
// Dispatch a request to it's controller and fetch the response
func dispatchRequestAndGetResponse(c Controller, url string) string {
// Get the request parameters, the method name and the arguments
_, function, args := router.GetParamsFromURL(url)
// Get the controller and it's requested method
controller := reflect.ValueOf(c)
method := controller.MethodByName(function)
if !method.IsValid() {
// HTTP 404 if the method isn't found
c.sendHttpResponseCode(http.StatusNotFound)
return "This page cannot be found"
}
mType := method.Type()
if mType.NumOut() < 0 || mType.Out(0).String() != "string" {
// HTTP 405 if the method called isn't allowed
c.sendHttpResponseCode(http.StatusMethodNotAllowed)
return "This page is not accessible"
}
if !method.Type().IsVariadic() && method.Type().NumIn() != len(args) {
// HTTP 400 if URL is incomplete
c.sendHttpResponseCode(http.StatusBadRequest)
return "Illegal request"
}
in := make([]reflect.Value, len(args))
for k, v := range args {
in[k] = reflect.ValueOf(v)
}
// Call the method with the arguments and return the response as string
response := method.Call(in)[0]
return response.String()
}