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
ad0d166d
Unverified
Commit
ad0d166d
authored
Sep 30, 2018
by
astaxie
Committed by
GitHub
Sep 30, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #3295 from GNURub/feature/outputWithformat
feature/outputwithformat
parents
2c2ace9a
755cc98e
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
67 additions
and
70 deletions
+67
-70
admin.go
admin.go
+13
-13
admin_test.go
admin_test.go
+3
-3
beego.go
beego.go
+4
-1
config_test.go
config_test.go
+4
-4
context.go
context/context.go
+8
-0
output.go
context/output.go
+14
-1
controller.go
controller.go
+13
-40
error.go
error.go
+1
-1
templatefunc.go
templatefunc.go
+1
-1
templatefunc_test.go
templatefunc_test.go
+6
-6
No files found.
admin.go
View file @
ad0d166d
...
...
@@ -67,18 +67,18 @@ func init() {
// AdminIndex is the default http.Handler for admin module.
// it matches url pattern "/".
func
adminIndex
(
rw
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
func
adminIndex
(
rw
http
.
ResponseWriter
,
_
*
http
.
Request
)
{
execTpl
(
rw
,
map
[
interface
{}]
interface
{}{},
indexTpl
,
defaultScriptsTpl
)
}
// QpsIndex is the http.Handler for writing qbs statistics map result info in http.ResponseWriter.
// it's registered with url pattern "/qbs" in admin module.
func
qpsIndex
(
rw
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
func
qpsIndex
(
rw
http
.
ResponseWriter
,
_
*
http
.
Request
)
{
data
:=
make
(
map
[
interface
{}]
interface
{})
data
[
"Content"
]
=
toolbox
.
StatisticsMap
.
GetMap
()
// do html escape before display path, avoid xss
if
content
,
ok
:=
(
data
[
"Content"
])
.
(
map
[
string
]
interface
{}
);
ok
{
if
content
,
ok
:=
(
data
[
"Content"
])
.
(
M
);
ok
{
if
resultLists
,
ok
:=
(
content
[
"Data"
])
.
([][]
string
);
ok
{
for
i
:=
range
resultLists
{
if
len
(
resultLists
[
i
])
>
0
{
...
...
@@ -104,7 +104,7 @@ func listConf(rw http.ResponseWriter, r *http.Request) {
data
:=
make
(
map
[
interface
{}]
interface
{})
switch
command
{
case
"conf"
:
m
:=
make
(
map
[
string
]
interface
{}
)
m
:=
make
(
M
)
list
(
"BConfig"
,
BConfig
,
m
)
m
[
"AppConfigPath"
]
=
appConfigPath
m
[
"AppConfigProvider"
]
=
appConfigProvider
...
...
@@ -128,14 +128,14 @@ func listConf(rw http.ResponseWriter, r *http.Request) {
execTpl
(
rw
,
data
,
routerAndFilterTpl
,
defaultScriptsTpl
)
case
"filter"
:
var
(
content
=
map
[
string
]
interface
{}
{
content
=
M
{
"Fields"
:
[]
string
{
"Router Pattern"
,
"Filter Function"
,
},
}
filterTypes
=
[]
string
{}
filterTypeData
=
make
(
map
[
string
]
interface
{}
)
filterTypeData
=
make
(
M
)
)
if
BeeApp
.
Handlers
.
enableFilter
{
...
...
@@ -173,7 +173,7 @@ func listConf(rw http.ResponseWriter, r *http.Request) {
}
}
func
list
(
root
string
,
p
interface
{},
m
map
[
string
]
interface
{}
)
{
func
list
(
root
string
,
p
interface
{},
m
M
)
{
pt
:=
reflect
.
TypeOf
(
p
)
pv
:=
reflect
.
ValueOf
(
p
)
if
pt
.
Kind
()
==
reflect
.
Ptr
{
...
...
@@ -196,11 +196,11 @@ func list(root string, p interface{}, m map[string]interface{}) {
}
// PrintTree prints all registered routers.
func
PrintTree
()
map
[
string
]
interface
{}
{
func
PrintTree
()
M
{
var
(
content
=
map
[
string
]
interface
{}
{}
content
=
M
{}
methods
=
[]
string
{}
methodsData
=
make
(
map
[
string
]
interface
{}
)
methodsData
=
make
(
M
)
)
for
method
,
t
:=
range
BeeApp
.
Handlers
.
routers
{
...
...
@@ -291,12 +291,12 @@ func profIndex(rw http.ResponseWriter, r *http.Request) {
// Healthcheck is a http.Handler calling health checking and showing the result.
// it's in "/healthcheck" pattern in admin module.
func
healthcheck
(
rw
http
.
ResponseWriter
,
req
*
http
.
Request
)
{
func
healthcheck
(
rw
http
.
ResponseWriter
,
_
*
http
.
Request
)
{
var
(
result
[]
string
data
=
make
(
map
[
interface
{}]
interface
{})
resultList
=
new
([][]
string
)
content
=
map
[
string
]
interface
{}
{
content
=
M
{
"Fields"
:
[]
string
{
"Name"
,
"Message"
,
"Status"
},
}
)
...
...
@@ -344,7 +344,7 @@ func taskStatus(rw http.ResponseWriter, req *http.Request) {
}
// List Tasks
content
:=
make
(
map
[
string
]
interface
{}
)
content
:=
make
(
M
)
resultList
:=
new
([][]
string
)
var
fields
=
[]
string
{
"Task Name"
,
...
...
admin_test.go
View file @
ad0d166d
...
...
@@ -6,7 +6,7 @@ import (
)
func
TestList_01
(
t
*
testing
.
T
)
{
m
:=
make
(
map
[
string
]
interface
{}
)
m
:=
make
(
M
)
list
(
"BConfig"
,
BConfig
,
m
)
t
.
Log
(
m
)
om
:=
oldMap
()
...
...
@@ -18,8 +18,8 @@ func TestList_01(t *testing.T) {
}
}
func
oldMap
()
map
[
string
]
interface
{}
{
m
:=
make
(
map
[
string
]
interface
{}
)
func
oldMap
()
M
{
m
:=
make
(
M
)
m
[
"BConfig.AppName"
]
=
BConfig
.
AppName
m
[
"BConfig.RunMode"
]
=
BConfig
.
RunMode
m
[
"BConfig.RouterCaseSensitive"
]
=
BConfig
.
RouterCaseSensitive
...
...
beego.go
View file @
ad0d166d
...
...
@@ -31,7 +31,10 @@ const (
PROD
=
"prod"
)
//hook function to run
// Map shortcut
type
M
map
[
string
]
interface
{}
// Hook function to run
type
hookfunc
func
()
error
var
(
...
...
config_test.go
View file @
ad0d166d
...
...
@@ -48,15 +48,15 @@ func TestAssignConfig_02(t *testing.T) {
_BConfig
:=
&
Config
{}
bs
,
_
:=
json
.
Marshal
(
newBConfig
())
jsonMap
:=
map
[
string
]
interface
{}
{}
jsonMap
:=
M
{}
json
.
Unmarshal
(
bs
,
&
jsonMap
)
configMap
:=
map
[
string
]
interface
{}
{}
configMap
:=
M
{}
for
k
,
v
:=
range
jsonMap
{
if
reflect
.
TypeOf
(
v
)
.
Kind
()
==
reflect
.
Map
{
for
k1
,
v1
:=
range
v
.
(
map
[
string
]
interface
{}
)
{
for
k1
,
v1
:=
range
v
.
(
M
)
{
if
reflect
.
TypeOf
(
v1
)
.
Kind
()
==
reflect
.
Map
{
for
k2
,
v2
:=
range
v1
.
(
map
[
string
]
interface
{}
)
{
for
k2
,
v2
:=
range
v1
.
(
M
)
{
configMap
[
k2
]
=
v2
}
}
else
{
...
...
context/context.go
View file @
ad0d166d
...
...
@@ -38,6 +38,14 @@ import (
"github.com/astaxie/beego/utils"
)
//commonly used mime-types
const
(
ApplicationJSON
=
"application/json"
ApplicationXML
=
"application/xml"
ApplicationYAML
=
"application/x-yaml"
TextXML
=
"text/xml"
)
// NewContext return the Context with Input and Output
func
NewContext
()
*
Context
{
return
&
Context
{
...
...
context/output.go
View file @
ad0d166d
...
...
@@ -206,7 +206,7 @@ func (output *BeegoOutput) JSON(data interface{}, hasIndent bool, encoding bool)
// YAML writes yaml to response body.
func
(
output
*
BeegoOutput
)
YAML
(
data
interface
{})
error
{
output
.
Header
(
"Content-Type"
,
"application/
application/
x-yaml; charset=utf-8"
)
output
.
Header
(
"Content-Type"
,
"application/x-yaml; charset=utf-8"
)
var
content
[]
byte
var
err
error
content
,
err
=
yaml
.
Marshal
(
data
)
...
...
@@ -260,6 +260,19 @@ func (output *BeegoOutput) XML(data interface{}, hasIndent bool) error {
return
output
.
Body
(
content
)
}
// ServeFormatted serve YAML, XML OR JSON, depending on the value of the Accept header
func
(
output
*
BeegoOutput
)
ServeFormatted
(
data
interface
{},
hasIndent
bool
,
hasEncode
...
bool
)
{
accept
:=
output
.
Context
.
Input
.
Header
(
"Accept"
)
switch
accept
{
case
ApplicationYAML
:
output
.
YAML
(
data
)
case
ApplicationXML
,
TextXML
:
output
.
XML
(
data
,
hasIndent
)
default
:
output
.
JSON
(
data
,
hasIndent
,
len
(
hasEncode
)
>
0
&&
hasEncode
[
0
])
}
}
// Download forces response for download file.
// it prepares the download response header automatically.
func
(
output
*
BeegoOutput
)
Download
(
file
string
,
filename
...
string
)
{
...
...
controller.go
View file @
ad0d166d
...
...
@@ -32,14 +32,6 @@ import (
"github.com/astaxie/beego/session"
)
//commonly used mime-types
const
(
applicationJSON
=
"application/json"
applicationXML
=
"application/xml"
applicationYAML
=
"application/x-yaml"
textXML
=
"text/xml"
)
var
(
// ErrAbort custom error when user stop request handler manually.
ErrAbort
=
errors
.
New
(
"User stop run"
)
...
...
@@ -281,9 +273,9 @@ func (c *Controller) Redirect(url string, code int) {
func
(
c
*
Controller
)
SetData
(
data
interface
{})
{
accept
:=
c
.
Ctx
.
Input
.
Header
(
"Accept"
)
switch
accept
{
case
applicationJSON
:
c
.
Data
[
"
json
"
]
=
data
case
applicationXML
,
t
extXML
:
case
context
.
ApplicationYAML
:
c
.
Data
[
"
yaml
"
]
=
data
case
context
.
ApplicationXML
,
context
.
T
extXML
:
c
.
Data
[
"xml"
]
=
data
default
:
c
.
Data
[
"json"
]
=
data
...
...
@@ -332,33 +324,22 @@ func (c *Controller) URLFor(endpoint string, values ...interface{}) string {
// ServeJSON sends a json response with encoding charset.
func
(
c
*
Controller
)
ServeJSON
(
encoding
...
bool
)
{
var
(
hasIndent
=
true
hasEncoding
=
false
hasIndent
=
BConfig
.
RunMode
!=
PROD
hasEncoding
=
len
(
encoding
)
>
0
&&
encoding
[
0
]
)
if
BConfig
.
RunMode
==
PROD
{
hasIndent
=
false
}
if
len
(
encoding
)
>
0
&&
encoding
[
0
]
{
hasEncoding
=
true
}
c
.
Ctx
.
Output
.
JSON
(
c
.
Data
[
"json"
],
hasIndent
,
hasEncoding
)
}
// ServeJSONP sends a jsonp response.
func
(
c
*
Controller
)
ServeJSONP
()
{
hasIndent
:=
true
if
BConfig
.
RunMode
==
PROD
{
hasIndent
=
false
}
hasIndent
:=
BConfig
.
RunMode
!=
PROD
c
.
Ctx
.
Output
.
JSONP
(
c
.
Data
[
"jsonp"
],
hasIndent
)
}
// ServeXML sends xml response.
func
(
c
*
Controller
)
ServeXML
()
{
hasIndent
:=
true
if
BConfig
.
RunMode
==
PROD
{
hasIndent
=
false
}
hasIndent
:=
BConfig
.
RunMode
!=
PROD
c
.
Ctx
.
Output
.
XML
(
c
.
Data
[
"xml"
],
hasIndent
)
}
...
...
@@ -367,19 +348,11 @@ func (c *Controller) ServeYAML() {
c
.
Ctx
.
Output
.
YAML
(
c
.
Data
[
"yaml"
])
}
// ServeFormatted serve Xml OR Json, depending on the value of the Accept header
func
(
c
*
Controller
)
ServeFormatted
()
{
accept
:=
c
.
Ctx
.
Input
.
Header
(
"Accept"
)
switch
accept
{
case
applicationJSON
:
c
.
ServeJSON
()
case
applicationXML
,
textXML
:
c
.
ServeXML
()
case
applicationYAML
:
c
.
ServeYAML
()
default
:
c
.
ServeJSON
()
}
// ServeFormatted serve YAML, XML OR JSON, depending on the value of the Accept header
func
(
c
*
Controller
)
ServeFormatted
(
encoding
...
bool
)
{
hasIndent
:=
BConfig
.
RunMode
!=
PROD
hasEncoding
:=
len
(
encoding
)
>
0
&&
encoding
[
0
]
c
.
Ctx
.
Output
.
ServeFormatted
(
c
.
Data
,
hasIndent
,
hasEncoding
)
}
// Input returns the input data map from POST or PUT request body and query string.
...
...
error.go
View file @
ad0d166d
...
...
@@ -361,7 +361,7 @@ func gatewayTimeout(rw http.ResponseWriter, r *http.Request) {
func
responseError
(
rw
http
.
ResponseWriter
,
r
*
http
.
Request
,
errCode
int
,
errContent
string
)
{
t
,
_
:=
template
.
New
(
"beegoerrortemp"
)
.
Parse
(
errtpl
)
data
:=
map
[
string
]
interface
{}
{
data
:=
M
{
"Title"
:
http
.
StatusText
(
errCode
),
"BeegoVersion"
:
VERSION
,
"Content"
:
template
.
HTML
(
errContent
),
...
...
templatefunc.go
View file @
ad0d166d
...
...
@@ -692,7 +692,7 @@ func ge(arg1, arg2 interface{}) (bool, error) {
// MapGet getting value from map by keys
// usage:
// Data["m"] =
map[string]interface{}
{
// Data["m"] =
M
{
// "a": 1,
// "1": map[string]float64{
// "c": 4,
...
...
templatefunc_test.go
View file @
ad0d166d
...
...
@@ -329,7 +329,7 @@ func TestMapGet(t *testing.T) {
}
// test 2 level map
m2
:=
map
[
string
]
interface
{}
{
m2
:=
M
{
"1"
:
map
[
string
]
float64
{
"2"
:
3.5
,
},
...
...
@@ -344,11 +344,11 @@ func TestMapGet(t *testing.T) {
}
// test 5 level map
m5
:=
map
[
string
]
interface
{}
{
"1"
:
map
[
string
]
interface
{}
{
"2"
:
map
[
string
]
interface
{}
{
"3"
:
map
[
string
]
interface
{}
{
"4"
:
map
[
string
]
interface
{}
{
m5
:=
M
{
"1"
:
M
{
"2"
:
M
{
"3"
:
M
{
"4"
:
M
{
"5"
:
1.2
,
},
},
...
...
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