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
61570ac2
Commit
61570ac2
authored
Sep 08, 2015
by
astaxie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
make golint happy with controller.go
parent
f28a941e
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
80 additions
and
86 deletions
+80
-86
controller.go
controller.go
+64
-68
router.go
router.go
+1
-1
template.go
template.go
+1
-1
templatefunc.go
templatefunc.go
+14
-16
No files found.
controller.go
View file @
61570ac2
...
...
@@ -34,18 +34,19 @@ import (
//commonly used mime-types
const
(
applicationJ
son
=
"application/json"
applicationX
ml
=
"application/xml"
textX
ml
=
"text/xml"
applicationJ
SON
=
"application/json"
applicationX
ML
=
"application/xml"
textX
ML
=
"text/xml"
)
var
(
// custom error when user stop request handler manually.
USERSTOPRUN
=
errors
.
New
(
"User stop run"
)
GlobalControllerRouter
map
[
string
][]
ControllerComments
=
make
(
map
[
string
][]
ControllerComments
)
//pkgpath+controller:comments
// ErrAbort custom error when user stop request handler manually.
ErrAbort
=
errors
.
New
(
"User stop run"
)
// GlobalControllerRouter store comments with controller. pkgpath+controller:comments
GlobalControllerRouter
=
make
(
map
[
string
][]
ControllerComments
)
)
// store the comment for the controller method
//
ControllerComments
store the comment for the controller method
type
ControllerComments
struct
{
Method
string
Router
string
...
...
@@ -64,7 +65,7 @@ type Controller struct {
Layout
string
LayoutSections
map
[
string
]
string
// the key is the section name and the value is the template name
TplExt
string
_xsrf
_token
string
_xsrf
Token
string
gotofunc
string
CruSession
session
.
SessionStore
XSRFExpire
int
...
...
@@ -153,20 +154,20 @@ func (c *Controller) Options() {
http
.
Error
(
c
.
Ctx
.
ResponseWriter
,
"Method Not Allowed"
,
405
)
}
//
call function fn
//
HandlerFunc call function with the name
func
(
c
*
Controller
)
HandlerFunc
(
fnname
string
)
bool
{
if
v
,
ok
:=
c
.
methodMapping
[
fnname
];
ok
{
v
()
return
true
}
else
{
return
false
}
return
false
}
// URLMapping register the internal Controller router.
func
(
c
*
Controller
)
URLMapping
()
{
}
// Mapping the method to function
func
(
c
*
Controller
)
Mapping
(
method
string
,
fn
func
())
{
c
.
methodMapping
[
method
]
=
fn
}
...
...
@@ -177,13 +178,11 @@ func (c *Controller) Render() error {
return
nil
}
rb
,
err
:=
c
.
RenderBytes
()
if
err
!=
nil
{
return
err
}
else
{
c
.
Ctx
.
Output
.
Header
(
"Content-Type"
,
"text/html; charset=utf-8"
)
c
.
Ctx
.
Output
.
Body
(
rb
)
}
c
.
Ctx
.
Output
.
Header
(
"Content-Type"
,
"text/html; charset=utf-8"
)
c
.
Ctx
.
Output
.
Body
(
rb
)
return
nil
}
...
...
@@ -200,7 +199,7 @@ func (c *Controller) RenderBytes() ([]byte, error) {
if
c
.
TplNames
==
""
{
c
.
TplNames
=
strings
.
ToLower
(
c
.
controllerName
)
+
"/"
+
strings
.
ToLower
(
c
.
actionName
)
+
"."
+
c
.
TplExt
}
if
RunMode
==
"dev"
{
buildFiles
:=
make
([]
string
,
1
)
buildFiles
=
append
(
buildFiles
,
c
.
TplNames
)
...
...
@@ -252,25 +251,25 @@ func (c *Controller) RenderBytes() ([]byte, error) {
}
icontent
,
_
:=
ioutil
.
ReadAll
(
ibytes
)
return
icontent
,
nil
}
else
{
if
c
.
TplNames
==
""
{
c
.
TplNames
=
strings
.
ToLower
(
c
.
controllerName
)
+
"/"
+
strings
.
ToLower
(
c
.
actionName
)
+
"."
+
c
.
TplExt
}
if
RunMode
==
"dev"
{
BuildTemplate
(
ViewsPath
,
c
.
TplNames
)
}
ibytes
:=
bytes
.
NewBufferString
(
""
)
if
_
,
ok
:=
BeeTemplates
[
c
.
TplNames
];
!
ok
{
panic
(
"can't find templatefile in the path:"
+
c
.
TplNames
)
}
err
:=
BeeTemplates
[
c
.
TplNames
]
.
ExecuteTemplate
(
ibytes
,
c
.
TplNames
,
c
.
Data
)
if
err
!=
nil
{
Trace
(
"template Execute err:"
,
err
)
return
nil
,
err
}
icontent
,
_
:=
ioutil
.
ReadAll
(
ibytes
)
return
icontent
,
nil
}
if
c
.
TplNames
==
""
{
c
.
TplNames
=
strings
.
ToLower
(
c
.
controllerName
)
+
"/"
+
strings
.
ToLower
(
c
.
actionName
)
+
"."
+
c
.
TplExt
}
if
RunMode
==
"dev"
{
BuildTemplate
(
ViewsPath
,
c
.
TplNames
)
}
ibytes
:=
bytes
.
NewBufferString
(
""
)
if
_
,
ok
:=
BeeTemplates
[
c
.
TplNames
];
!
ok
{
panic
(
"can't find templatefile in the path:"
+
c
.
TplNames
)
}
err
:=
BeeTemplates
[
c
.
TplNames
]
.
ExecuteTemplate
(
ibytes
,
c
.
TplNames
,
c
.
Data
)
if
err
!=
nil
{
Trace
(
"template Execute err:"
,
err
)
return
nil
,
err
}
icontent
,
_
:=
ioutil
.
ReadAll
(
ibytes
)
return
icontent
,
nil
}
// Redirect sends the redirection response to url with status code.
...
...
@@ -278,7 +277,7 @@ func (c *Controller) Redirect(url string, code int) {
c
.
Ctx
.
Redirect
(
code
,
url
)
}
// Abort
s
stops controller handler and show the error data if code is defined in ErrorMap or code string.
// Abort stops controller handler and show the error data if code is defined in ErrorMap or code string.
func
(
c
*
Controller
)
Abort
(
code
string
)
{
status
,
err
:=
strconv
.
Atoi
(
code
)
if
err
!=
nil
{
...
...
@@ -296,29 +295,28 @@ func (c *Controller) CustomAbort(status int, body string) {
}
// last panic user string
c
.
Ctx
.
ResponseWriter
.
Write
([]
byte
(
body
))
panic
(
USERSTOPRUN
)
panic
(
ErrAbort
)
}
// StopRun makes panic of USERSTOPRUN error and go to recover function if defined.
func
(
c
*
Controller
)
StopRun
()
{
panic
(
USERSTOPRUN
)
panic
(
ErrAbort
)
}
// U
rl
For does another controller handler in this request function.
// U
RL
For does another controller handler in this request function.
// it goes to this controller method if endpoint is not clear.
func
(
c
*
Controller
)
U
rl
For
(
endpoint
string
,
values
...
interface
{})
string
{
func
(
c
*
Controller
)
U
RL
For
(
endpoint
string
,
values
...
interface
{})
string
{
if
len
(
endpoint
)
<=
0
{
return
""
}
if
endpoint
[
0
]
==
'.'
{
return
UrlFor
(
reflect
.
Indirect
(
reflect
.
ValueOf
(
c
.
AppController
))
.
Type
()
.
Name
()
+
endpoint
,
values
...
)
}
else
{
return
UrlFor
(
endpoint
,
values
...
)
return
URLFor
(
reflect
.
Indirect
(
reflect
.
ValueOf
(
c
.
AppController
))
.
Type
()
.
Name
()
+
endpoint
,
values
...
)
}
return
URLFor
(
endpoint
,
values
...
)
}
// ServeJ
son
sends a json response with encoding charset.
func
(
c
*
Controller
)
ServeJ
son
(
encoding
...
bool
)
{
// ServeJ
SON
sends a json response with encoding charset.
func
(
c
*
Controller
)
ServeJ
SON
(
encoding
...
bool
)
{
var
hasIndent
bool
var
hasencoding
bool
if
RunMode
==
"prod"
{
...
...
@@ -332,8 +330,8 @@ func (c *Controller) ServeJson(encoding ...bool) {
c
.
Ctx
.
Output
.
Json
(
c
.
Data
[
"json"
],
hasIndent
,
hasencoding
)
}
// ServeJ
sonp
sends a jsonp response.
func
(
c
*
Controller
)
ServeJ
sonp
()
{
// ServeJ
SONP
sends a jsonp response.
func
(
c
*
Controller
)
ServeJ
SONP
()
{
var
hasIndent
bool
if
RunMode
==
"prod"
{
hasIndent
=
false
...
...
@@ -343,8 +341,8 @@ func (c *Controller) ServeJsonp() {
c
.
Ctx
.
Output
.
Jsonp
(
c
.
Data
[
"jsonp"
],
hasIndent
)
}
// ServeX
ml
sends xml response.
func
(
c
*
Controller
)
ServeX
ml
()
{
// ServeX
ML
sends xml response.
func
(
c
*
Controller
)
ServeX
ML
()
{
var
hasIndent
bool
if
RunMode
==
"prod"
{
hasIndent
=
false
...
...
@@ -358,12 +356,12 @@ func (c *Controller) ServeXml() {
func
(
c
*
Controller
)
ServeFormatted
()
{
accept
:=
c
.
Ctx
.
Input
.
Header
(
"Accept"
)
switch
accept
{
case
applicationJ
son
:
c
.
ServeJ
son
()
case
applicationX
ml
,
textXml
:
c
.
ServeX
ml
()
case
applicationJ
SON
:
c
.
ServeJ
SON
()
case
applicationX
ML
,
textXML
:
c
.
ServeX
ML
()
default
:
c
.
ServeJ
son
()
c
.
ServeJ
SON
()
}
}
...
...
@@ -389,9 +387,8 @@ func (c *Controller) GetString(key string, def ...string) string {
if
v
:=
c
.
Ctx
.
Input
.
Query
(
key
);
v
!=
""
{
return
v
}
else
{
return
defv
}
return
defv
}
// GetStrings returns the input string slice by key string or the default value while it's present and input is blank
...
...
@@ -410,9 +407,8 @@ func (c *Controller) GetStrings(key string, def ...[]string) []string {
vs
:=
f
[
key
]
if
len
(
vs
)
>
0
{
return
vs
}
else
{
return
defv
}
return
defv
}
// GetInt returns input as an int or the default value while it's present and input is blank
...
...
@@ -586,7 +582,7 @@ func (c *Controller) GetSession(name interface{}) interface{} {
return
c
.
CruSession
.
Get
(
name
)
}
//
Set
Session removes value from session.
//
Del
Session removes value from session.
func
(
c
*
Controller
)
DelSession
(
name
interface
{})
{
if
c
.
CruSession
==
nil
{
c
.
StartSession
()
...
...
@@ -625,34 +621,34 @@ func (c *Controller) SetSecureCookie(Secret, name, value string, others ...inter
c
.
Ctx
.
SetSecureCookie
(
Secret
,
name
,
value
,
others
...
)
}
// X
srfToken creates a xsrf
token string and returns.
func
(
c
*
Controller
)
X
srf
Token
()
string
{
if
c
.
_xsrf
_t
oken
==
""
{
// X
SRFToken creates a CSRF
token string and returns.
func
(
c
*
Controller
)
X
SRF
Token
()
string
{
if
c
.
_xsrf
T
oken
==
""
{
var
expire
int64
if
c
.
XSRFExpire
>
0
{
expire
=
int64
(
c
.
XSRFExpire
)
}
else
{
expire
=
int64
(
XSRFExpire
)
}
c
.
_xsrf
_t
oken
=
c
.
Ctx
.
XsrfToken
(
XSRFKEY
,
expire
)
c
.
_xsrf
T
oken
=
c
.
Ctx
.
XsrfToken
(
XSRFKEY
,
expire
)
}
return
c
.
_xsrf
_t
oken
return
c
.
_xsrf
T
oken
}
// CheckX
srf
Cookie checks xsrf token in this request is valid or not.
// CheckX
SRF
Cookie checks xsrf token in this request is valid or not.
// the token can provided in request header "X-Xsrftoken" and "X-CsrfToken"
// or in form field value named as "_xsrf".
func
(
c
*
Controller
)
CheckX
srf
Cookie
()
bool
{
func
(
c
*
Controller
)
CheckX
SRF
Cookie
()
bool
{
if
!
c
.
EnableXSRF
{
return
true
}
return
c
.
Ctx
.
CheckXsrfCookie
()
}
// X
srfFormHtml
writes an input field contains xsrf token value.
func
(
c
*
Controller
)
X
srfFormHtml
()
string
{
// X
SRFFormHTML
writes an input field contains xsrf token value.
func
(
c
*
Controller
)
X
SRFFormHTML
()
string
{
return
"<input type=
\"
hidden
\"
name=
\"
_xsrf
\"
value=
\"
"
+
c
.
_xsrf
_t
oken
+
"
\"
/>"
c
.
_xsrf
T
oken
+
"
\"
/>"
}
// GetControllerAndAction gets the executing controller name and action name.
...
...
router.go
View file @
61570ac2
...
...
@@ -863,7 +863,7 @@ Admin:
func
(
p
*
ControllerRegister
)
recoverPanic
(
context
*
beecontext
.
Context
)
{
if
err
:=
recover
();
err
!=
nil
{
if
err
==
USERSTOPRUN
{
if
err
==
ErrAbort
{
return
}
if
!
RecoverPanic
{
...
...
template.go
View file @
61570ac2
...
...
@@ -65,7 +65,7 @@ func init() {
beegoTplFuncMap
[
"lt"
]
=
lt
// <
beegoTplFuncMap
[
"ne"
]
=
ne
// !=
beegoTplFuncMap
[
"urlfor"
]
=
U
rl
For
// !=
beegoTplFuncMap
[
"urlfor"
]
=
U
RL
For
// !=
}
// AddFuncMap let user to register a func in the template.
...
...
templatefunc.go
View file @
61570ac2
...
...
@@ -140,11 +140,11 @@ func Compare(a, b interface{}) (equal bool) {
}
func
CompareNot
(
a
,
b
interface
{})
(
equal
bool
)
{
return
!
Compare
(
a
,
b
)
return
!
Compare
(
a
,
b
)
}
func
NotNil
(
a
interface
{})
(
is_nil
bool
)
{
return
CompareNot
(
a
,
nil
)
return
CompareNot
(
a
,
nil
)
}
func
Config
(
returnType
,
key
string
,
defaultVal
interface
{})
(
value
interface
{},
err
error
)
{
...
...
@@ -237,14 +237,14 @@ func Htmlunquote(src string) string {
return
strings
.
TrimSpace
(
text
)
}
// U
rl
For returns url string with another registered controller handler with params.
// U
RL
For returns url string with another registered controller handler with params.
// usage:
//
// U
rl
For(".index")
// print U
rl
For("index")
// U
RL
For(".index")
// print U
RL
For("index")
// router /login
// print U
rl
For("login")
// print U
rl
For("login", "next","/"")
// print U
RL
For("login")
// print U
RL
For("login", "next","/"")
// router /profile/:username
// print UrlFor("profile", ":username","John Doe")
// result:
...
...
@@ -254,7 +254,7 @@ func Htmlunquote(src string) string {
// /user/John%20Doe
//
// more detail http://beego.me/docs/mvc/controller/urlbuilding.md
func
U
rl
For
(
endpoint
string
,
values
...
interface
{})
string
{
func
U
RL
For
(
endpoint
string
,
values
...
interface
{})
string
{
return
BeeApp
.
Handlers
.
UrlFor
(
endpoint
,
values
...
)
}
...
...
@@ -698,22 +698,21 @@ func MapGet(arg1 interface{}, arg2 ...interface{}) (interface{}, error) {
storedVal
:=
arg1Val
.
MapIndex
(
arg2Val
)
if
storedVal
.
IsValid
()
{
var
result
interface
{}
switch
arg1Type
.
Elem
()
.
Kind
()
{
case
reflect
.
Bool
:
case
reflect
.
Bool
:
result
=
storedVal
.
Bool
()
case
reflect
.
Int
,
reflect
.
Int8
,
reflect
.
Int16
,
reflect
.
Int32
,
reflect
.
Int64
:
case
reflect
.
Int
,
reflect
.
Int8
,
reflect
.
Int16
,
reflect
.
Int32
,
reflect
.
Int64
:
result
=
storedVal
.
Int
()
case
reflect
.
Uint
,
reflect
.
Uint8
,
reflect
.
Uint16
,
reflect
.
Uint32
,
reflect
.
Uint64
,
reflect
.
Uintptr
:
case
reflect
.
Uint
,
reflect
.
Uint8
,
reflect
.
Uint16
,
reflect
.
Uint32
,
reflect
.
Uint64
,
reflect
.
Uintptr
:
result
=
storedVal
.
Uint
()
case
reflect
.
Float32
,
reflect
.
Float64
:
case
reflect
.
Float32
,
reflect
.
Float64
:
result
=
storedVal
.
Float
()
case
reflect
.
String
:
case
reflect
.
String
:
result
=
storedVal
.
String
()
default
:
default
:
result
=
storedVal
.
Interface
()
}
...
...
@@ -727,7 +726,6 @@ func MapGet(arg1 interface{}, arg2 ...interface{}) (interface{}, error) {
return
nil
,
nil
}
}
else
{
return
nil
,
nil
}
...
...
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