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
42fab96c
Commit
42fab96c
authored
Jul 28, 2015
by
astaxie
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1285 from JessonChan/beego_develop
typo fixed
parents
9775e3e3
b26ef5b2
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
49 additions
and
49 deletions
+49
-49
app.go
app.go
+1
-1
namespace.go
namespace.go
+19
-19
router.go
router.go
+29
-29
No files found.
app.go
View file @
42fab96c
...
...
@@ -28,7 +28,7 @@ import (
// App defines beego application with a new PatternServeMux.
type
App
struct
{
Handlers
*
ControllerRegist
o
r
Handlers
*
ControllerRegist
e
r
Server
*
http
.
Server
}
...
...
namespace.go
View file @
42fab96c
...
...
@@ -23,16 +23,16 @@ import (
type
namespaceCond
func
(
*
beecontext
.
Context
)
bool
type
inn
n
erNamespace
func
(
*
Namespace
)
type
innerNamespace
func
(
*
Namespace
)
// Namespace is store all the info
type
Namespace
struct
{
prefix
string
handlers
*
ControllerRegist
o
r
handlers
*
ControllerRegist
e
r
}
// get new Namespace
func
NewNamespace
(
prefix
string
,
params
...
inn
n
erNamespace
)
*
Namespace
{
func
NewNamespace
(
prefix
string
,
params
...
innerNamespace
)
*
Namespace
{
ns
:=
&
Namespace
{
prefix
:
prefix
,
handlers
:
NewControllerRegister
(),
...
...
@@ -276,112 +276,112 @@ func addPrefix(t *Tree, prefix string) {
}
// Namespace Condition
func
NSCond
(
cond
namespaceCond
)
inn
n
erNamespace
{
func
NSCond
(
cond
namespaceCond
)
innerNamespace
{
return
func
(
ns
*
Namespace
)
{
ns
.
Cond
(
cond
)
}
}
// Namespace BeforeRouter filter
func
NSBefore
(
filiterList
...
FilterFunc
)
inn
n
erNamespace
{
func
NSBefore
(
filiterList
...
FilterFunc
)
innerNamespace
{
return
func
(
ns
*
Namespace
)
{
ns
.
Filter
(
"before"
,
filiterList
...
)
}
}
// Namespace FinishRouter filter
func
NSAfter
(
filiterList
...
FilterFunc
)
inn
n
erNamespace
{
func
NSAfter
(
filiterList
...
FilterFunc
)
innerNamespace
{
return
func
(
ns
*
Namespace
)
{
ns
.
Filter
(
"after"
,
filiterList
...
)
}
}
// Namespace Include ControllerInterface
func
NSInclude
(
cList
...
ControllerInterface
)
inn
n
erNamespace
{
func
NSInclude
(
cList
...
ControllerInterface
)
innerNamespace
{
return
func
(
ns
*
Namespace
)
{
ns
.
Include
(
cList
...
)
}
}
// Namespace Router
func
NSRouter
(
rootpath
string
,
c
ControllerInterface
,
mappingMethods
...
string
)
inn
n
erNamespace
{
func
NSRouter
(
rootpath
string
,
c
ControllerInterface
,
mappingMethods
...
string
)
innerNamespace
{
return
func
(
ns
*
Namespace
)
{
ns
.
Router
(
rootpath
,
c
,
mappingMethods
...
)
}
}
// Namespace Get
func
NSGet
(
rootpath
string
,
f
FilterFunc
)
inn
n
erNamespace
{
func
NSGet
(
rootpath
string
,
f
FilterFunc
)
innerNamespace
{
return
func
(
ns
*
Namespace
)
{
ns
.
Get
(
rootpath
,
f
)
}
}
// Namespace Post
func
NSPost
(
rootpath
string
,
f
FilterFunc
)
inn
n
erNamespace
{
func
NSPost
(
rootpath
string
,
f
FilterFunc
)
innerNamespace
{
return
func
(
ns
*
Namespace
)
{
ns
.
Post
(
rootpath
,
f
)
}
}
// Namespace Head
func
NSHead
(
rootpath
string
,
f
FilterFunc
)
inn
n
erNamespace
{
func
NSHead
(
rootpath
string
,
f
FilterFunc
)
innerNamespace
{
return
func
(
ns
*
Namespace
)
{
ns
.
Head
(
rootpath
,
f
)
}
}
// Namespace Put
func
NSPut
(
rootpath
string
,
f
FilterFunc
)
inn
n
erNamespace
{
func
NSPut
(
rootpath
string
,
f
FilterFunc
)
innerNamespace
{
return
func
(
ns
*
Namespace
)
{
ns
.
Put
(
rootpath
,
f
)
}
}
// Namespace Delete
func
NSDelete
(
rootpath
string
,
f
FilterFunc
)
inn
n
erNamespace
{
func
NSDelete
(
rootpath
string
,
f
FilterFunc
)
innerNamespace
{
return
func
(
ns
*
Namespace
)
{
ns
.
Delete
(
rootpath
,
f
)
}
}
// Namespace Any
func
NSAny
(
rootpath
string
,
f
FilterFunc
)
inn
n
erNamespace
{
func
NSAny
(
rootpath
string
,
f
FilterFunc
)
innerNamespace
{
return
func
(
ns
*
Namespace
)
{
ns
.
Any
(
rootpath
,
f
)
}
}
// Namespace Options
func
NSOptions
(
rootpath
string
,
f
FilterFunc
)
inn
n
erNamespace
{
func
NSOptions
(
rootpath
string
,
f
FilterFunc
)
innerNamespace
{
return
func
(
ns
*
Namespace
)
{
ns
.
Options
(
rootpath
,
f
)
}
}
// Namespace Patch
func
NSPatch
(
rootpath
string
,
f
FilterFunc
)
inn
n
erNamespace
{
func
NSPatch
(
rootpath
string
,
f
FilterFunc
)
innerNamespace
{
return
func
(
ns
*
Namespace
)
{
ns
.
Patch
(
rootpath
,
f
)
}
}
//Namespace AutoRouter
func
NSAutoRouter
(
c
ControllerInterface
)
inn
n
erNamespace
{
func
NSAutoRouter
(
c
ControllerInterface
)
innerNamespace
{
return
func
(
ns
*
Namespace
)
{
ns
.
AutoRouter
(
c
)
}
}
// Namespace AutoPrefix
func
NSAutoPrefix
(
prefix
string
,
c
ControllerInterface
)
inn
n
erNamespace
{
func
NSAutoPrefix
(
prefix
string
,
c
ControllerInterface
)
innerNamespace
{
return
func
(
ns
*
Namespace
)
{
ns
.
AutoPrefix
(
prefix
,
c
)
}
}
// Namespace add sub Namespace
func
NSNamespace
(
prefix
string
,
params
...
inn
nerNamespace
)
in
nnerNamespace
{
func
NSNamespace
(
prefix
string
,
params
...
inn
erNamespace
)
i
nnerNamespace
{
return
func
(
ns
*
Namespace
)
{
n
:=
NewNamespace
(
prefix
,
params
...
)
ns
.
Namespace
(
n
)
...
...
router.go
View file @
42fab96c
...
...
@@ -110,22 +110,22 @@ type controllerInfo struct {
routerType
int
}
// ControllerRegist
o
r containers registered router rules, controller handlers and filters.
type
ControllerRegist
o
r
struct
{
// ControllerRegist
e
r containers registered router rules, controller handlers and filters.
type
ControllerRegist
e
r
struct
{
routers
map
[
string
]
*
Tree
enableFilter
bool
filters
map
[
int
][]
*
FilterRouter
}
// NewControllerRegister returns a new ControllerRegist
o
r.
func
NewControllerRegister
()
*
ControllerRegist
o
r
{
return
&
ControllerRegist
o
r
{
// NewControllerRegister returns a new ControllerRegist
e
r.
func
NewControllerRegister
()
*
ControllerRegist
e
r
{
return
&
ControllerRegist
e
r
{
routers
:
make
(
map
[
string
]
*
Tree
),
filters
:
make
(
map
[
int
][]
*
FilterRouter
),
}
}
// Add controller handler and pattern rules to ControllerRegist
o
r.
// Add controller handler and pattern rules to ControllerRegist
e
r.
// usage:
// default methods is the same name as method
// Add("/user",&UserController{})
...
...
@@ -135,7 +135,7 @@ func NewControllerRegister() *ControllerRegistor {
// Add("/api/delete",&RestController{},"delete:DeleteFood")
// Add("/api",&RestController{},"get,post:ApiFunc")
// Add("/simple",&SimpleController{},"get:GetFunc;post:PostFunc")
func
(
p
*
ControllerRegist
o
r
)
Add
(
pattern
string
,
c
ControllerInterface
,
mappingMethods
...
string
)
{
func
(
p
*
ControllerRegist
e
r
)
Add
(
pattern
string
,
c
ControllerInterface
,
mappingMethods
...
string
)
{
reflectVal
:=
reflect
.
ValueOf
(
c
)
t
:=
reflect
.
Indirect
(
reflectVal
)
.
Type
()
methods
:=
make
(
map
[
string
]
string
)
...
...
@@ -183,7 +183,7 @@ func (p *ControllerRegistor) Add(pattern string, c ControllerInterface, mappingM
}
}
func
(
p
*
ControllerRegist
o
r
)
addToRouter
(
method
,
pattern
string
,
r
*
controllerInfo
)
{
func
(
p
*
ControllerRegist
e
r
)
addToRouter
(
method
,
pattern
string
,
r
*
controllerInfo
)
{
if
!
RouterCaseSensitive
{
pattern
=
strings
.
ToLower
(
pattern
)
}
...
...
@@ -198,7 +198,7 @@ func (p *ControllerRegistor) addToRouter(method, pattern string, r *controllerIn
// only when the Runmode is dev will generate router file in the router/auto.go from the controller
// Include(&BankAccount{}, &OrderController{},&RefundController{},&ReceiptController{})
func
(
p
*
ControllerRegist
o
r
)
Include
(
cList
...
ControllerInterface
)
{
func
(
p
*
ControllerRegist
e
r
)
Include
(
cList
...
ControllerInterface
)
{
if
RunMode
==
"dev"
{
skip
:=
make
(
map
[
string
]
bool
,
10
)
for
_
,
c
:=
range
cList
{
...
...
@@ -243,7 +243,7 @@ func (p *ControllerRegistor) Include(cList ...ControllerInterface) {
// Get("/", func(ctx *context.Context){
// ctx.Output.Body("hello world")
// })
func
(
p
*
ControllerRegist
o
r
)
Get
(
pattern
string
,
f
FilterFunc
)
{
func
(
p
*
ControllerRegist
e
r
)
Get
(
pattern
string
,
f
FilterFunc
)
{
p
.
AddMethod
(
"get"
,
pattern
,
f
)
}
...
...
@@ -252,7 +252,7 @@ func (p *ControllerRegistor) Get(pattern string, f FilterFunc) {
// Post("/api", func(ctx *context.Context){
// ctx.Output.Body("hello world")
// })
func
(
p
*
ControllerRegist
o
r
)
Post
(
pattern
string
,
f
FilterFunc
)
{
func
(
p
*
ControllerRegist
e
r
)
Post
(
pattern
string
,
f
FilterFunc
)
{
p
.
AddMethod
(
"post"
,
pattern
,
f
)
}
...
...
@@ -261,7 +261,7 @@ func (p *ControllerRegistor) Post(pattern string, f FilterFunc) {
// Put("/api/:id", func(ctx *context.Context){
// ctx.Output.Body("hello world")
// })
func
(
p
*
ControllerRegist
o
r
)
Put
(
pattern
string
,
f
FilterFunc
)
{
func
(
p
*
ControllerRegist
e
r
)
Put
(
pattern
string
,
f
FilterFunc
)
{
p
.
AddMethod
(
"put"
,
pattern
,
f
)
}
...
...
@@ -270,7 +270,7 @@ func (p *ControllerRegistor) Put(pattern string, f FilterFunc) {
// Delete("/api/:id", func(ctx *context.Context){
// ctx.Output.Body("hello world")
// })
func
(
p
*
ControllerRegist
o
r
)
Delete
(
pattern
string
,
f
FilterFunc
)
{
func
(
p
*
ControllerRegist
e
r
)
Delete
(
pattern
string
,
f
FilterFunc
)
{
p
.
AddMethod
(
"delete"
,
pattern
,
f
)
}
...
...
@@ -279,7 +279,7 @@ func (p *ControllerRegistor) Delete(pattern string, f FilterFunc) {
// Head("/api/:id", func(ctx *context.Context){
// ctx.Output.Body("hello world")
// })
func
(
p
*
ControllerRegist
o
r
)
Head
(
pattern
string
,
f
FilterFunc
)
{
func
(
p
*
ControllerRegist
e
r
)
Head
(
pattern
string
,
f
FilterFunc
)
{
p
.
AddMethod
(
"head"
,
pattern
,
f
)
}
...
...
@@ -288,7 +288,7 @@ func (p *ControllerRegistor) Head(pattern string, f FilterFunc) {
// Patch("/api/:id", func(ctx *context.Context){
// ctx.Output.Body("hello world")
// })
func
(
p
*
ControllerRegist
o
r
)
Patch
(
pattern
string
,
f
FilterFunc
)
{
func
(
p
*
ControllerRegist
e
r
)
Patch
(
pattern
string
,
f
FilterFunc
)
{
p
.
AddMethod
(
"patch"
,
pattern
,
f
)
}
...
...
@@ -297,7 +297,7 @@ func (p *ControllerRegistor) Patch(pattern string, f FilterFunc) {
// Options("/api/:id", func(ctx *context.Context){
// ctx.Output.Body("hello world")
// })
func
(
p
*
ControllerRegist
o
r
)
Options
(
pattern
string
,
f
FilterFunc
)
{
func
(
p
*
ControllerRegist
e
r
)
Options
(
pattern
string
,
f
FilterFunc
)
{
p
.
AddMethod
(
"options"
,
pattern
,
f
)
}
...
...
@@ -306,7 +306,7 @@ func (p *ControllerRegistor) Options(pattern string, f FilterFunc) {
// Any("/api/:id", func(ctx *context.Context){
// ctx.Output.Body("hello world")
// })
func
(
p
*
ControllerRegist
o
r
)
Any
(
pattern
string
,
f
FilterFunc
)
{
func
(
p
*
ControllerRegist
e
r
)
Any
(
pattern
string
,
f
FilterFunc
)
{
p
.
AddMethod
(
"*"
,
pattern
,
f
)
}
...
...
@@ -315,7 +315,7 @@ func (p *ControllerRegistor) Any(pattern string, f FilterFunc) {
// AddMethod("get","/api/:id", func(ctx *context.Context){
// ctx.Output.Body("hello world")
// })
func
(
p
*
ControllerRegist
o
r
)
AddMethod
(
method
,
pattern
string
,
f
FilterFunc
)
{
func
(
p
*
ControllerRegist
e
r
)
AddMethod
(
method
,
pattern
string
,
f
FilterFunc
)
{
if
_
,
ok
:=
HTTPMETHOD
[
strings
.
ToUpper
(
method
)];
method
!=
"*"
&&
!
ok
{
panic
(
"not support http method: "
+
method
)
}
...
...
@@ -344,7 +344,7 @@ func (p *ControllerRegistor) AddMethod(method, pattern string, f FilterFunc) {
}
// add user defined Handler
func
(
p
*
ControllerRegist
o
r
)
Handler
(
pattern
string
,
h
http
.
Handler
,
options
...
interface
{})
{
func
(
p
*
ControllerRegist
e
r
)
Handler
(
pattern
string
,
h
http
.
Handler
,
options
...
interface
{})
{
route
:=
&
controllerInfo
{}
route
.
pattern
=
pattern
route
.
routerType
=
routerTypeHandler
...
...
@@ -359,21 +359,21 @@ func (p *ControllerRegistor) Handler(pattern string, h http.Handler, options ...
}
}
// Add auto router to ControllerRegist
o
r.
// Add auto router to ControllerRegist
e
r.
// example beego.AddAuto(&MainContorlller{}),
// MainController has method List and Page.
// visit the url /main/list to execute List function
// /main/page to execute Page function.
func
(
p
*
ControllerRegist
o
r
)
AddAuto
(
c
ControllerInterface
)
{
func
(
p
*
ControllerRegist
e
r
)
AddAuto
(
c
ControllerInterface
)
{
p
.
AddAutoPrefix
(
"/"
,
c
)
}
// Add auto router to ControllerRegist
o
r with prefix.
// Add auto router to ControllerRegist
e
r with prefix.
// example beego.AddAutoPrefix("/admin",&MainContorlller{}),
// MainController has method List and Page.
// visit the url /admin/main/list to execute List function
// /admin/main/page to execute Page function.
func
(
p
*
ControllerRegist
o
r
)
AddAutoPrefix
(
prefix
string
,
c
ControllerInterface
)
{
func
(
p
*
ControllerRegist
e
r
)
AddAutoPrefix
(
prefix
string
,
c
ControllerInterface
)
{
reflectVal
:=
reflect
.
ValueOf
(
c
)
rt
:=
reflectVal
.
Type
()
ct
:=
reflect
.
Indirect
(
reflectVal
)
.
Type
()
...
...
@@ -401,7 +401,7 @@ func (p *ControllerRegistor) AddAutoPrefix(prefix string, c ControllerInterface)
// Add a FilterFunc with pattern rule and action constant.
// The bool params is for setting the returnOnOutput value (false allows multiple filters to execute)
func
(
p
*
ControllerRegist
o
r
)
InsertFilter
(
pattern
string
,
pos
int
,
filter
FilterFunc
,
params
...
bool
)
error
{
func
(
p
*
ControllerRegist
e
r
)
InsertFilter
(
pattern
string
,
pos
int
,
filter
FilterFunc
,
params
...
bool
)
error
{
mr
:=
new
(
FilterRouter
)
mr
.
tree
=
NewTree
()
...
...
@@ -420,7 +420,7 @@ func (p *ControllerRegistor) InsertFilter(pattern string, pos int, filter Filter
}
// add Filter into
func
(
p
*
ControllerRegist
o
r
)
insertFilterRouter
(
pos
int
,
mr
*
FilterRouter
)
error
{
func
(
p
*
ControllerRegist
e
r
)
insertFilterRouter
(
pos
int
,
mr
*
FilterRouter
)
error
{
p
.
filters
[
pos
]
=
append
(
p
.
filters
[
pos
],
mr
)
p
.
enableFilter
=
true
return
nil
...
...
@@ -428,7 +428,7 @@ func (p *ControllerRegistor) insertFilterRouter(pos int, mr *FilterRouter) error
// UrlFor does another controller handler in this request function.
// it can access any controller method.
func
(
p
*
ControllerRegist
o
r
)
UrlFor
(
endpoint
string
,
values
...
interface
{})
string
{
func
(
p
*
ControllerRegist
e
r
)
UrlFor
(
endpoint
string
,
values
...
interface
{})
string
{
paths
:=
strings
.
Split
(
endpoint
,
"."
)
if
len
(
paths
)
<=
1
{
Warn
(
"urlfor endpoint must like path.controller.method"
)
...
...
@@ -460,7 +460,7 @@ func (p *ControllerRegistor) UrlFor(endpoint string, values ...interface{}) stri
return
""
}
func
(
p
*
ControllerRegist
o
r
)
geturl
(
t
*
Tree
,
url
,
controllName
,
methodName
string
,
params
map
[
string
]
string
,
httpMethod
string
)
(
bool
,
string
)
{
func
(
p
*
ControllerRegist
e
r
)
geturl
(
t
*
Tree
,
url
,
controllName
,
methodName
string
,
params
map
[
string
]
string
,
httpMethod
string
)
(
bool
,
string
)
{
for
k
,
subtree
:=
range
t
.
fixrouters
{
u
:=
path
.
Join
(
url
,
k
)
ok
,
u
:=
p
.
geturl
(
subtree
,
u
,
controllName
,
methodName
,
params
,
httpMethod
)
...
...
@@ -575,7 +575,7 @@ func (p *ControllerRegistor) geturl(t *Tree, url, controllName, methodName strin
}
// Implement http.Handler interface.
func
(
p
*
ControllerRegist
o
r
)
ServeHTTP
(
rw
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
func
(
p
*
ControllerRegist
e
r
)
ServeHTTP
(
rw
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
starttime
:=
time
.
Now
()
var
runrouter
reflect
.
Type
var
findrouter
bool
...
...
@@ -861,7 +861,7 @@ Admin:
}
}
func
(
p
*
ControllerRegist
o
r
)
recoverPanic
(
context
*
beecontext
.
Context
)
{
func
(
p
*
ControllerRegist
e
r
)
recoverPanic
(
context
*
beecontext
.
Context
)
{
if
err
:=
recover
();
err
!=
nil
{
if
err
==
USERSTOPRUN
{
return
...
...
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