-
Notifications
You must be signed in to change notification settings - Fork 0
/
interfaces.go
71 lines (59 loc) · 1.6 KB
/
interfaces.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
package router
import (
"net/http"
"net/url"
)
const (
DefaultParamRequirement = `([^\/]+)`
)
type Action interface{}
type Options struct {
Priority int
Secure bool
Host string
DefaultParams ParamsMap
}
type Route interface {
Priority() int
Name() string
Path() string
Action() Action
URL(params ParamsMap) (*url.URL, error)
ExtractParams(request *http.Request) (ParamsMap, error)
}
type RouteGroup interface {
AddRoute(name string, path string, method string, action Action, options Options) error
AddDeleteRoute(name string, path string, action Action) error
AddGetRoute(name string, path string, action Action) error
AddHeadRoute(name string, path string, action Action) error
AddOptionsRoute(name string, path string, action Action) error
AddPatchRoute(name string, path string, action Action) error
AddPostRoute(name string, path string, action Action) error
AddPutRoute(name string, path string, action Action) error
AddRouteGroup(name string, path string, options Options) (RouteGroup, error)
}
type Router interface {
RouteGroup
FindRouteByRequest(request *http.Request) (Route, bool)
FindRouteByName(name string) (Route, bool)
}
type Builder interface {
SetSecure(secure bool) Builder
SetHost(host string) Builder
SetDefaultParamRequirement(expr string) Builder
Build() (Router, error)
}
func New() Router {
router, err := NewBuilder().Build()
if err != nil {
panic(err)
}
return router
}
func NewWithHost(secure bool, host string) Router {
router, err := NewBuilder().SetSecure(secure).SetHost(host).Build()
if err != nil {
panic(err)
}
return router
}