-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathroute.go
More file actions
49 lines (42 loc) · 1 KB
/
route.go
File metadata and controls
49 lines (42 loc) · 1 KB
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
package hime
import "context"
// Routes is the map for route name => path
type Routes map[string]string
func cloneRoutes(xs Routes) Routes {
if xs == nil {
return nil
}
rs := make(Routes)
for k, v := range xs {
rs[k] = v
}
return rs
}
// Routes registers route name and path
func (app *App) Routes(routes Routes) {
if app.routes == nil {
app.routes = make(Routes)
}
for name, path := range routes {
app.routes[name] = path
}
}
// Route gets route path from given name
func (app *App) Route(name string, params ...any) string {
if app.routes == nil {
panic(newErrRouteNotFound(name))
}
path, ok := app.routes[name]
if !ok {
panic(newErrRouteNotFound(name))
}
return buildPath(path, params...)
}
// Route gets route path from name
func (ctx *Context) Route(name string, params ...any) string {
return ctx.app.Route(name, params...)
}
// Route returns route value from context
func Route(ctx context.Context, name string, params ...any) string {
return getApp(ctx).Route(name, params...)
}