Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
B
beego
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
beego
Commits
6809c976
Commit
6809c976
authored
Jun 10, 2014
by
astaxie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
beego: improve performance
parent
675643c6
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
51 additions
and
45 deletions
+51
-45
beego.go
beego.go
+4
-0
router.go
router.go
+40
-37
staticfile.go
staticfile.go
+7
-8
No files found.
beego.go
View file @
6809c976
...
...
@@ -379,6 +379,10 @@ func initBeforeHttpRun() {
middleware
.
VERSION
=
VERSION
middleware
.
AppName
=
AppName
middleware
.
RegisterErrorHandler
()
for
u
,
_
:=
range
StaticDir
{
Get
(
u
,
serverStaticRouter
)
}
}
// this function is for test package init
...
...
router.go
View file @
6809c976
...
...
@@ -43,7 +43,17 @@ const (
var
(
// supported http methods.
HTTPMETHOD
=
[]
string
{
"get"
,
"post"
,
"put"
,
"delete"
,
"patch"
,
"options"
,
"head"
,
"trace"
,
"connect"
}
HTTPMETHOD
=
map
[
string
]
string
{
"GET"
:
"GET"
,
"POST"
:
"POST"
,
"PUT"
:
"PUT"
,
"DELETE"
:
"DELETE"
,
"PATCH"
:
"PATCH"
,
"OPTIONS"
:
"OPTIONS"
,
"HEAD"
:
"HEAD"
,
"TRACE"
:
"TRACE"
,
"CONNECT"
:
"CONNECT"
,
}
// these beego.Controller's methods shouldn't reflect to AutoRouter
exceptMethod
=
[]
string
{
"Init"
,
"Prepare"
,
"Finish"
,
"Render"
,
"RenderString"
,
"RenderBytes"
,
"Redirect"
,
"Abort"
,
"StopRun"
,
"UrlFor"
,
"ServeJson"
,
"ServeJsonp"
,
...
...
@@ -106,9 +116,9 @@ func (p *ControllerRegistor) Add(pattern string, c ControllerInterface, mappingM
}
comma
:=
strings
.
Split
(
colon
[
0
],
","
)
for
_
,
m
:=
range
comma
{
if
m
==
"*"
||
utils
.
InSlice
(
strings
.
ToLower
(
m
),
HTTPMETHOD
)
{
if
_
,
ok
:=
HTTPMETHOD
[
strings
.
ToUpper
(
m
)];
m
==
"*"
||
ok
{
if
val
:=
reflectVal
.
MethodByName
(
colon
[
1
]);
val
.
IsValid
()
{
methods
[
strings
.
To
Low
er
(
m
)]
=
colon
[
1
]
methods
[
strings
.
To
Upp
er
(
m
)]
=
colon
[
1
]
}
else
{
panic
(
colon
[
1
]
+
" method doesn't exist in the controller "
+
t
.
Name
())
}
...
...
@@ -271,7 +281,7 @@ func (p *ControllerRegistor) Any(pattern string, f FilterFunc) {
// ctx.Output.Body("hello world")
// })
func
(
p
*
ControllerRegistor
)
AddMethod
(
method
,
pattern
string
,
f
FilterFunc
)
{
if
method
!=
"*"
&&
!
utils
.
InSlice
(
strings
.
ToLower
(
method
),
HTTPMETHOD
)
{
if
_
,
ok
:=
HTTPMETHOD
[
strings
.
ToUpper
(
method
)];
method
!=
"*"
&&
!
ok
{
panic
(
"not support http method: "
+
method
)
}
route
:=
&
controllerInfo
{}
...
...
@@ -284,7 +294,7 @@ func (p *ControllerRegistor) AddMethod(method, pattern string, f FilterFunc) {
methods
[
val
]
=
val
}
}
else
{
methods
[
method
]
=
method
methods
[
strings
.
ToUpper
(
method
)]
=
strings
.
ToUpper
(
method
)
}
route
.
methods
=
methods
for
k
,
_
:=
range
methods
{
...
...
@@ -417,8 +427,8 @@ func (p *ControllerRegistor) geturl(t *Tree, url, controllName, methodName strin
if
c
,
ok
:=
t
.
leaf
.
runObject
.
(
*
controllerInfo
);
ok
{
if
c
.
routerType
==
routerTypeBeego
&&
c
.
controllerType
.
Name
()
==
controllName
{
find
:=
false
if
utils
.
InSlice
(
strings
.
ToLower
(
methodName
),
HTTPMETHOD
)
{
if
m
,
ok
:=
c
.
methods
[
strings
.
To
Lower
(
methodName
)];
ok
&&
m
!=
methodName
{
if
_
,
ok
:=
HTTPMETHOD
[
strings
.
ToUpper
(
methodName
)];
ok
{
if
m
,
ok
:=
c
.
methods
[
strings
.
To
Upper
(
methodName
)];
ok
&&
m
!=
strings
.
ToUpper
(
methodName
)
{
return
false
,
""
}
else
if
m
,
ok
=
c
.
methods
[
"*"
];
ok
&&
m
!=
methodName
{
return
false
,
""
...
...
@@ -508,8 +518,6 @@ func (p *ControllerRegistor) geturl(t *Tree, url, controllName, methodName strin
func
(
p
*
ControllerRegistor
)
ServeHTTP
(
rw
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
defer
p
.
recoverPanic
(
rw
,
r
)
starttime
:=
time
.
Now
()
requestPath
:=
r
.
URL
.
Path
method
:=
strings
.
ToLower
(
r
.
Method
)
var
runrouter
reflect
.
Type
var
findrouter
bool
var
runMethod
string
...
...
@@ -559,12 +567,12 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
}()
}
if
!
utils
.
InSlice
(
method
,
HTTPMETHOD
)
{
if
_
,
ok
:=
HTTPMETHOD
[
r
.
Method
];
!
ok
{
http
.
Error
(
w
,
"Method Not Allowed"
,
405
)
goto
Admin
}
if
!
context
.
Input
.
IsGet
()
&&
!
context
.
Input
.
IsHead
()
{
if
r
.
Method
!=
"GET"
&&
r
.
Method
!=
"HEAD"
{
if
CopyRequestBody
&&
!
context
.
Input
.
IsUpload
()
{
context
.
Input
.
CopyBody
()
}
...
...
@@ -575,11 +583,6 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
goto
Admin
}
//static file server
if
serverStaticRouter
(
context
)
{
goto
Admin
}
if
context
.
Input
.
RunController
!=
nil
&&
context
.
Input
.
RunMethod
!=
""
{
findrouter
=
true
runMethod
=
context
.
Input
.
RunMethod
...
...
@@ -587,8 +590,8 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
}
if
!
findrouter
{
if
t
,
ok
:=
p
.
routers
[
m
ethod
];
ok
{
runObject
,
p
:=
t
.
Match
(
r
equest
Path
)
if
t
,
ok
:=
p
.
routers
[
r
.
M
ethod
];
ok
{
runObject
,
p
:=
t
.
Match
(
r
.
URL
.
Path
)
if
r
,
ok
:=
runObject
.
(
*
controllerInfo
);
ok
{
routerInfo
=
r
findrouter
=
true
...
...
@@ -618,7 +621,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
isRunable
:=
false
if
routerInfo
!=
nil
{
if
routerInfo
.
routerType
==
routerTypeRESTFul
{
if
_
,
ok
:=
routerInfo
.
methods
[
strings
.
ToLower
(
r
.
Method
)
];
ok
{
if
_
,
ok
:=
routerInfo
.
methods
[
r
.
Method
];
ok
{
isRunable
=
true
routerInfo
.
runfunction
(
context
)
}
else
{
...
...
@@ -630,19 +633,19 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
routerInfo
.
handler
.
ServeHTTP
(
rw
,
r
)
}
else
{
runrouter
=
routerInfo
.
controllerType
method
:=
strings
.
ToLower
(
r
.
Method
)
if
method
==
"post"
&&
strings
.
ToLower
(
context
.
Input
.
Query
(
"_method"
))
==
"put
"
{
method
=
"
put
"
method
:=
r
.
Method
if
r
.
Method
==
"POST"
&&
context
.
Input
.
Query
(
"_method"
)
==
"PUT
"
{
method
=
"
PUT
"
}
if
method
==
"post"
&&
strings
.
ToLower
(
context
.
Input
.
Query
(
"_method"
))
==
"delete
"
{
method
=
"
delete
"
if
r
.
Method
==
"POST"
&&
context
.
Input
.
Query
(
"_method"
)
==
"DELETE
"
{
method
=
"
DELETE
"
}
if
m
,
ok
:=
routerInfo
.
methods
[
method
];
ok
{
runMethod
=
m
}
else
if
m
,
ok
=
routerInfo
.
methods
[
"*"
];
ok
{
runMethod
=
m
}
else
{
runMethod
=
strings
.
Title
(
method
)
runMethod
=
method
}
}
}
...
...
@@ -676,19 +679,19 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
if
!
w
.
started
{
//exec main logic
switch
runMethod
{
case
"G
et
"
:
case
"G
ET
"
:
execController
.
Get
()
case
"P
ost
"
:
case
"P
OST
"
:
execController
.
Post
()
case
"D
elete
"
:
case
"D
ELETE
"
:
execController
.
Delete
()
case
"P
ut
"
:
case
"P
UT
"
:
execController
.
Put
()
case
"H
ead
"
:
case
"H
EAD
"
:
execController
.
Head
()
case
"P
atch
"
:
case
"P
ATCH
"
:
execController
.
Patch
()
case
"O
ptions
"
:
case
"O
PTIONS
"
:
execController
.
Options
()
default
:
if
!
execController
.
HandlerFunc
(
runMethod
)
{
...
...
@@ -725,20 +728,20 @@ Admin:
timeend
:=
time
.
Since
(
starttime
)
//admin module record QPS
if
EnableAdmin
{
if
FilterMonitorFunc
(
r
.
Method
,
r
equest
Path
,
timeend
)
{
if
FilterMonitorFunc
(
r
.
Method
,
r
.
URL
.
Path
,
timeend
)
{
if
runrouter
!=
nil
{
go
toolbox
.
StatisticsMap
.
AddStatistics
(
r
.
Method
,
r
equest
Path
,
runrouter
.
Name
(),
timeend
)
go
toolbox
.
StatisticsMap
.
AddStatistics
(
r
.
Method
,
r
.
URL
.
Path
,
runrouter
.
Name
(),
timeend
)
}
else
{
go
toolbox
.
StatisticsMap
.
AddStatistics
(
r
.
Method
,
r
equest
Path
,
""
,
timeend
)
go
toolbox
.
StatisticsMap
.
AddStatistics
(
r
.
Method
,
r
.
URL
.
Path
,
""
,
timeend
)
}
}
}
if
RunMode
==
"dev"
{
if
findrouter
{
Info
(
"beego: router defined "
+
routerInfo
.
pattern
+
" "
+
r
equest
Path
+
" +"
+
timeend
.
String
())
Info
(
"beego: router defined "
+
routerInfo
.
pattern
+
" "
+
r
.
URL
.
Path
+
" +"
+
timeend
.
String
())
}
else
{
Info
(
"beego:"
+
r
equest
Path
+
" 404"
+
" +"
+
timeend
.
String
())
Info
(
"beego:"
+
r
.
URL
.
Path
+
" 404"
+
" +"
+
timeend
.
String
())
}
}
}
...
...
staticfile.go
View file @
6809c976
...
...
@@ -18,7 +18,7 @@ import (
"github.com/astaxie/beego/utils"
)
func
serverStaticRouter
(
ctx
*
context
.
Context
)
bool
{
func
serverStaticRouter
(
ctx
*
context
.
Context
)
{
requestPath
:=
path
.
Clean
(
ctx
.
Input
.
Request
.
URL
.
Path
)
for
prefix
,
staticDir
:=
range
StaticDir
{
if
len
(
prefix
)
==
0
{
...
...
@@ -28,7 +28,7 @@ func serverStaticRouter(ctx *context.Context) bool {
file
:=
path
.
Join
(
staticDir
,
requestPath
)
if
utils
.
FileExists
(
file
)
{
http
.
ServeFile
(
ctx
.
ResponseWriter
,
ctx
.
Request
,
file
)
return
true
return
}
}
if
strings
.
HasPrefix
(
requestPath
,
prefix
)
{
...
...
@@ -37,7 +37,7 @@ func serverStaticRouter(ctx *context.Context) bool {
}
if
requestPath
==
prefix
&&
prefix
[
len
(
prefix
)
-
1
]
!=
'/'
{
http
.
Redirect
(
ctx
.
ResponseWriter
,
ctx
.
Request
,
requestPath
+
"/"
,
302
)
return
true
return
}
file
:=
path
.
Join
(
staticDir
,
requestPath
[
len
(
prefix
)
:
])
finfo
,
err
:=
os
.
Stat
(
file
)
...
...
@@ -46,12 +46,12 @@ func serverStaticRouter(ctx *context.Context) bool {
Warn
(
err
)
}
http
.
NotFound
(
ctx
.
ResponseWriter
,
ctx
.
Request
)
return
true
return
}
//if the request is dir and DirectoryIndex is false then
if
finfo
.
IsDir
()
&&
!
DirectoryIndex
{
middleware
.
Exception
(
"403"
,
ctx
.
ResponseWriter
,
ctx
.
Request
,
"403 Forbidden"
)
return
true
return
}
//This block obtained from (https://github.com/smithfox/beego) - it should probably get merged into astaxie/beego after a pull request
...
...
@@ -73,7 +73,7 @@ func serverStaticRouter(ctx *context.Context) bool {
memzipfile
,
err
:=
openMemZipFile
(
file
,
contentEncoding
)
if
err
!=
nil
{
return
true
return
}
if
contentEncoding
==
"gzip"
{
...
...
@@ -89,8 +89,7 @@ func serverStaticRouter(ctx *context.Context) bool {
}
else
{
http
.
ServeFile
(
ctx
.
ResponseWriter
,
ctx
.
Request
,
file
)
}
return
true
return
}
}
return
false
}
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