Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nexpie-grafana-theme
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kornkitt Poolsup
nexpie-grafana-theme
Commits
f8422653
Commit
f8422653
authored
Sep 13, 2017
by
bergquist
Committed by
Carl Bergquist
Sep 14, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add custom route register
parent
bf138d18
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
101 additions
and
52 deletions
+101
-52
pkg/api/route_register.go
+33
-31
pkg/api/route_register_test.go
+68
-21
No files found.
pkg/api/route_register.go
View file @
f8422653
...
...
@@ -6,18 +6,26 @@ import (
macaron
"gopkg.in/macaron.v1"
)
type
Router
interface
{
Route
(
pattern
,
method
string
,
handlers
...
macaron
.
Handler
)
}
type
RouteRegister
interface
{
Get
(
string
,
...
macaron
.
Handler
)
Post
(
string
,
...
macaron
.
Handler
)
Delete
(
string
,
...
macaron
.
Handler
)
Put
(
string
,
...
macaron
.
Handler
)
Group
(
string
,
func
(
RouteRegister
),
...
macaron
.
Handler
)
Register
(
Router
)
}
func
newRouteRegister
(
rr
*
macaron
.
Router
)
RouteRegister
{
func
newRouteRegister
()
RouteRegister
{
return
&
routeRegister
{
prefix
:
""
,
routes
:
[]
route
{},
subfixHandlers
:
[]
macaron
.
Handler
{},
}
}
...
...
@@ -31,56 +39,50 @@ type routeRegister struct {
prefix
string
subfixHandlers
[]
macaron
.
Handler
routes
[]
route
groups
[]
*
routeRegister
}
func
(
rr
*
routeRegister
)
Group
(
pattern
string
,
fn
func
(
rr
RouteRegister
),
handlers
...
macaron
.
Handler
)
{
group
:=
&
routeRegister
{
prefix
:
rr
.
prefix
+
pattern
,
subfixHandlers
:
handlers
,
routes
:
rr
.
routes
,
subfixHandlers
:
append
(
rr
.
subfixHandlers
,
handlers
...
)
,
routes
:
[]
route
{}
,
}
fn
(
group
)
rr
.
groups
=
append
(
rr
.
groups
,
group
)
}
func
(
rr
*
routeRegister
)
Get
(
pattern
string
,
handlers
...
macaron
.
Handl
er
)
{
rr
.
routes
=
append
(
rr
.
routes
,
route
{
method
:
http
.
MethodGet
,
pattern
:
rr
.
prefix
+
pattern
,
handlers
:
handlers
,
})
println
(
"get: get "
,
len
(
rr
.
routes
)
)
rr
.
routes
=
rr
.
routes
[
:
len
(
rr
.
routes
)
-
1
]
func
(
rr
*
routeRegister
)
Register
(
router
Rout
er
)
{
for
_
,
r
:=
range
rr
.
routes
{
router
.
Route
(
r
.
pattern
,
r
.
method
,
r
.
handlers
...
)
}
for
_
,
g
:=
range
rr
.
groups
{
g
.
Register
(
router
)
}
}
func
(
rr
*
routeRegister
)
Post
(
pattern
string
,
handlers
...
macaron
.
Handler
)
{
func
(
rr
*
routeRegister
)
route
(
pattern
,
method
string
,
handlers
...
macaron
.
Handler
)
{
rr
.
routes
=
append
(
rr
.
routes
,
route
{
method
:
http
.
MethodPost
,
method
:
method
,
pattern
:
rr
.
prefix
+
pattern
,
handlers
:
handlers
,
handlers
:
append
(
rr
.
subfixHandlers
,
handlers
...
)
,
})
println
(
"get: post "
,
len
(
rr
.
routes
))
}
rr
.
routes
=
rr
.
routes
[
:
len
(
rr
.
routes
)
-
1
]
func
(
rr
*
routeRegister
)
Get
(
pattern
string
,
handlers
...
macaron
.
Handler
)
{
rr
.
route
(
pattern
,
http
.
MethodGet
,
handlers
...
)
}
func
(
rr
*
routeRegister
)
Delete
(
pattern
string
,
handlers
...
macaron
.
Handler
)
{
rr
.
routes
=
append
(
rr
.
routes
,
route
{
method
:
http
.
MethodDelete
,
pattern
:
rr
.
prefix
+
pattern
,
handlers
:
handlers
,
})
println
(
"get: delete "
,
len
(
rr
.
routes
))
func
(
rr
*
routeRegister
)
Post
(
pattern
string
,
handlers
...
macaron
.
Handler
)
{
rr
.
route
(
pattern
,
http
.
MethodPost
,
handlers
...
)
}
rr
.
routes
=
rr
.
routes
[
:
len
(
rr
.
routes
)
-
1
]
func
(
rr
*
routeRegister
)
Delete
(
pattern
string
,
handlers
...
macaron
.
Handler
)
{
rr
.
route
(
pattern
,
http
.
MethodDelete
,
handlers
...
)
}
func
(
rr
*
routeRegister
)
Put
(
pattern
string
,
handlers
...
macaron
.
Handler
)
{
rr
.
routes
=
append
(
rr
.
routes
,
route
{
method
:
http
.
MethodPut
,
pattern
:
rr
.
prefix
+
pattern
,
handlers
:
handlers
,
})
rr
.
routes
=
rr
.
routes
[
:
len
(
rr
.
routes
)
-
1
]
rr
.
route
(
pattern
,
http
.
MethodPut
,
handlers
...
)
}
pkg/api/route_register_test.go
View file @
f8422653
package
api
import
"testing"
import
(
"strconv"
"testing"
func
TestRouteRegister
(
t
*
testing
.
T
)
{
macaron
"gopkg.in/macaron.v1"
)
type
fakeRouter
struct
{
route
[]
route
}
rr
:=
&
routeRegister
{
prefix
:
""
,
routes
:
[]
route
{},
func
(
fr
*
fakeRouter
)
Route
(
pattern
,
method
string
,
handlers
...
macaron
.
Handler
)
{
fr
.
route
=
append
(
fr
.
route
,
route
{
pattern
:
pattern
,
method
:
method
,
handlers
:
handlers
,
})
}
func
emptyHandlers
(
n
int
)
[]
macaron
.
Handler
{
res
:=
[]
macaron
.
Handler
{}
for
i
:=
1
;
n
>=
i
;
i
++
{
res
=
append
(
res
,
emptyHandler
(
strconv
.
Itoa
(
i
)))
}
return
res
}
rr
.
Delete
(
"/admin"
)
rr
.
Get
(
"/down"
)
func
emptyHandler
(
name
string
)
macaron
.
Handler
{
return
struct
{
name
string
}{
name
:
name
}
}
rr
.
Group
(
"/user"
,
func
(
innerRR
RouteRegister
)
{
innerRR
.
Delete
(
""
)
innerRR
.
Get
(
"/friends"
)
func
TestRouteRegister
(
t
*
testing
.
T
)
{
testTable
:=
[]
route
{
{
method
:
"DELETE"
,
pattern
:
"/admin"
,
handlers
:
emptyHandlers
(
1
)},
{
method
:
"GET"
,
pattern
:
"/down"
,
handlers
:
emptyHandlers
(
2
)},
{
method
:
"POST"
,
pattern
:
"/user"
,
handlers
:
emptyHandlers
(
1
)},
{
method
:
"PUT"
,
pattern
:
"/user/friends"
,
handlers
:
emptyHandlers
(
1
)},
{
method
:
"DELETE"
,
pattern
:
"/user/admin"
,
handlers
:
emptyHandlers
(
2
)},
{
method
:
"GET"
,
pattern
:
"/user/admin/all"
,
handlers
:
emptyHandlers
(
4
)},
}
// Setup
rr
:=
newRouteRegister
()
rr
.
Delete
(
"/admin"
,
emptyHandler
(
"1"
))
rr
.
Get
(
"/down"
,
emptyHandler
(
"1"
),
emptyHandler
(
"2"
))
rr
.
Group
(
"/user"
,
func
(
user
RouteRegister
)
{
user
.
Post
(
""
,
emptyHandler
(
"1"
))
user
.
Put
(
"/friends"
,
emptyHandler
(
"2"
))
user
.
Group
(
"/admin"
,
func
(
admin
RouteRegister
)
{
admin
.
Delete
(
""
,
emptyHandler
(
"3"
))
admin
.
Get
(
"/all"
,
emptyHandler
(
"3"
),
emptyHandler
(
"4"
),
emptyHandler
(
"5"
))
},
emptyHandler
(
"3"
))
})
println
(
"len"
,
len
(
rr
.
routes
))
fr
:=
&
fakeRouter
{}
rr
.
Register
(
fr
)
if
rr
.
routes
[
0
]
.
pattern
!=
"/admin"
&&
rr
.
routes
[
0
]
.
method
!=
"DELETE"
{
t
.
Errorf
(
"expected first route to be DELETE /admin"
)
// Validation
if
len
(
fr
.
route
)
!=
len
(
testTable
)
{
t
.
Errorf
(
"want %v routes, got %v"
,
len
(
testTable
),
len
(
fr
.
route
))
}
if
rr
.
routes
[
1
]
.
pattern
!=
"/down"
&&
rr
.
routes
[
1
]
.
method
!=
"GET"
{
t
.
Errorf
(
"expected first route to be GET /down"
)
for
i
,
_
:=
range
testTable
{
if
testTable
[
i
]
.
method
!=
fr
.
route
[
i
]
.
method
{
t
.
Errorf
(
"want %s got %v"
,
testTable
[
i
]
.
method
,
fr
.
route
[
i
]
.
method
)
}
println
(
"len"
,
len
(
rr
.
routes
))
if
rr
.
routes
[
2
]
.
pattern
!=
"/user"
&&
rr
.
routes
[
2
]
.
method
!=
"DELETE"
{
t
.
Errorf
(
"expected first route to be DELETE /admin"
)
if
testTable
[
i
]
.
pattern
!=
fr
.
route
[
i
]
.
pattern
{
t
.
Errorf
(
"want %s got %v"
,
testTable
[
i
]
.
pattern
,
fr
.
route
[
i
]
.
pattern
)
}
if
rr
.
routes
[
3
]
.
pattern
!=
"/user/friends"
&&
rr
.
routes
[
3
]
.
method
!=
"GET"
{
t
.
Errorf
(
"expected first route to be GET /down"
)
if
len
(
testTable
[
i
]
.
handlers
)
!=
len
(
fr
.
route
[
i
]
.
handlers
)
{
t
.
Errorf
(
"want %d handlers got %d handlers
\n
testcase: %v
\n
route: %v
\n
"
,
len
(
testTable
[
i
]
.
handlers
),
len
(
fr
.
route
[
i
]
.
handlers
),
testTable
[
i
],
fr
.
route
[
i
])
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment