Skip to content
Toggle navigation
Toggle navigation
This project
Loading...
Sign in
张磊
/
FileStorageBeego
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
1f3ae3d6
authored
2013-08-11 23:14:30 +0800
by
astaxie
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
Improve performance
1 parent
ca1354e7
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
49 additions
and
6 deletions
router.go
router.go
View file @
1f3ae3d
...
...
@@ -22,6 +22,7 @@ type controllerInfo struct {
params
map
[
int
]
string
controllerType
reflect
.
Type
methods
map
[
string
]
string
hasMethod
bool
}
type
userHandler
struct
{
...
...
@@ -34,8 +35,12 @@ type userHandler struct {
type
ControllerRegistor
struct
{
routers
[]
*
controllerInfo
fixrouters
[]
*
controllerInfo
enableFilter
bool
filters
[]
http
.
HandlerFunc
afterFilters
[]
http
.
HandlerFunc
enableUser
bool
userHandlers
map
[
string
]
*
userHandler
enableAuto
bool
autoRouter
map
[
string
]
map
[
string
]
reflect
.
Type
//key:controller key:method value:reflect.type
}
...
...
@@ -130,6 +135,9 @@ func (p *ControllerRegistor) Add(pattern string, c ControllerInterface, mappingM
route
.
pattern
=
pattern
route
.
controllerType
=
t
route
.
methods
=
methods
if
len
(
methods
)
>
0
{
route
.
hasMethod
=
true
}
p
.
fixrouters
=
append
(
p
.
fixrouters
,
route
)
}
else
{
// add regexp routers
//recreate the url pattern, with parameters replaced
...
...
@@ -149,12 +157,16 @@ func (p *ControllerRegistor) Add(pattern string, c ControllerInterface, mappingM
route
.
params
=
params
route
.
pattern
=
pattern
route
.
methods
=
methods
if
len
(
methods
)
>
0
{
route
.
hasMethod
=
true
}
route
.
controllerType
=
t
p
.
routers
=
append
(
p
.
routers
,
route
)
}
}
func
(
p
*
ControllerRegistor
)
AddAuto
(
c
ControllerInterface
)
{
p
.
enableAuto
=
true
reflectVal
:=
reflect
.
ValueOf
(
c
)
rt
:=
reflectVal
.
Type
()
ct
:=
reflect
.
Indirect
(
reflectVal
)
.
Type
()
...
...
@@ -170,6 +182,7 @@ func (p *ControllerRegistor) AddAuto(c ControllerInterface) {
}
func
(
p
*
ControllerRegistor
)
AddHandler
(
pattern
string
,
c
http
.
Handler
)
{
p
.
enableUser
=
true
parts
:=
strings
.
Split
(
pattern
,
"/"
)
j
:=
0
...
...
@@ -217,6 +230,7 @@ func (p *ControllerRegistor) AddHandler(pattern string, c http.Handler) {
// Filter adds the middleware filter.
func
(
p
*
ControllerRegistor
)
Filter
(
filter
http
.
HandlerFunc
)
{
p
.
enableFilter
=
true
p
.
filters
=
append
(
p
.
filters
,
filter
)
}
...
...
@@ -332,6 +346,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
r
.
ParseMultipartForm
(
MaxMemory
)
//user defined Handler
if
p
.
enableUser
{
for
pattern
,
c
:=
range
p
.
userHandlers
{
if
c
.
regex
==
nil
&&
pattern
==
requestPath
{
c
.
h
.
ServeHTTP
(
rw
,
r
)
...
...
@@ -368,16 +383,11 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
c
.
h
.
ServeHTTP
(
rw
,
r
)
return
}
}
//first find path from the fixrouters to Improve Performance
for
_
,
route
:=
range
p
.
fixrouters
{
n
:=
len
(
requestPath
)
//route like "/"
//if n == 1 {
// else {
// continue
// }
//}
if
requestPath
==
route
.
pattern
{
runrouter
=
route
findrouter
=
true
...
...
@@ -392,6 +402,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
}
}
//find regex's router
if
!
findrouter
{
//find a matching Route
for
_
,
route
:=
range
p
.
routers
{
...
...
@@ -466,6 +477,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
//if response has written,yes don't run next
if
!
w
.
started
{
if
r
.
Method
==
"GET"
{
if
runrouter
.
hasMethod
{
if
m
,
ok
:=
runrouter
.
methods
[
"get"
];
ok
{
method
=
vc
.
MethodByName
(
m
)
}
else
if
m
,
ok
=
runrouter
.
methods
[
"*"
];
ok
{
...
...
@@ -473,8 +485,12 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
}
else
{
method
=
vc
.
MethodByName
(
"Get"
)
}
}
else
{
method
=
vc
.
MethodByName
(
"Get"
)
}
method
.
Call
(
in
)
}
else
if
r
.
Method
==
"HEAD"
{
if
runrouter
.
hasMethod
{
if
m
,
ok
:=
runrouter
.
methods
[
"head"
];
ok
{
method
=
vc
.
MethodByName
(
m
)
}
else
if
m
,
ok
=
runrouter
.
methods
[
"*"
];
ok
{
...
...
@@ -482,8 +498,13 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
}
else
{
method
=
vc
.
MethodByName
(
"Head"
)
}
}
else
{
method
=
vc
.
MethodByName
(
"Head"
)
}
method
.
Call
(
in
)
}
else
if
r
.
Method
==
"DELETE"
||
(
r
.
Method
==
"POST"
&&
r
.
Form
.
Get
(
"_method"
)
==
"delete"
)
{
if
runrouter
.
hasMethod
{
if
m
,
ok
:=
runrouter
.
methods
[
"delete"
];
ok
{
method
=
vc
.
MethodByName
(
m
)
}
else
if
m
,
ok
=
runrouter
.
methods
[
"*"
];
ok
{
...
...
@@ -491,8 +512,12 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
}
else
{
method
=
vc
.
MethodByName
(
"Delete"
)
}
}
else
{
method
=
vc
.
MethodByName
(
"Delete"
)
}
method
.
Call
(
in
)
}
else
if
r
.
Method
==
"PUT"
||
(
r
.
Method
==
"POST"
&&
r
.
Form
.
Get
(
"_method"
)
==
"put"
)
{
if
runrouter
.
hasMethod
{
if
m
,
ok
:=
runrouter
.
methods
[
"put"
];
ok
{
method
=
vc
.
MethodByName
(
m
)
}
else
if
m
,
ok
=
runrouter
.
methods
[
"*"
];
ok
{
...
...
@@ -500,8 +525,12 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
}
else
{
method
=
vc
.
MethodByName
(
"Put"
)
}
}
else
{
method
=
vc
.
MethodByName
(
"Put"
)
}
method
.
Call
(
in
)
}
else
if
r
.
Method
==
"POST"
{
if
runrouter
.
hasMethod
{
if
m
,
ok
:=
runrouter
.
methods
[
"post"
];
ok
{
method
=
vc
.
MethodByName
(
m
)
}
else
if
m
,
ok
=
runrouter
.
methods
[
"*"
];
ok
{
...
...
@@ -509,8 +538,12 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
}
else
{
method
=
vc
.
MethodByName
(
"Post"
)
}
}
else
{
method
=
vc
.
MethodByName
(
"Post"
)
}
method
.
Call
(
in
)
}
else
if
r
.
Method
==
"PATCH"
{
if
runrouter
.
hasMethod
{
if
m
,
ok
:=
runrouter
.
methods
[
"patch"
];
ok
{
method
=
vc
.
MethodByName
(
m
)
}
else
if
m
,
ok
=
runrouter
.
methods
[
"*"
];
ok
{
...
...
@@ -518,8 +551,12 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
}
else
{
method
=
vc
.
MethodByName
(
"Patch"
)
}
}
else
{
method
=
vc
.
MethodByName
(
"Patch"
)
}
method
.
Call
(
in
)
}
else
if
r
.
Method
==
"OPTIONS"
{
if
runrouter
.
hasMethod
{
if
m
,
ok
:=
runrouter
.
methods
[
"options"
];
ok
{
method
=
vc
.
MethodByName
(
m
)
}
else
if
m
,
ok
=
runrouter
.
methods
[
"*"
];
ok
{
...
...
@@ -527,6 +564,9 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
}
else
{
method
=
vc
.
MethodByName
(
"Options"
)
}
}
else
{
method
=
vc
.
MethodByName
(
"Options"
)
}
method
.
Call
(
in
)
}
gotofunc
:=
vc
.
Elem
()
.
FieldByName
(
"gotofunc"
)
.
String
()
...
...
@@ -553,6 +593,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
//start autorouter
if
p
.
enableAuto
{
if
!
findrouter
{
for
cName
,
methodmap
:=
range
p
.
autoRouter
{
...
...
@@ -619,6 +660,8 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
}
}
}
}
//if no matches to url, throw a not found exception
if
!
findrouter
{
if
h
,
ok
:=
ErrorMaps
[
"404"
];
ok
{
...
...
Write
Preview
Styling with
Markdown
is supported
Attach a file
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 post a comment