-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathroute.go
133 lines (115 loc) · 3.3 KB
/
route.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
package main
import (
"DemaeDominos/dominos"
"github.com/getsentry/sentry-go"
"net/http"
"strings"
)
type Route struct {
Actions []Action
}
// Action contains information about how a specified action should be handled.
type Action struct {
ActionName string
Callback func(*Response)
XMLType XMLType
ServiceType string
}
func NewRoute() Route {
return Route{}
}
// RoutingGroup defines a group of actions for a given service type.
type RoutingGroup struct {
Route *Route
ServiceType string
}
// HandleGroup returns a routing group type for the given service type.
func (r *Route) HandleGroup(serviceType string) RoutingGroup {
return RoutingGroup{
Route: r,
ServiceType: serviceType,
}
}
func (r *RoutingGroup) NormalResponse(action string, function func(*Response)) {
r.Route.Actions = append(r.Route.Actions, Action{
ActionName: action,
Callback: function,
XMLType: Normal,
ServiceType: r.ServiceType,
})
}
func (r *RoutingGroup) MultipleRootNodes(action string, function func(*Response)) {
r.Route.Actions = append(r.Route.Actions, Action{
ActionName: action,
Callback: function,
XMLType: MultipleRootNodes,
ServiceType: r.ServiceType,
})
}
func (r *Route) Handle() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
// First check if it is an image route.
if strings.Contains(req.URL.Path, "itemimg") {
splitUrl := strings.Split(req.URL.Path, "/")
imageName := splitUrl[len(splitUrl)-1]
dom, err := dominos.NewDominos(req)
if err != nil {
// Most likely the user is not registered.
printError(w, err.Error(), http.StatusUnauthorized)
sentry.CaptureException(err)
return
}
img := dom.DownloadAndReturnImage(imageName)
w.Write(img)
return
} else if strings.Contains(req.URL.Path, "logoimg2") {
// Serve Domino's logo
w.Write(dominos.DominosLogo)
return
}
// If this is a POST request it is either an actual request or an error.
var actionName string
var serviceType string
var userAgent string
if req.Method == "POST" {
req.ParseForm()
actionName = req.PostForm.Get("action")
userAgent = req.PostForm.Get("platform")
serviceType = "nwapi.php"
} else {
actionName = req.URL.Query().Get("action")
userAgent = req.URL.Query().Get("platform")
serviceType = strings.Replace(req.URL.Path, "/", "", -1)
}
if userAgent != "wii" {
printError(w, "Invalid request.", http.StatusBadRequest)
return
}
// Ensure we can route to this action before processing.
// Search all registered actions and find a matching action.
var action Action
for _, routeAction := range r.Actions {
if routeAction.ActionName == actionName && routeAction.ServiceType == serviceType {
action = routeAction
}
}
// Action is only properly populated if we found it previously.
if action.ActionName == "" && action.ServiceType == "" {
printError(w, "Unknown action was passed.", http.StatusBadRequest)
return
}
resp := NewResponse(req, &w, action.XMLType)
if resp == nil {
// Already wrote the error in the caller.
return
}
action.Callback(resp)
contents, err := resp.toXML()
if err != nil {
printError(w, err.Error(), http.StatusInternalServerError)
sentry.CaptureException(err)
return
}
w.Write([]byte(contents))
})
}