-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathroothandler.go
123 lines (110 loc) · 2.01 KB
/
roothandler.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
package essen
import (
"net/http"
"github.com/akshitgrover/essen/jobqueue"
)
func rootHandler(res http.ResponseWriter, req *http.Request) {
done := make(chan bool)
// Handler For Static Methods
staticPath := matchStaticUrl(req.URL.Path)
vStatic, ok := paths.static[staticPath]
if ok {
jobqueue.QueuePush(func() {
http.StripPrefix(staticPath+"/", vStatic).ServeHTTP(res, req)
done <- true
jobqueue.QueueNext()
})
<-done
return
}
//Handler For Use Methods
vUse, useOk := paths.use[req.URL.Path]
if useOk {
jobqueue.QueuePush(func() {
vUse(res, req)
done <- true
jobqueue.QueueNext()
})
<-done
return
}
switch req.Method {
//Handler Head Requests
case "HEAD":
v, ok := paths.head[req.URL.Path]
if !ok {
jobqueue.QueuePush(func() {
http.NotFound(res, req)
done <- true
jobqueue.QueueNext()
})
break
}
jobqueue.QueuePush(func() {
v(res, req)
done <- true
jobqueue.QueueNext()
})
break
//Handle Get Requests
case "GET":
v, ok := paths.get[req.URL.Path]
if !ok {
jobqueue.QueuePush(func() {
http.NotFound(res, req)
done <- true
jobqueue.QueueNext()
})
break
}
jobqueue.QueuePush(func() {
v(res, req)
done <- true
jobqueue.QueueNext()
})
break
//Handle Post Requests
case "POST":
v, ok := paths.post[req.URL.Path]
if !ok {
jobqueue.QueuePush(func() {
http.NotFound(res, req)
done <- true
jobqueue.QueueNext()
})
break
}
jobqueue.QueuePush(func() {
v(res, req)
done <- true
jobqueue.QueueNext()
})
break
//Handle Put Requests
case "PUT":
v, ok := paths.put[req.URL.Path]
if !ok {
jobqueue.QueuePush(func() {
http.NotFound(res, req)
done <- true
jobqueue.QueueNext()
})
break
}
jobqueue.QueuePush(func() {
v(res, req)
done <- true
jobqueue.QueueNext()
})
break
//Handle Any Other Request Method
default:
jobqueue.QueuePush(func() {
http.NotFound(res, req)
done <- true
jobqueue.QueueNext()
})
break
}
<-done
}