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
54c89c0d
authored
2013-12-17 16:46:56 +0800
by
slene
Browse Files
Options
Browse Files
Tag
Download
Plain Diff
Merge branch 'master' of github.com:astaxie/beego
2 parents
1c52f683
c3bc2bed
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
92 additions
and
212 deletions
context/context.go
controller.go
router.go
context/context.go
View file @
54c89c0
...
...
@@ -16,7 +16,6 @@ type Context struct {
func
(
ctx
*
Context
)
Redirect
(
status
int
,
localurl
string
)
{
ctx
.
Output
.
Header
(
"Location"
,
localurl
)
ctx
.
Output
.
SetStatus
(
status
)
ctx
.
Output
.
Body
([]
byte
(
" "
))
}
func
(
ctx
*
Context
)
Abort
(
status
int
,
body
string
)
{
...
...
controller.go
View file @
54c89c0
...
...
@@ -24,10 +24,15 @@ import (
"github.com/astaxie/beego/session"
)
var
(
USERSTOPRUN
=
errors
.
New
(
"User stop run"
)
)
type
Controller
struct
{
Ctx
*
context
.
Context
Data
map
[
interface
{}]
interface
{}
ChildName
string
controllerName
string
actionName
string
TplNames
string
Layout
string
TplExt
string
...
...
@@ -39,7 +44,7 @@ type Controller struct {
}
type
ControllerInterface
interface
{
Init
(
ct
*
context
.
Context
,
c
hild
Name
string
,
app
interface
{})
Init
(
ct
*
context
.
Context
,
c
ontrollerName
,
action
Name
string
,
app
interface
{})
Prepare
()
Get
()
Post
()
...
...
@@ -52,11 +57,12 @@ type ControllerInterface interface {
Render
()
error
}
func
(
c
*
Controller
)
Init
(
ctx
*
context
.
Context
,
c
hild
Name
string
,
app
interface
{})
{
func
(
c
*
Controller
)
Init
(
ctx
*
context
.
Context
,
c
ontrollerName
,
action
Name
string
,
app
interface
{})
{
c
.
Data
=
make
(
map
[
interface
{}]
interface
{})
c
.
Layout
=
""
c
.
TplNames
=
""
c
.
ChildName
=
childName
c
.
controllerName
=
controllerName
c
.
actionName
=
actionName
c
.
Ctx
=
ctx
c
.
TplExt
=
"tpl"
c
.
AppController
=
app
...
...
@@ -119,7 +125,7 @@ func (c *Controller) RenderBytes() ([]byte, error) {
//if the controller has set layout, then first get the tplname's content set the content to the layout
if
c
.
Layout
!=
""
{
if
c
.
TplNames
==
""
{
c
.
TplNames
=
c
.
ChildName
+
"/"
+
strings
.
ToLower
(
c
.
Ctx
.
Request
.
Method
)
+
"."
+
c
.
TplExt
c
.
TplNames
=
strings
.
ToLower
(
c
.
controllerName
)
+
"/"
+
strings
.
ToLower
(
c
.
actionName
)
+
"."
+
c
.
TplExt
}
if
RunMode
==
"dev"
{
BuildTemplate
(
ViewsPath
)
...
...
@@ -146,7 +152,7 @@ func (c *Controller) RenderBytes() ([]byte, error) {
return
icontent
,
nil
}
else
{
if
c
.
TplNames
==
""
{
c
.
TplNames
=
c
.
ChildName
+
"/"
+
strings
.
ToLower
(
c
.
Ctx
.
Request
.
Method
)
+
"."
+
c
.
TplExt
c
.
TplNames
=
strings
.
ToLower
(
c
.
controllerName
)
+
"/"
+
strings
.
ToLower
(
c
.
actionName
)
+
"."
+
c
.
TplExt
}
if
RunMode
==
"dev"
{
BuildTemplate
(
ViewsPath
)
...
...
@@ -181,7 +187,7 @@ func (c *Controller) Abort(code string) {
}
func
(
c
*
Controller
)
StopRun
()
{
panic
(
""
)
panic
(
USERSTOPRUN
)
}
func
(
c
*
Controller
)
UrlFor
(
endpoint
string
,
values
...
string
)
string
{
...
...
router.go
View file @
54c89c0
...
...
@@ -211,6 +211,7 @@ func (p *ControllerRegistor) Add(pattern string, c ControllerInterface, mappingM
}
}
// add auto router to controller
func
(
p
*
ControllerRegistor
)
AddAuto
(
c
ControllerInterface
)
{
p
.
enableAuto
=
true
reflectVal
:=
reflect
.
ValueOf
(
c
)
...
...
@@ -369,6 +370,9 @@ func (p *ControllerRegistor) UrlFor(endpoint string, values ...string) string {
func
(
p
*
ControllerRegistor
)
ServeHTTP
(
rw
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
defer
func
()
{
if
err
:=
recover
();
err
!=
nil
{
if
err
==
USERSTOPRUN
{
return
}
if
_
,
ok
:=
err
.
(
middleware
.
HTTPException
);
ok
{
// catch intented errors, only for HTTP 4XX and 5XX
}
else
{
...
...
@@ -421,8 +425,9 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
starttime
:=
time
.
Now
()
requestPath
:=
r
.
URL
.
Path
var
runrouter
*
controllerInfo
var
runrouter
reflect
.
Type
var
findrouter
bool
var
runMethod
string
params
:=
make
(
map
[
string
]
string
)
w
:=
&
responseWriter
{
writer
:
rw
}
...
...
@@ -547,8 +552,9 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
for
_
,
route
:=
range
p
.
fixrouters
{
n
:=
len
(
requestPath
)
if
requestPath
==
route
.
pattern
{
runrouter
=
route
runrouter
=
route
.
controllerType
findrouter
=
true
runMethod
=
p
.
getRunMethod
(
r
.
Method
,
route
)
break
}
// pattern /admin url /admin 200 /admin/ 404
...
...
@@ -588,176 +594,20 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
//reassemble query params and add to RawQuery
r
.
URL
.
RawQuery
=
url
.
Values
(
values
)
.
Encode
()
}
runrouter
=
route
runrouter
=
route
.
controllerType
findrouter
=
true
break
}
}
context
.
Input
.
Params
=
params
if
runrouter
!=
nil
{
if
r
.
Method
==
"POST"
{
r
.
ParseMultipartForm
(
MaxMemory
)
}
//execute middleware filters
if
do_filter
(
BeforeExec
)
{
goto
Admin
}
//Invoke the request handler
vc
:=
reflect
.
New
(
runrouter
.
controllerType
)
//call the controller init function
method
:=
vc
.
MethodByName
(
"Init"
)
in
:=
make
([]
reflect
.
Value
,
3
)
in
[
0
]
=
reflect
.
ValueOf
(
context
)
in
[
1
]
=
reflect
.
ValueOf
(
runrouter
.
controllerType
.
Name
())
in
[
2
]
=
reflect
.
ValueOf
(
vc
.
Interface
())
method
.
Call
(
in
)
//if XSRF is Enable then check cookie where there has any cookie in the request's cookie _csrf
if
EnableXSRF
{
in
=
make
([]
reflect
.
Value
,
0
)
method
=
vc
.
MethodByName
(
"XsrfToken"
)
method
.
Call
(
in
)
if
r
.
Method
==
"POST"
||
r
.
Method
==
"DELETE"
||
r
.
Method
==
"PUT"
||
(
r
.
Method
==
"POST"
&&
(
r
.
Form
.
Get
(
"_method"
)
==
"delete"
||
r
.
Form
.
Get
(
"_method"
)
==
"put"
))
{
method
=
vc
.
MethodByName
(
"CheckXsrfCookie"
)
method
.
Call
(
in
)
}
}
//call prepare function
in
=
make
([]
reflect
.
Value
,
0
)
method
=
vc
.
MethodByName
(
"Prepare"
)
method
.
Call
(
in
)
//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
{
method
=
vc
.
MethodByName
(
m
)
}
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
{
method
=
vc
.
MethodByName
(
m
)
}
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
{
method
=
vc
.
MethodByName
(
m
)
}
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
{
method
=
vc
.
MethodByName
(
m
)
}
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
{
method
=
vc
.
MethodByName
(
m
)
}
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
{
method
=
vc
.
MethodByName
(
m
)
}
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
{
method
=
vc
.
MethodByName
(
m
)
}
else
{
method
=
vc
.
MethodByName
(
"Options"
)
}
}
else
{
method
=
vc
.
MethodByName
(
"Options"
)
}
method
.
Call
(
in
)
}
gotofunc
:=
vc
.
Elem
()
.
FieldByName
(
"gotofunc"
)
.
String
()
if
gotofunc
!=
""
{
method
=
vc
.
MethodByName
(
gotofunc
)
if
method
.
IsValid
()
{
method
.
Call
(
in
)
}
else
{
panic
(
"gotofunc is exists:"
+
gotofunc
)
}
}
if
!
w
.
started
&&
!
context
.
Input
.
IsWebsocket
()
{
if
AutoRender
{
method
=
vc
.
MethodByName
(
"Render"
)
callMethodWithError
(
method
,
in
)
}
}
}
method
=
vc
.
MethodByName
(
"Finish"
)
method
.
Call
(
in
)
//execute middleware filters
if
do_filter
(
AfterExec
)
{
goto
Admin
runMethod
=
p
.
getRunMethod
(
r
.
Method
,
route
)
break
}
}
//start autorouter
if
p
.
enableAuto
{
if
!
findrouter
{
if
!
findrouter
&&
p
.
enableAuto
{
// deal with url with diffirent ext
// /controller/simple
// /controller/simple.html
// /controller/simple.json
// /controller/simple.rss
lastindex
:=
strings
.
LastIndex
(
requestPath
,
"/"
)
lastsub
:=
requestPath
[
lastindex
+
1
:
]
if
subindex
:=
strings
.
LastIndex
(
lastsub
,
"."
);
subindex
!=
-
1
{
...
...
@@ -767,57 +617,64 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
requestPath
=
requestPath
[
:
len
(
requestPath
)
-
len
(
lastsub
[
subindex
:
])]
}
for
cName
,
methodmap
:=
range
p
.
autoRouter
{
// if prev already find the router break
if
findrouter
{
break
}
if
strings
.
ToLower
(
requestPath
)
==
"/"
+
cName
{
http
.
Redirect
(
w
,
r
,
requestPath
+
"/"
,
301
)
goto
Admin
}
// if there's no action, set the default action to index
if
strings
.
ToLower
(
requestPath
)
==
"/"
+
cName
+
"/"
{
requestPath
=
requestPath
+
"index"
}
// if the request path start with controllerName
if
strings
.
HasPrefix
(
strings
.
ToLower
(
requestPath
),
"/"
+
cName
+
"/"
)
{
for
mName
,
controllerType
:=
range
methodmap
{
if
strings
.
ToLower
(
requestPath
)
==
"/"
+
cName
+
"/"
+
strings
.
ToLower
(
mName
)
||
(
strings
.
HasPrefix
(
strings
.
ToLower
(
requestPath
),
"/"
+
cName
+
"/"
+
strings
.
ToLower
(
mName
))
&&
requestPath
[
len
(
"/"
+
cName
+
"/"
+
strings
.
ToLower
(
mName
))
:
len
(
"/"
+
cName
+
"/"
+
strings
.
ToLower
(
mName
))
+
1
]
==
"/"
)
{
runrouter
=
controllerType
runMethod
=
mName
findrouter
=
true
break
}
}
}
}
}
//if no matches to url, throw a not found exception
if
!
findrouter
{
middleware
.
Exception
(
"404"
,
rw
,
r
,
""
)
goto
Admin
}
if
findrouter
{
if
r
.
Method
==
"POST"
{
r
.
ParseMultipartForm
(
MaxMemory
)
}
// set find
findrouter
=
true
//execute middleware filters
if
do_filter
(
BeforeExec
)
{
goto
Admin
}
//parse params
otherurl
:=
requestPath
[
len
(
"/"
+
cName
+
"/"
+
strings
.
ToLower
(
mName
))
:
]
if
len
(
otherurl
)
>
1
{
plist
:=
strings
.
Split
(
otherurl
,
"/"
)
for
k
,
v
:=
range
plist
[
1
:
]
{
params
[
strconv
.
Itoa
(
k
)]
=
v
}
}
//Invoke the request handler
vc
:=
reflect
.
New
(
controllerType
)
vc
:=
reflect
.
New
(
runrouter
)
//call the controller init function
init
:=
vc
.
MethodByName
(
"Init"
)
in
:=
make
([]
reflect
.
Value
,
3
)
method
:=
vc
.
MethodByName
(
"Init"
)
in
:=
make
([]
reflect
.
Value
,
4
)
in
[
0
]
=
reflect
.
ValueOf
(
context
)
in
[
1
]
=
reflect
.
ValueOf
(
controllerType
.
Name
())
in
[
2
]
=
reflect
.
ValueOf
(
vc
.
Interface
())
init
.
Call
(
in
)
//call prepare function
in
=
make
([]
reflect
.
Value
,
0
)
method
:=
vc
.
MethodByName
(
"Prepare"
)
method
.
Call
(
in
)
method
=
vc
.
MethodByName
(
mName
)
in
[
1
]
=
reflect
.
ValueOf
(
runrouter
.
Name
())
in
[
2
]
=
reflect
.
ValueOf
(
runMethod
)
in
[
3
]
=
reflect
.
ValueOf
(
vc
.
Interface
())
method
.
Call
(
in
)
//if XSRF is Enable then check cookie where there has any cookie in the request's cookie _csrf
if
EnableXSRF
{
in
=
make
([]
reflect
.
Value
,
0
)
method
=
vc
.
MethodByName
(
"XsrfToken"
)
method
.
Call
(
in
)
if
r
.
Method
==
"POST"
||
r
.
Method
==
"DELETE"
||
r
.
Method
==
"PUT"
||
...
...
@@ -826,12 +683,27 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
method
.
Call
(
in
)
}
}
//call prepare function
in
=
make
([]
reflect
.
Value
,
0
)
method
=
vc
.
MethodByName
(
"Prepare"
)
method
.
Call
(
in
)
if
!
w
.
started
{
//exec main logic
method
=
vc
.
MethodByName
(
runMethod
)
method
.
Call
(
in
)
//render template
if
!
w
.
started
&&
!
context
.
Input
.
IsWebsocket
()
{
if
AutoRender
{
method
=
vc
.
MethodByName
(
"Render"
)
callMethodWithError
(
method
,
in
)
}
}
}
// finish all runrouter. release resource
method
=
vc
.
MethodByName
(
"Finish"
)
method
.
Call
(
in
)
...
...
@@ -839,18 +711,6 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
if
do_filter
(
AfterExec
)
{
goto
Admin
}
goto
Admin
}
}
}
}
}
}
//if no matches to url, throw a not found exception
if
!
findrouter
{
middleware
.
Exception
(
"404"
,
rw
,
r
,
""
)
}
Admin
:
...
...
@@ -861,7 +721,7 @@ Admin:
timeend
:=
time
.
Since
(
starttime
)
if
FilterMonitorFunc
(
r
.
Method
,
requestPath
,
timeend
)
{
if
runrouter
!=
nil
{
go
toolbox
.
StatisticsMap
.
AddStatistics
(
r
.
Method
,
requestPath
,
runrouter
.
controllerType
.
Name
(),
timeend
)
go
toolbox
.
StatisticsMap
.
AddStatistics
(
r
.
Method
,
requestPath
,
runrouter
.
Name
(),
timeend
)
}
else
{
go
toolbox
.
StatisticsMap
.
AddStatistics
(
r
.
Method
,
requestPath
,
""
,
timeend
)
}
...
...
@@ -887,6 +747,21 @@ func (p *ControllerRegistor) getErrorHandler(errorCode string) func(rw http.Resp
return
handler
}
func
(
p
*
ControllerRegistor
)
getRunMethod
(
method
string
,
router
*
controllerInfo
)
string
{
method
=
strings
.
ToLower
(
method
)
if
router
.
hasMethod
{
if
m
,
ok
:=
router
.
methods
[
method
];
ok
{
return
m
}
else
if
m
,
ok
=
router
.
methods
[
"*"
];
ok
{
return
m
}
else
{
return
strings
.
Title
(
method
)
}
}
else
{
return
strings
.
Title
(
method
)
}
}
//responseWriter is a wrapper for the http.ResponseWriter
//started set to true if response was written to then don't execute other handler
type
responseWriter
struct
{
...
...
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