-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
56 lines (44 loc) · 1.13 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
package armony
import (
"net/http"
"net/url"
"strings"
)
type routes map[string]Controller
// Controller : A type representing a controller funcion
type Controller func(*http.ResponseWriter, *http.Request, *Session) (string, interface{})
// Routes : all routes
var Routes routes
var routesInitialized = false
// Handler : Armony router handler
func Handler(w http.ResponseWriter, r *http.Request) {
ss := LoadSession(&w, r)
u, _ := url.Parse(r.RequestURI)
if fn, ok := Routes[u.EscapedPath()]; ok {
res, data := fn(&w, r, &ss)
if res != "" {
//Options
command := strings.Split(res, ":")[0]
param := strings.Split(res, ":")[1]
switch command {
case "template":
err := Templates.ExecuteTemplate(w, param, data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
}
}
}
// AddRoute : Adds a new route
func AddRoute(path string, controller Controller) {
if !routesInitialized {
Routes = make(routes)
routesInitialized = true
}
Routes[path] = controller
}
// RemoveRoute : Removes a new route
func RemoveRoute(path string, controller Controller) {
delete(Routes, path)
}