-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrouter.go
197 lines (182 loc) · 7.88 KB
/
router.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
package main
import (
"github.com/valyala/fasthttp"
"log"
)
var _ = log.Printf
func router(ctx *fasthttp.RequestCtx) {
method, uri := ctx.Method(), ctx.Path()
// We will set Connection header in fasthttp code
//ctx.Response.Header.Set("Connection", "keep-alive")
lu := len(uri)
// We should check for '/' request, but skip for now
//if lu < 2 {
// ctx.SetStatusCode(fasthttp.StatusNotFound)
// return
//}
method_char, uri_char := method[0], uri[1]
switch method_char {
case 71: // = ord('G') = GET
switch uri_char {
case 108: // = ord('l') = /locations
last_char := uri[lu-1]
switch last_char {
case 103: // = ord('g') => /locations/:id/avg
id, err := Atoi(uri[11:lu-4])
if err == nil {
//log.Printf("%s %q %s %d", method, uri, "/locations/:id/avg", id)
l := getLocation(id)
if l != nil {
locationAvgHandler(ctx, l)
} else {
ctx.SetStatusCode(fasthttp.StatusNotFound)
}
} else {
ctx.SetStatusCode(fasthttp.StatusBadRequest)
}
default:
id, err := Atoi(uri[11:lu])
if err == nil {
//log.Printf("%s %q %s %d", method, uri, "/locations/:id", id)
l := getLocation(id)
if l != nil {
ctx.Write([]byte("{\"id\":"))
WriteInt(ctx, id)
ctx.Write([]byte(",\"place\":\""))
ctx.Write(l.Place)
ctx.Write([]byte("\",\"country\":\""))
ctx.Write(l.Country)
ctx.Write([]byte("\",\"city\":\""))
ctx.Write(l.City)
ctx.Write([]byte("\",\"distance\":"))
WriteInt(ctx, l.Distance)
ctx.Write([]byte("}"))
} else {
ctx.SetStatusCode(fasthttp.StatusNotFound)
}
} else {
ctx.SetStatusCode(fasthttp.StatusBadRequest)
}
}
case 117: // = ord('u') = /users
last_char := uri[lu-1]
switch last_char {
case 115: // ord('s') = /users/:id/visits
id, err := Atoi(uri[7:lu-7])
if err == nil {
//log.Printf("%s %q %s %d", method, uri, "/users/:id/visits", id)
u := getUser(id)
if u != nil {
usersVisitsHandler(ctx, u)
} else {
ctx.SetStatusCode(fasthttp.StatusNotFound)
}
} else {
ctx.SetStatusCode(fasthttp.StatusBadRequest)
}
default: // GET /users/:id
id, err := Atoi(uri[7:lu])
if err == nil {
//log.Printf("%s %q %s %d", method, uri, "/users/:id", id)
u := getUser(id)
if u != nil {
ctx.Write([]byte("{\"id\":"))
WriteInt(ctx, id)
ctx.Write([]byte(",\"email\":\""))
ctx.Write(u.Email)
ctx.Write([]byte("\",\"first_name\":\""))
ctx.Write(u.First_name)
ctx.Write([]byte("\",\"last_name\":\""))
ctx.Write(u.Last_name)
ctx.Write([]byte("\",\"gender\":\""))
ctx.Write([]byte{uint8(u.Gender)})
ctx.Write([]byte("\",\"birth_date\":"))
WriteInt(ctx, u.Birth_date)
ctx.Write([]byte("}"))
} else {
ctx.SetStatusCode(fasthttp.StatusNotFound)
}
} else {
ctx.SetStatusCode(fasthttp.StatusNotFound) // holywar fix instead of 400
}
}
case 118: // = ord('v') = /visits
id, err := Atoi(uri[8:lu])
if err == nil {
//log.Printf("%s %q %s %d", method, uri, "/visits/:id", id)
v := getVisit(id)
if v != nil {
ctx.Write([]byte("{\"id\":"))
WriteInt(ctx, id)
ctx.Write([]byte(",\"location\":"))
WriteInt(ctx, v.Location)
ctx.Write([]byte(",\"user\":"))
WriteInt(ctx, v.User)
ctx.Write([]byte(",\"mark\":"))
ctx.Write([]byte{uint8(v.Mark) + '0'})
ctx.Write([]byte(",\"visited_at\":"))
WriteInt(ctx, v.Visited_at)
ctx.Write([]byte("}"))
} else {
ctx.SetStatusCode(fasthttp.StatusNotFound)
}
} else {
ctx.SetStatusCode(fasthttp.StatusBadRequest)
}
default:
ctx.SetStatusCode(fasthttp.StatusBadRequest)
}
case 80: // = ord('P') = POST
switch uri_char {
case 108: // = ord('l') = /locations
last_char := uri[lu-1]
switch last_char {
case 119: // = ord('w') => /locations/new
//log.Printf("%s %q %s", method, uri, "/locations/new")
locationInsertHandler(ctx)
default:
id, err := Atoi(uri[11:lu])
if err == nil {
//log.Printf("%s %q %s %d", method, uri, "/locations/:id", id)
locationUpdateHandler(ctx, id)
} else {
ctx.SetStatusCode(fasthttp.StatusBadRequest)
}
}
case 117: // = ord('u') = /users
last_char := uri[lu-1]
switch last_char {
case 119: // = ord('w') => /users/new
//log.Printf("%s %q %s", method, uri, "/users/new")
userInsertHandler(ctx)
default:
id, err := Atoi(uri[7:lu])
if err == nil {
//log.Printf("%s %q %s %d", method, uri, "/users/:id", id)
userUpdateHandler(ctx, id)
} else {
ctx.SetStatusCode(fasthttp.StatusBadRequest)
}
}
case 118: // = ord('v') = /visits
last_char := uri[lu-1]
switch last_char {
case 119: // = ord('w') => /visits/new
//log.Printf("%s %q %s", method, uri, "/visits/new")
visitInsertHandler(ctx)
default:
id, err := Atoi(uri[8:lu])
if err == nil {
//log.Printf("%s %q %s %d", method, uri, "/visits/:id", id)
visitUpdateHandler(ctx, id)
} else {
ctx.SetStatusCode(fasthttp.StatusBadRequest)
}
}
default:
ctx.SetStatusCode(fasthttp.StatusNotFound)
}
default:
ctx.SetStatusCode(fasthttp.StatusBadRequest)
}
}