-
Notifications
You must be signed in to change notification settings - Fork 5
/
ship_route_group_builder.go
119 lines (104 loc) · 3.53 KB
/
ship_route_group_builder.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
// Copyright 2020 xgfone
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ship
import (
"strings"
)
// RouteGroupBuilder is a route group to build a set of routes.
type RouteGroupBuilder struct {
ship *Ship
prefix string
data interface{}
mdwares []Middleware
}
func newRouteGroup(s *Ship, pprefix, prefix string,
mws ...Middleware) *RouteGroupBuilder {
if prefix = strings.TrimSuffix(prefix, "/"); len(prefix) == 0 {
prefix = "/"
} else if prefix[0] != '/' {
prefix = "/" + prefix
}
return &RouteGroupBuilder{
ship: s,
prefix: strings.TrimSuffix(pprefix, "/") + prefix,
mdwares: append([]Middleware{}, mws...),
}
}
// Group returns a new route sub-group with the group prefix.
//
// If the prefix does not start with "/", it will add "/" as the prefix.
func (s *Ship) Group(prefix string) *RouteGroupBuilder {
return newRouteGroup(s, s.Prefix, prefix, s.mws...)
}
// Ship returns the ship that the current group belongs to.
func (g *RouteGroupBuilder) Ship() *Ship { return g.ship }
// Clone clones itself and returns a new one.
func (g *RouteGroupBuilder) Clone() *RouteGroupBuilder {
return &RouteGroupBuilder{
ship: g.ship,
data: g.data,
prefix: g.prefix,
mdwares: append([]Middleware{}, g.mdwares...),
}
}
// Use appends some middlwares into the group.
func (g *RouteGroupBuilder) Use(middlewares ...Middleware) *RouteGroupBuilder {
g.mdwares = append(g.mdwares, middlewares...)
return g
}
// Group returns a new route sub-group.
//
// If the prefix does not start with "/", it will add "/" as the prefix.
func (g *RouteGroupBuilder) Group(prefix string, middlewares ...Middleware) *RouteGroupBuilder {
mws := make([]Middleware, 0, len(g.mdwares)+len(middlewares))
mws = append(mws, g.mdwares...)
mws = append(mws, middlewares...)
return newRouteGroup(g.ship, g.prefix, prefix, mws...)
}
// Data sets the context data.
func (g *RouteGroupBuilder) Data(data interface{}) *RouteGroupBuilder {
g.data = data
return g
}
// Route returns a new route, which is used to build and register the route.
//
// You should call Method() or its short method to register it.
func (g *RouteGroupBuilder) Route(path string) *RouteBuilder {
return newRouteBuilder(g.ship, g, g.prefix, path, g.data, g.mdwares...)
}
// ResetMiddlewares resets the middlewares of the group to ms.
func (g *RouteGroupBuilder) ResetMiddlewares(ms ...Middleware) *RouteGroupBuilder {
g.mdwares = append([]Middleware{}, ms...)
return g
}
// AddRoutes registers a set of the routes.
//
// It will panic with it if there is an error when adding the routes.
func (g *RouteGroupBuilder) AddRoutes(routes ...Route) *RouteGroupBuilder {
for i, r := range routes {
routes[i].Path = g.Route(r.Path).path
}
g.ship.AddRoutes(routes...)
return g
}
// DelRoutes deletes a set of the registered routes.
//
// It will panic with it if there is an error when deleting the routes.
func (g *RouteGroupBuilder) DelRoutes(routes ...Route) *RouteGroupBuilder {
for i, r := range routes {
routes[i].Path = g.Route(r.Path).path
}
g.ship.DelRoutes(routes...)
return g
}