This repository has been archived by the owner on Jun 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
route.go
80 lines (69 loc) · 1.9 KB
/
route.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
// Goro
//
// Created by Yakka
// http://theyakka.com
//
// Copyright (c) 2019 Yakka LLC.
// All rights reserved.
// See the LICENSE file for licensing details and requirements.
package goro
import (
"strings"
)
const (
// RouteInfoKeyIsRoot - does the route have wildcard parts
RouteInfoKeyIsRoot string = "is_root"
// RouteInfoKeyDescription - does the route have a catch all part
RouteInfoKeyDescription string = "description"
)
// Route stores all the information about a route
type Route struct {
Method string
Path string
PathFormat string
Handler ContextHandler
Meta map[string]interface{}
Info map[string]interface{}
}
// NewRoute creates a new Route instance
func NewRoute(method string, path string) *Route {
return NewRouteWithMeta(method, path, nil)
}
// NewRouteWithMeta creates a new Route instance with meta values
func NewRouteWithMeta(method string, path string, meta map[string]interface{}) *Route {
upMethod := strings.ToUpper(method)
routeMeta := meta
if routeMeta == nil {
routeMeta = map[string]interface{}{}
}
route := &Route{
Method: upMethod,
PathFormat: path,
Meta: routeMeta,
}
info := map[string]interface{}{}
if path == RootPath {
info[RouteInfoKeyIsRoot] = true
}
route.Info = info
return route
}
// Handle adds a ContextHandler to the Route
func (rte *Route) Handle(handlerFunc ContextHandler) *Route {
rte.Handler = handlerFunc
return rte
}
// HandleFunc adds a ContextHandlerFunc to the Route
func (rte *Route) HandleFunc(handlerFunc ContextHandlerFunc) *Route {
rte.Handler = handlerFunc
return rte
}
// Describe allows you to add a description of the route for other developers
func (rte *Route) Describe(description string) *Route {
rte.Info[RouteInfoKeyDescription] = description
return rte
}
// IsRoot returns true if the Route path is '/'
func (rte *Route) IsRoot() bool {
return rte.Info[RouteInfoKeyIsRoot] == true
}