This repository has been archived by the owner on May 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
154 lines (138 loc) · 3.9 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
package router
import (
"net/url"
"strconv"
"strings"
"github.com/Nigel2392/jsext-framework/router/routes"
"github.com/Nigel2392/jsext-framework/router/rterr"
"github.com/Nigel2392/jsext-framework/router/vars"
)
// Router is the main router struct.
type Router struct {
routes []*routes.Route
skipTrailingSlash bool
nameToTitle bool
onErr func(err error)
onLoad func()
onPageChange func(vars.Vars, *url.URL)
afterPageChange func(vars.Vars, *url.URL)
middlewares []func(vars.Vars, *url.URL, *routes.Route, rterr.ErrorThrower) bool
}
// Initialize a new router.
func NewRouter() *Router {
return &Router{routes: make([]*routes.Route, 0)}
}
// SkipTrailingSlash will skip the trailing slash in the path.
func (r *Router) SkipTrailingSlash() {
r.skipTrailingSlash = true
}
// Add a middleware to the router.
func (r *Router) Use(middleware func(vars.Vars, *url.URL, *routes.Route, rterr.ErrorThrower) bool) {
r.middlewares = append(r.middlewares, middleware)
}
// Decide what to do on errors.
func (r *Router) OnError(cb func(err error)) {
r.onErr = cb
}
// Throw an error in the router with a message.
func (r *Router) Error(code int, msg string) rterr.RouterError {
var err = rterr.NewError(code, msg)
if r.onErr == nil {
panic(err)
}
r.onErr(error(err))
return err
}
// Throw an error in the router with predefined error code messages.
func (r *Router) Throw(code int) {
var err = rterr.NewError(code)
if r.onErr == nil {
panic(err)
} else {
r.onErr(error(err))
}
}
// Display nicely formatted URLs
func (r *Router) String() string {
var sb = &strings.Builder{}
sb.WriteString("Errorfunc defined: " + strconv.FormatBool(r.onErr != nil) + "\n")
sb.WriteString("Skip trailing slash: " + strconv.FormatBool(r.skipTrailingSlash) + "\n")
var level = 0
for _, route := range r.routes {
route.StringIndent(sb, level)
}
return sb.String()
}
// Match a raw path.
func (r *Router) Match(path string) (*routes.Route, vars.Vars, bool) {
if r.skipTrailingSlash && len(path) > 1 {
path = strings.TrimSuffix(path, "/")
}
for _, route := range r.routes {
if match, rt, vars := route.Match(path); match {
return rt, vars, match
}
}
return &routes.Route{
Internal_name: "404",
Path: path,
}, nil, false
}
// Get a route by name.
// If it does not directly exist, it will search in the subroutes.
func (r *Router) GetRoute(name string) *routes.Route {
for _, route := range r.routes {
if route.Internal_name == name {
return route
}
}
for _, route := range r.routes {
var rt = route.GetRoute(name)
if rt != nil {
return rt
}
}
return nil
}
// Register a new route.
// If the route name already exists, it will panic.
func (r *Router) Register(name, path string, callable func(v vars.Vars, u *url.URL)) *routes.Route {
if r.skipTrailingSlash && len(path) > 1 {
path = strings.TrimSuffix(path, "/")
}
for _, route := range r.routes {
if route.Internal_name == name {
panic("Router [500] route name already exists: " + name)
}
}
var route = &routes.Route{Name: name, Internal_name: name, Path: path, Callable: callable, SkipTrailingSlash: r.skipTrailingSlash}
r.routes = append(r.routes, route)
return route
}
// Handle a path.
func (r *Router) HandlePath(path string) {
var u, err = url.Parse(path)
if err != nil {
r.onErr(err)
return
}
r.Handle(u)
}
// Handle a path in the form of a redirect. NYI.
func (r *Router) Redirect(path string) {
r.HandlePath(path)
}
// Redirect to a route by name.
func (r *Router) RedirectNamed(name string, varMap vars.Vars) {
var route = r.GetRoute(name)
if route == nil {
r.Error(404, "Route not found: "+name)
return
}
if varMap == nil {
varMap = make(vars.Vars)
}
if route.Callable != nil {
go route.Callable(varMap, nil)
}
}