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
92086288
Commit
92086288
authored
Mar 06, 2016
by
ysqi
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'astaxie/develop' into emptybodyfix
# Conflicts: # config.go
parents
23860e68
f6f34306
Show whitespace changes
Inline
Side-by-side
Showing
21 changed files
with
239 additions
and
165 deletions
+239
-165
.travis.yml
.travis.yml
+2
-2
config.go
config.go
+24
-26
fake.go
config/fake.go
+6
-2
ini.go
config/ini.go
+7
-2
ini_test.go
config/ini_test.go
+1
-0
json.go
config/json.go
+2
-2
xml.go
config/xml/xml.go
+6
-2
xml_test.go
config/xml/xml_test.go
+3
-0
yaml.go
config/yaml/yaml.go
+6
-2
yaml_test.go
config/yaml/yaml_test.go
+4
-0
controller.go
controller.go
+18
-45
console.go
logs/console.go
+10
-7
log.go
logs/log.go
+49
-4
sess_mem.go
session/sess_mem.go
+2
-2
session.go
session/session.go
+3
-1
staticfile.go
staticfile.go
+3
-1
template.go
template.go
+72
-57
template_test.go
template_test.go
+4
-4
tree.go
tree.go
+4
-5
tree_test.go
tree_test.go
+12
-0
validators.go
validation/validators.go
+1
-1
No files found.
.travis.yml
View file @
92086288
...
...
@@ -2,9 +2,9 @@ language: go
go
:
-
tip
-
1.6.0
-
1.5.3
-
1.4.3
-
1.3.3
services
:
-
redis-server
-
mysql
...
...
@@ -21,9 +21,9 @@ install:
-
go get github.com/bradfitz/gomemcache/memcache
-
go get github.com/garyburd/redigo/redis
-
go get github.com/beego/x2j
-
go get github.com/couchbase/go-couchbase
-
go get github.com/beego/goyaml2
-
go get github.com/belogik/goes
-
go get github.com/couchbase/go-couchbase
-
go get github.com/siddontang/ledisdb/config
-
go get github.com/siddontang/ledisdb/ledis
-
go get golang.org/x/tools/cmd/vet
...
...
config.go
View file @
92086288
...
...
@@ -16,7 +16,10 @@ package beego
import
(
"fmt"
<<<<<<<
HEAD
"html/template"
=======
>>>>>>>
astaxie
/
develop
"os"
"path/filepath"
"strings"
...
...
@@ -106,12 +109,9 @@ var (
AppConfig
*
beegoAppConfig
// AppPath is the absolute path to the app
AppPath
string
// TemplateCache stores template caching
TemplateCache
map
[
string
]
*
template
.
Template
// GlobalSessions is the instance for the session manager
GlobalSessions
*
session
.
Manager
workPath
string
// appConfigPath is the path to the config files
appConfigPath
string
// appConfigProvider is the provider for the config, default is ini
...
...
@@ -120,12 +120,8 @@ var (
func
init
()
{
AppPath
,
_
=
filepath
.
Abs
(
filepath
.
Dir
(
os
.
Args
[
0
]))
workPath
,
_
=
os
.
Getwd
()
workPath
,
_
=
filepath
.
Abs
(
workPath
)
if
workPath
!=
AppPath
{
os
.
Chdir
(
AppPath
)
}
BConfig
=
&
Config
{
AppName
:
"beego"
,
...
...
@@ -175,7 +171,7 @@ func init() {
SessionName
:
"beegosessionID"
,
SessionGCMaxLifetime
:
3600
,
SessionProviderConfig
:
""
,
SessionCookieLifeTime
:
0
,
//set cookie default is the brower life
SessionCookieLifeTime
:
0
,
//set cookie default is the brow
s
er life
SessionAutoSetCookie
:
true
,
SessionDomain
:
""
,
},
...
...
@@ -189,11 +185,13 @@ func init() {
appConfigPath
=
filepath
.
Join
(
AppPath
,
"conf"
,
"app.conf"
)
if
!
utils
.
FileExists
(
appConfigPath
)
{
AppConfig
=
&
beegoAppConfig
{
config
.
NewFakeConfig
()}
AppConfig
=
&
beegoAppConfig
{
innerConfig
:
config
.
NewFakeConfig
()}
return
}
parseConfig
(
appConfigPath
)
if
err
:=
parseConfig
(
appConfigPath
);
err
!=
nil
{
panic
(
err
)
}
}
// now only support ini, next will support json.
...
...
@@ -202,11 +200,11 @@ func parseConfig(appConfigPath string) (err error) {
if
err
!=
nil
{
return
err
}
// set the runmode first
// set the run
mode first
if
envRunMode
:=
os
.
Getenv
(
"BEEGO_RUNMODE"
);
envRunMode
!=
""
{
BConfig
.
RunMode
=
envRunMode
}
else
if
run
mode
:=
AppConfig
.
String
(
"RunMode"
);
runm
ode
!=
""
{
BConfig
.
RunMode
=
run
m
ode
}
else
if
run
Mode
:=
AppConfig
.
String
(
"RunMode"
);
runM
ode
!=
""
{
BConfig
.
RunMode
=
run
M
ode
}
BConfig
.
AppName
=
AppConfig
.
DefaultString
(
"AppName"
,
BConfig
.
AppName
)
...
...
@@ -299,7 +297,7 @@ func parseConfig(appConfigPath string) (err error) {
}
//init log
BeeLogger
.
Close
()
BeeLogger
.
Reset
()
for
adaptor
,
config
:=
range
BConfig
.
Log
.
Outputs
{
err
=
BeeLogger
.
SetLogger
(
adaptor
,
config
)
if
err
!=
nil
{
...
...
@@ -393,46 +391,46 @@ func (b *beegoAppConfig) Float(key string) (float64, error) {
return
b
.
innerConfig
.
Float
(
key
)
}
func
(
b
*
beegoAppConfig
)
DefaultString
(
key
string
,
default
v
al
string
)
string
{
func
(
b
*
beegoAppConfig
)
DefaultString
(
key
string
,
default
V
al
string
)
string
{
if
v
:=
b
.
String
(
key
);
v
!=
""
{
return
v
}
return
default
v
al
return
default
V
al
}
func
(
b
*
beegoAppConfig
)
DefaultStrings
(
key
string
,
default
v
al
[]
string
)
[]
string
{
func
(
b
*
beegoAppConfig
)
DefaultStrings
(
key
string
,
default
V
al
[]
string
)
[]
string
{
if
v
:=
b
.
Strings
(
key
);
len
(
v
)
!=
0
{
return
v
}
return
default
v
al
return
default
V
al
}
func
(
b
*
beegoAppConfig
)
DefaultInt
(
key
string
,
default
v
al
int
)
int
{
func
(
b
*
beegoAppConfig
)
DefaultInt
(
key
string
,
default
V
al
int
)
int
{
if
v
,
err
:=
b
.
Int
(
key
);
err
==
nil
{
return
v
}
return
default
v
al
return
default
V
al
}
func
(
b
*
beegoAppConfig
)
DefaultInt64
(
key
string
,
default
v
al
int64
)
int64
{
func
(
b
*
beegoAppConfig
)
DefaultInt64
(
key
string
,
default
V
al
int64
)
int64
{
if
v
,
err
:=
b
.
Int64
(
key
);
err
==
nil
{
return
v
}
return
default
v
al
return
default
V
al
}
func
(
b
*
beegoAppConfig
)
DefaultBool
(
key
string
,
default
v
al
bool
)
bool
{
func
(
b
*
beegoAppConfig
)
DefaultBool
(
key
string
,
default
V
al
bool
)
bool
{
if
v
,
err
:=
b
.
Bool
(
key
);
err
==
nil
{
return
v
}
return
default
v
al
return
default
V
al
}
func
(
b
*
beegoAppConfig
)
DefaultFloat
(
key
string
,
default
v
al
float64
)
float64
{
func
(
b
*
beegoAppConfig
)
DefaultFloat
(
key
string
,
default
V
al
float64
)
float64
{
if
v
,
err
:=
b
.
Float
(
key
);
err
==
nil
{
return
v
}
return
default
v
al
return
default
V
al
}
func
(
b
*
beegoAppConfig
)
DIY
(
key
string
)
(
interface
{},
error
)
{
...
...
config/fake.go
View file @
92086288
...
...
@@ -46,12 +46,16 @@ func (c *fakeConfigContainer) DefaultString(key string, defaultval string) strin
}
func
(
c
*
fakeConfigContainer
)
Strings
(
key
string
)
[]
string
{
return
strings
.
Split
(
c
.
getData
(
key
),
";"
)
v
:=
c
.
getData
(
key
)
if
v
==
""
{
return
nil
}
return
strings
.
Split
(
v
,
";"
)
}
func
(
c
*
fakeConfigContainer
)
DefaultStrings
(
key
string
,
defaultval
[]
string
)
[]
string
{
v
:=
c
.
Strings
(
key
)
if
len
(
v
)
==
0
{
if
v
==
nil
{
return
defaultval
}
return
v
...
...
config/ini.go
View file @
92086288
...
...
@@ -269,15 +269,20 @@ func (c *IniConfigContainer) DefaultString(key string, defaultval string) string
}
// Strings returns the []string value for a given key.
// Return nil if config value does not exist or is empty.
func
(
c
*
IniConfigContainer
)
Strings
(
key
string
)
[]
string
{
return
strings
.
Split
(
c
.
String
(
key
),
";"
)
v
:=
c
.
String
(
key
)
if
v
==
""
{
return
nil
}
return
strings
.
Split
(
v
,
";"
)
}
// DefaultStrings returns the []string value for a given key.
// if err != nil return defaltval
func
(
c
*
IniConfigContainer
)
DefaultStrings
(
key
string
,
defaultval
[]
string
)
[]
string
{
v
:=
c
.
Strings
(
key
)
if
len
(
v
)
==
0
{
if
v
==
nil
{
return
defaultval
}
return
v
...
...
config/ini_test.go
View file @
92086288
...
...
@@ -71,6 +71,7 @@ peers = one;two;three
"null"
:
""
,
"demo2::key1"
:
""
,
"error"
:
""
,
"emptystrings"
:
[]
string
{},
}
)
...
...
config/json.go
View file @
92086288
...
...
@@ -173,7 +173,7 @@ func (c *JSONConfigContainer) DefaultString(key string, defaultval string) strin
func
(
c
*
JSONConfigContainer
)
Strings
(
key
string
)
[]
string
{
stringVal
:=
c
.
String
(
key
)
if
stringVal
==
""
{
return
[]
string
{}
return
nil
}
return
strings
.
Split
(
c
.
String
(
key
),
";"
)
}
...
...
@@ -181,7 +181,7 @@ func (c *JSONConfigContainer) Strings(key string) []string {
// DefaultStrings returns the []string value for a given key.
// if err != nil return defaltval
func
(
c
*
JSONConfigContainer
)
DefaultStrings
(
key
string
,
defaultval
[]
string
)
[]
string
{
if
v
:=
c
.
Strings
(
key
);
len
(
v
)
>
0
{
if
v
:=
c
.
Strings
(
key
);
v
!=
nil
{
return
v
}
return
defaultval
...
...
config/xml/xml.go
View file @
92086288
...
...
@@ -174,14 +174,18 @@ func (c *ConfigContainer) DefaultString(key string, defaultval string) string {
// Strings returns the []string value for a given key.
func
(
c
*
ConfigContainer
)
Strings
(
key
string
)
[]
string
{
return
strings
.
Split
(
c
.
String
(
key
),
";"
)
v
:=
c
.
String
(
key
)
if
v
==
""
{
return
nil
}
return
strings
.
Split
(
v
,
";"
)
}
// DefaultStrings returns the []string value for a given key.
// if err != nil return defaltval
func
(
c
*
ConfigContainer
)
DefaultStrings
(
key
string
,
defaultval
[]
string
)
[]
string
{
v
:=
c
.
Strings
(
key
)
if
len
(
v
)
==
0
{
if
v
==
nil
{
return
defaultval
}
return
v
...
...
config/xml/xml_test.go
View file @
92086288
...
...
@@ -82,4 +82,7 @@ func TestXML(t *testing.T) {
if
xmlconf
.
String
(
"name"
)
!=
"astaxie"
{
t
.
Fatal
(
"get name error"
)
}
if
xmlconf
.
Strings
(
"emptystrings"
)
!=
nil
{
t
.
Fatal
(
"get emtpy strings error"
)
}
}
config/yaml/yaml.go
View file @
92086288
...
...
@@ -211,14 +211,18 @@ func (c *ConfigContainer) DefaultString(key string, defaultval string) string {
// Strings returns the []string value for a given key.
func
(
c
*
ConfigContainer
)
Strings
(
key
string
)
[]
string
{
return
strings
.
Split
(
c
.
String
(
key
),
";"
)
v
:=
c
.
String
(
key
)
if
v
==
""
{
return
nil
}
return
strings
.
Split
(
v
,
";"
)
}
// DefaultStrings returns the []string value for a given key.
// if err != nil return defaltval
func
(
c
*
ConfigContainer
)
DefaultStrings
(
key
string
,
defaultval
[]
string
)
[]
string
{
v
:=
c
.
Strings
(
key
)
if
len
(
v
)
==
0
{
if
v
==
nil
{
return
defaultval
}
return
v
...
...
config/yaml/yaml_test.go
View file @
92086288
...
...
@@ -79,4 +79,8 @@ func TestYaml(t *testing.T) {
if
yamlconf
.
String
(
"name"
)
!=
"astaxie"
{
t
.
Fatal
(
"get name error"
)
}
if
yamlconf
.
Strings
(
"emptystrings"
)
!=
nil
{
t
.
Fatal
(
"get emtpy strings error"
)
}
}
controller.go
View file @
92086288
...
...
@@ -196,33 +196,9 @@ func (c *Controller) RenderString() (string, error) {
// RenderBytes returns the bytes of rendered template string. Do not send out response.
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
var
buf
bytes
.
Buffer
if
c
.
Layout
!=
""
{
if
c
.
TplName
==
""
{
c
.
TplName
=
strings
.
ToLower
(
c
.
controllerName
)
+
"/"
+
strings
.
ToLower
(
c
.
actionName
)
+
"."
+
c
.
TplExt
}
if
BConfig
.
RunMode
==
DEV
{
buildFiles
:=
[]
string
{
c
.
TplName
}
if
c
.
LayoutSections
!=
nil
{
for
_
,
sectionTpl
:=
range
c
.
LayoutSections
{
if
sectionTpl
==
""
{
continue
}
buildFiles
=
append
(
buildFiles
,
sectionTpl
)
}
}
BuildTemplate
(
BConfig
.
WebConfig
.
ViewsPath
,
buildFiles
...
)
}
if
_
,
ok
:=
BeeTemplates
[
c
.
TplName
];
!
ok
{
panic
(
"can't find templatefile in the path:"
+
c
.
TplName
)
}
err
:=
BeeTemplates
[
c
.
TplName
]
.
ExecuteTemplate
(
&
buf
,
c
.
TplName
,
c
.
Data
)
if
err
!=
nil
{
Trace
(
"template Execute err:"
,
err
)
return
nil
,
err
}
buf
,
err
:=
c
.
renderTemplate
()
//if the controller has set layout, then first get the tplName's content set the content to the layout
if
err
==
nil
&&
c
.
Layout
!=
""
{
c
.
Data
[
"LayoutContent"
]
=
template
.
HTML
(
buf
.
String
())
if
c
.
LayoutSections
!=
nil
{
...
...
@@ -231,11 +207,9 @@ func (c *Controller) RenderBytes() ([]byte, error) {
c
.
Data
[
sectionName
]
=
""
continue
}
buf
.
Reset
()
err
=
BeeTemplates
[
sectionTpl
]
.
E
xecuteTemplate
(
&
buf
,
sectionTpl
,
c
.
Data
)
err
=
e
xecuteTemplate
(
&
buf
,
sectionTpl
,
c
.
Data
)
if
err
!=
nil
{
Trace
(
"template Execute err:"
,
err
)
return
nil
,
err
}
c
.
Data
[
sectionName
]
=
template
.
HTML
(
buf
.
String
())
...
...
@@ -243,30 +217,29 @@ func (c *Controller) RenderBytes() ([]byte, error) {
}
buf
.
Reset
()
err
=
BeeTemplates
[
c
.
Layout
]
.
ExecuteTemplate
(
&
buf
,
c
.
Layout
,
c
.
Data
)
if
err
!=
nil
{
Trace
(
"template Execute err:"
,
err
)
return
nil
,
err
}
return
buf
.
Bytes
(),
nil
executeTemplate
(
&
buf
,
c
.
Layout
,
c
.
Data
)
}
return
buf
.
Bytes
(),
err
}
func
(
c
*
Controller
)
renderTemplate
()
(
bytes
.
Buffer
,
error
)
{
var
buf
bytes
.
Buffer
if
c
.
TplName
==
""
{
c
.
TplName
=
strings
.
ToLower
(
c
.
controllerName
)
+
"/"
+
strings
.
ToLower
(
c
.
actionName
)
+
"."
+
c
.
TplExt
}
if
BConfig
.
RunMode
==
DEV
{
BuildTemplate
(
BConfig
.
WebConfig
.
ViewsPath
,
c
.
TplName
)
buildFiles
:=
[]
string
{
c
.
TplName
}
if
c
.
Layout
!=
""
&&
c
.
LayoutSections
!=
nil
{
for
_
,
sectionTpl
:=
range
c
.
LayoutSections
{
if
sectionTpl
==
""
{
continue
}
if
_
,
ok
:=
BeeTemplates
[
c
.
TplName
];
!
ok
{
panic
(
"can't find templatefile in the path:"
+
c
.
TplName
)
buildFiles
=
append
(
buildFiles
,
sectionTpl
)
}
buf
.
Reset
()
err
:=
BeeTemplates
[
c
.
TplName
]
.
ExecuteTemplate
(
&
buf
,
c
.
TplName
,
c
.
Data
)
if
err
!=
nil
{
Trace
(
"template Execute err:"
,
err
)
return
nil
,
err
}
return
buf
.
Bytes
(),
nil
BuildTemplate
(
BConfig
.
WebConfig
.
ViewsPath
,
buildFiles
...
)
}
return
buf
,
executeTemplate
(
&
buf
,
c
.
TplName
,
c
.
Data
)
}
// Redirect sends the redirection response to url with status code.
...
...
logs/console.go
View file @
92086288
...
...
@@ -49,7 +49,7 @@ var colors = []brush{
type
consoleWriter
struct
{
lg
*
log
.
Logger
Level
int
`json:"level"`
Color
bool
`json:"color"`
Color
ful
bool
`json:"color"`
//this filed is useful only when system's terminal supports color
}
// NewConsole create ConsoleWriter returning as LoggerInterface.
...
...
@@ -57,7 +57,7 @@ func NewConsole() Logger {
cw
:=
&
consoleWriter
{
lg
:
log
.
New
(
os
.
Stdout
,
""
,
0
),
Level
:
LevelDebug
,
Color
:
true
,
Color
ful
:
true
,
}
return
cw
}
...
...
@@ -68,7 +68,11 @@ func (c *consoleWriter) Init(jsonConfig string) error {
if
len
(
jsonConfig
)
==
0
{
return
nil
}
return
json
.
Unmarshal
([]
byte
(
jsonConfig
),
c
)
err
:=
json
.
Unmarshal
([]
byte
(
jsonConfig
),
c
)
if
runtime
.
GOOS
==
"windows"
{
c
.
Colorful
=
false
}
return
err
}
// WriteMsg write message in console.
...
...
@@ -77,12 +81,11 @@ func (c *consoleWriter) WriteMsg(when time.Time, msg string, level int) error {
return
nil
}
msg
=
formatLogTime
(
when
)
+
msg
if
runtime
.
GOOS
==
"windows"
||
!
c
.
Color
{
if
c
.
Colorful
{
c
.
lg
.
Println
(
colors
[
level
](
msg
))
}
else
{
c
.
lg
.
Println
(
msg
)
return
nil
}
c
.
lg
.
Println
(
colors
[
level
](
msg
))
return
nil
}
...
...
logs/log.go
View file @
92086288
...
...
@@ -98,6 +98,8 @@ type BeeLogger struct {
loggerFuncCallDepth
int
asynchronous
bool
msgChan
chan
*
logMsg
signalChan
chan
string
wg
sync
.
WaitGroup
outputs
[]
*
nameLogger
}
...
...
@@ -122,6 +124,7 @@ func NewLogger(channelLen int64) *BeeLogger {
bl
.
level
=
LevelDebug
bl
.
loggerFuncCallDepth
=
2
bl
.
msgChan
=
make
(
chan
*
logMsg
,
channelLen
)
bl
.
signalChan
=
make
(
chan
string
,
1
)
return
bl
}
...
...
@@ -133,6 +136,7 @@ func (bl *BeeLogger) Async() *BeeLogger {
return
&
logMsg
{}
},
}
bl
.
wg
.
Add
(
1
)
go
bl
.
startLogger
()
return
bl
}
...
...
@@ -232,11 +236,26 @@ func (bl *BeeLogger) EnableFuncCallDepth(b bool) {
// start logger chan reading.
// when chan is not empty, write logs.
func
(
bl
*
BeeLogger
)
startLogger
()
{
gameOver
:=
false
for
{
select
{
case
bm
:=
<-
bl
.
msgChan
:
bl
.
writeToLoggers
(
bm
.
when
,
bm
.
msg
,
bm
.
level
)
logMsgPool
.
Put
(
bm
)
case
sg
:=
<-
bl
.
signalChan
:
// Now should only send "flush" or "close" to bl.signalChan
bl
.
flush
()
if
sg
==
"close"
{
for
_
,
l
:=
range
bl
.
outputs
{
l
.
Destroy
()
}
bl
.
outputs
=
nil
gameOver
=
true
}
bl
.
wg
.
Done
()
}
if
gameOver
{
break
}
}
}
...
...
@@ -345,13 +364,41 @@ func (bl *BeeLogger) Trace(format string, v ...interface{}) {
// Flush flush all chan data.
func
(
bl
*
BeeLogger
)
Flush
()
{
for
_
,
l
:=
range
bl
.
outputs
{
l
.
Flush
()
if
bl
.
asynchronous
{
bl
.
signalChan
<-
"flush"
bl
.
wg
.
Wait
()
bl
.
wg
.
Add
(
1
)
return
}
bl
.
flush
()
}
// Close close logger, flush all chan data and destroy all adapters in BeeLogger.
func
(
bl
*
BeeLogger
)
Close
()
{
if
bl
.
asynchronous
{
bl
.
signalChan
<-
"close"
bl
.
wg
.
Wait
()
}
else
{
bl
.
flush
()
for
_
,
l
:=
range
bl
.
outputs
{
l
.
Destroy
()
}
bl
.
outputs
=
nil
}
close
(
bl
.
msgChan
)
close
(
bl
.
signalChan
)
}
// Reset close all outputs, and set bl.outputs to nil
func
(
bl
*
BeeLogger
)
Reset
()
{
bl
.
Flush
()
for
_
,
l
:=
range
bl
.
outputs
{
l
.
Destroy
()
}
bl
.
outputs
=
nil
}
func
(
bl
*
BeeLogger
)
flush
()
{
for
{
if
len
(
bl
.
msgChan
)
>
0
{
bm
:=
<-
bl
.
msgChan
...
...
@@ -363,9 +410,7 @@ func (bl *BeeLogger) Close() {
}
for
_
,
l
:=
range
bl
.
outputs
{
l
.
Flush
()
l
.
Destroy
()
}
bl
.
outputs
=
nil
}
func
formatLogTime
(
when
time
.
Time
)
string
{
...
...
session/sess_mem.go
View file @
92086288
...
...
@@ -102,7 +102,7 @@ func (pder *MemProvider) SessionRead(sid string) (Store, error) {
pder
.
lock
.
RUnlock
()
pder
.
lock
.
Lock
()
newsess
:=
&
MemSessionStore
{
sid
:
sid
,
timeAccessed
:
time
.
Now
(),
value
:
make
(
map
[
interface
{}]
interface
{})}
element
:=
pder
.
list
.
Push
Back
(
newsess
)
element
:=
pder
.
list
.
Push
Front
(
newsess
)
pder
.
sessions
[
sid
]
=
element
pder
.
lock
.
Unlock
()
return
newsess
,
nil
...
...
@@ -134,7 +134,7 @@ func (pder *MemProvider) SessionRegenerate(oldsid, sid string) (Store, error) {
pder
.
lock
.
RUnlock
()
pder
.
lock
.
Lock
()
newsess
:=
&
MemSessionStore
{
sid
:
sid
,
timeAccessed
:
time
.
Now
(),
value
:
make
(
map
[
interface
{}]
interface
{})}
element
:=
pder
.
list
.
Push
Back
(
newsess
)
element
:=
pder
.
list
.
Push
Front
(
newsess
)
pder
.
sessions
[
sid
]
=
element
pder
.
lock
.
Unlock
()
return
newsess
,
nil
...
...
session/session.go
View file @
92086288
...
...
@@ -201,7 +201,9 @@ func (manager *Manager) SessionDestroy(w http.ResponseWriter, r *http.Request) {
if
err
!=
nil
||
cookie
.
Value
==
""
{
return
}
manager
.
provider
.
SessionDestroy
(
cookie
.
Value
)
sid
,
_
:=
url
.
QueryUnescape
(
cookie
.
Value
)
manager
.
provider
.
SessionDestroy
(
sid
)
if
manager
.
config
.
EnableSetCookie
{
expiration
:=
time
.
Now
()
cookie
=
&
http
.
Cookie
{
Name
:
manager
.
config
.
CookieName
,
...
...
staticfile.go
View file @
92086288
...
...
@@ -93,12 +93,14 @@ type serveContentHolder struct {
var
(
staticFileMap
=
make
(
map
[
string
]
*
serveContentHolder
)
mapLock
sync
.
Mutex
mapLock
sync
.
RW
Mutex
)
func
openFile
(
filePath
string
,
fi
os
.
FileInfo
,
acceptEncoding
string
)
(
bool
,
string
,
*
serveContentHolder
,
error
)
{
mapKey
:=
acceptEncoding
+
":"
+
filePath
mapLock
.
RLock
()
mapFile
,
_
:=
staticFileMap
[
mapKey
]
mapLock
.
RUnlock
()
if
isOk
(
mapFile
,
fi
)
{
return
mapFile
.
encoding
!=
""
,
mapFile
.
encoding
,
mapFile
,
nil
}
...
...
template.go
View file @
92086288
...
...
@@ -18,23 +18,41 @@ import (
"errors"
"fmt"
"html/template"
"io"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
"sync"
"github.com/astaxie/beego/utils"
)
var
(
beegoTplFuncMap
=
make
(
template
.
FuncMap
)
// BeeTemplates caching map and supported template file extensions.
BeeTemplates
=
make
(
map
[
string
]
*
template
.
Template
)
// BeeTemplateExt stores the template extension which will build
BeeTemplateExt
=
[]
string
{
"tpl"
,
"html"
}
// beeTemplates caching map and supported template file extensions.
beeTemplates
=
make
(
map
[
string
]
*
template
.
Template
)
templatesLock
sync
.
RWMutex
// beeTemplateExt stores the template extension which will build
beeTemplateExt
=
[]
string
{
"tpl"
,
"html"
}
)
func
executeTemplate
(
wr
io
.
Writer
,
name
string
,
data
interface
{})
error
{
if
BConfig
.
RunMode
==
DEV
{
templatesLock
.
RLock
()
defer
templatesLock
.
RUnlock
()
}
if
t
,
ok
:=
beeTemplates
[
name
];
ok
{
err
:=
t
.
ExecuteTemplate
(
wr
,
name
,
data
)
if
err
!=
nil
{
Trace
(
"template Execute err:"
,
err
)
}
return
err
}
panic
(
"can't find templatefile in the path:"
+
name
)
}
func
init
()
{
beegoTplFuncMap
[
"dateformat"
]
=
DateFormat
beegoTplFuncMap
[
"date"
]
=
Date
...
...
@@ -53,7 +71,6 @@ func init() {
beegoTplFuncMap
[
"config"
]
=
GetConfig
beegoTplFuncMap
[
"map_get"
]
=
MapGet
// go1.2 added template funcs
// Comparisons
beegoTplFuncMap
[
"eq"
]
=
eq
// ==
beegoTplFuncMap
[
"ge"
]
=
ge
// >=
...
...
@@ -66,17 +83,21 @@ func init() {
}
// AddFuncMap let user to register a func in the template.
func
AddFuncMap
(
key
string
,
f
unname
interface
{})
error
{
beegoTplFuncMap
[
key
]
=
f
unname
func
AddFuncMap
(
key
string
,
f
n
interface
{})
error
{
beegoTplFuncMap
[
key
]
=
f
n
return
nil
}
type
template
f
ile
struct
{
type
template
F
ile
struct
{
root
string
files
map
[
string
][]
string
}
func
(
tf
*
templatefile
)
visit
(
paths
string
,
f
os
.
FileInfo
,
err
error
)
error
{
// visit will make the paths into two part,the first is subDir (without tf.root),the second is full path(without tf.root).
// if tf.root="views" and
// paths is "views/errors/404.html",the subDir will be "errors",the file will be "errors/404.html"
// paths is "views/admin/errors/404.html",the subDir will be "admin/errors",the file will be "admin/errors/404.html"
func
(
tf
*
templateFile
)
visit
(
paths
string
,
f
os
.
FileInfo
,
err
error
)
error
{
if
f
==
nil
{
return
err
}
...
...
@@ -88,24 +109,16 @@ func (tf *templatefile) visit(paths string, f os.FileInfo, err error) error {
}
replace
:=
strings
.
NewReplacer
(
"
\\
"
,
"/"
)
a
:=
[]
byte
(
paths
)
a
=
a
[
len
([]
byte
(
tf
.
root
))
:
]
file
:=
strings
.
TrimLeft
(
replace
.
Replace
(
string
(
a
)),
"/"
)
subdir
:=
filepath
.
Dir
(
file
)
if
_
,
ok
:=
tf
.
files
[
subdir
];
ok
{
tf
.
files
[
subdir
]
=
append
(
tf
.
files
[
subdir
],
file
)
}
else
{
m
:=
make
([]
string
,
1
)
m
[
0
]
=
file
tf
.
files
[
subdir
]
=
m
}
file
:=
strings
.
TrimLeft
(
replace
.
Replace
(
paths
[
len
(
tf
.
root
)
:
]),
"/"
)
subDir
:=
filepath
.
Dir
(
file
)
tf
.
files
[
subDir
]
=
append
(
tf
.
files
[
subDir
],
file
)
return
nil
}
// HasTemplateExt return this path contains supported template extension of beego or not.
func
HasTemplateExt
(
paths
string
)
bool
{
for
_
,
v
:=
range
B
eeTemplateExt
{
for
_
,
v
:=
range
b
eeTemplateExt
{
if
strings
.
HasSuffix
(
paths
,
"."
+
v
)
{
return
true
}
...
...
@@ -115,12 +128,12 @@ func HasTemplateExt(paths string) bool {
// AddTemplateExt add new extension for template.
func
AddTemplateExt
(
ext
string
)
{
for
_
,
v
:=
range
B
eeTemplateExt
{
for
_
,
v
:=
range
b
eeTemplateExt
{
if
v
==
ext
{
return
}
}
BeeTemplateExt
=
append
(
B
eeTemplateExt
,
ext
)
beeTemplateExt
=
append
(
b
eeTemplateExt
,
ext
)
}
// BuildTemplate will build all template files in a directory.
...
...
@@ -132,7 +145,7 @@ func BuildTemplate(dir string, files ...string) error {
}
return
errors
.
New
(
"dir open err"
)
}
self
:=
&
template
f
ile
{
self
:=
&
template
F
ile
{
root
:
dir
,
files
:
make
(
map
[
string
][]
string
),
}
...
...
@@ -146,12 +159,14 @@ func BuildTemplate(dir string, files ...string) error {
for
_
,
v
:=
range
self
.
files
{
for
_
,
file
:=
range
v
{
if
len
(
files
)
==
0
||
utils
.
InSlice
(
file
,
files
)
{
templatesLock
.
Lock
()
t
,
err
:=
getTemplate
(
self
.
root
,
file
,
v
...
)
if
err
!=
nil
{
Trace
(
"parse template err:"
,
file
,
err
)
}
else
{
B
eeTemplates
[
file
]
=
t
b
eeTemplates
[
file
]
=
t
}
templatesLock
.
Unlock
()
}
}
}
...
...
@@ -159,16 +174,16 @@ func BuildTemplate(dir string, files ...string) error {
}
func
getTplDeep
(
root
,
file
,
parent
string
,
t
*
template
.
Template
)
(
*
template
.
Template
,
[][]
string
,
error
)
{
var
file
absp
ath
string
var
file
AbsP
ath
string
if
filepath
.
HasPrefix
(
file
,
"../"
)
{
file
absp
ath
=
filepath
.
Join
(
root
,
filepath
.
Dir
(
parent
),
file
)
file
AbsP
ath
=
filepath
.
Join
(
root
,
filepath
.
Dir
(
parent
),
file
)
}
else
{
file
absp
ath
=
filepath
.
Join
(
root
,
file
)
file
AbsP
ath
=
filepath
.
Join
(
root
,
file
)
}
if
e
:=
utils
.
FileExists
(
file
absp
ath
);
!
e
{
if
e
:=
utils
.
FileExists
(
file
AbsP
ath
);
!
e
{
panic
(
"can't find template file:"
+
file
)
}
data
,
err
:=
ioutil
.
ReadFile
(
file
absp
ath
)
data
,
err
:=
ioutil
.
ReadFile
(
file
AbsP
ath
)
if
err
!=
nil
{
return
nil
,
[][]
string
{},
err
}
...
...
@@ -177,11 +192,11 @@ func getTplDeep(root, file, parent string, t *template.Template) (*template.Temp
return
nil
,
[][]
string
{},
err
}
reg
:=
regexp
.
MustCompile
(
BConfig
.
WebConfig
.
TemplateLeft
+
"[ ]*template[ ]+
\"
([^
\"
]+)
\"
"
)
all
s
ub
:=
reg
.
FindAllStringSubmatch
(
string
(
data
),
-
1
)
for
_
,
m
:=
range
all
s
ub
{
all
S
ub
:=
reg
.
FindAllStringSubmatch
(
string
(
data
),
-
1
)
for
_
,
m
:=
range
all
S
ub
{
if
len
(
m
)
==
2
{
tl
ook
:=
t
.
Lookup
(
m
[
1
])
if
tl
ook
!=
nil
{
tl
:=
t
.
Lookup
(
m
[
1
])
if
tl
!=
nil
{
continue
}
if
!
HasTemplateExt
(
m
[
1
])
{
...
...
@@ -193,17 +208,17 @@ func getTplDeep(root, file, parent string, t *template.Template) (*template.Temp
}
}
}
return
t
,
all
s
ub
,
nil
return
t
,
all
S
ub
,
nil
}
func
getTemplate
(
root
,
file
string
,
others
...
string
)
(
t
*
template
.
Template
,
err
error
)
{
t
=
template
.
New
(
file
)
.
Delims
(
BConfig
.
WebConfig
.
TemplateLeft
,
BConfig
.
WebConfig
.
TemplateRight
)
.
Funcs
(
beegoTplFuncMap
)
var
sub
m
ods
[][]
string
t
,
sub
m
ods
,
err
=
getTplDeep
(
root
,
file
,
""
,
t
)
var
sub
M
ods
[][]
string
t
,
sub
M
ods
,
err
=
getTplDeep
(
root
,
file
,
""
,
t
)
if
err
!=
nil
{
return
nil
,
err
}
t
,
err
=
_getTemplate
(
t
,
root
,
sub
m
ods
,
others
...
)
t
,
err
=
_getTemplate
(
t
,
root
,
sub
M
ods
,
others
...
)
if
err
!=
nil
{
return
nil
,
err
...
...
@@ -211,44 +226,44 @@ func getTemplate(root, file string, others ...string) (t *template.Template, err
return
}
func
_getTemplate
(
t0
*
template
.
Template
,
root
string
,
sub
m
ods
[][]
string
,
others
...
string
)
(
t
*
template
.
Template
,
err
error
)
{
func
_getTemplate
(
t0
*
template
.
Template
,
root
string
,
sub
M
ods
[][]
string
,
others
...
string
)
(
t
*
template
.
Template
,
err
error
)
{
t
=
t0
for
_
,
m
:=
range
sub
m
ods
{
for
_
,
m
:=
range
sub
M
ods
{
if
len
(
m
)
==
2
{
t
em
pl
:=
t
.
Lookup
(
m
[
1
])
if
t
em
pl
!=
nil
{
tpl
:=
t
.
Lookup
(
m
[
1
])
if
tpl
!=
nil
{
continue
}
//first check filename
for
_
,
other
f
ile
:=
range
others
{
if
other
f
ile
==
m
[
1
]
{
var
sub
m
ods1
[][]
string
t
,
sub
mods1
,
err
=
getTplDeep
(
root
,
otherf
ile
,
""
,
t
)
for
_
,
other
F
ile
:=
range
others
{
if
other
F
ile
==
m
[
1
]
{
var
sub
M
ods1
[][]
string
t
,
sub
Mods1
,
err
=
getTplDeep
(
root
,
otherF
ile
,
""
,
t
)
if
err
!=
nil
{
Trace
(
"template parse file err:"
,
err
)
}
else
if
sub
mods1
!=
nil
&&
len
(
subm
ods1
)
>
0
{
t
,
err
=
_getTemplate
(
t
,
root
,
sub
m
ods1
,
others
...
)
}
else
if
sub
Mods1
!=
nil
&&
len
(
subM
ods1
)
>
0
{
t
,
err
=
_getTemplate
(
t
,
root
,
sub
M
ods1
,
others
...
)
}
break
}
}
//second check define
for
_
,
other
f
ile
:=
range
others
{
file
abspath
:=
filepath
.
Join
(
root
,
otherf
ile
)
data
,
err
:=
ioutil
.
ReadFile
(
file
absp
ath
)
for
_
,
other
F
ile
:=
range
others
{
file
AbsPath
:=
filepath
.
Join
(
root
,
otherF
ile
)
data
,
err
:=
ioutil
.
ReadFile
(
file
AbsP
ath
)
if
err
!=
nil
{
continue
}
reg
:=
regexp
.
MustCompile
(
BConfig
.
WebConfig
.
TemplateLeft
+
"[ ]*define[ ]+
\"
([^
\"
]+)
\"
"
)
all
s
ub
:=
reg
.
FindAllStringSubmatch
(
string
(
data
),
-
1
)
for
_
,
sub
:=
range
all
s
ub
{
all
S
ub
:=
reg
.
FindAllStringSubmatch
(
string
(
data
),
-
1
)
for
_
,
sub
:=
range
all
S
ub
{
if
len
(
sub
)
==
2
&&
sub
[
1
]
==
m
[
1
]
{
var
sub
m
ods1
[][]
string
t
,
sub
mods1
,
err
=
getTplDeep
(
root
,
otherf
ile
,
""
,
t
)
var
sub
M
ods1
[][]
string
t
,
sub
Mods1
,
err
=
getTplDeep
(
root
,
otherF
ile
,
""
,
t
)
if
err
!=
nil
{
Trace
(
"template parse file err:"
,
err
)
}
else
if
sub
mods1
!=
nil
&&
len
(
subm
ods1
)
>
0
{
t
,
err
=
_getTemplate
(
t
,
root
,
sub
m
ods1
,
others
...
)
}
else
if
sub
Mods1
!=
nil
&&
len
(
subM
ods1
)
>
0
{
t
,
err
=
_getTemplate
(
t
,
root
,
sub
M
ods1
,
others
...
)
}
break
}
...
...
template_test.go
View file @
92086288
...
...
@@ -70,10 +70,10 @@ func TestTemplate(t *testing.T) {
if
err
:=
BuildTemplate
(
dir
);
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
len
(
B
eeTemplates
)
!=
3
{
t
.
Fatalf
(
"should be 3 but got %v"
,
len
(
B
eeTemplates
))
if
len
(
b
eeTemplates
)
!=
3
{
t
.
Fatalf
(
"should be 3 but got %v"
,
len
(
b
eeTemplates
))
}
if
err
:=
B
eeTemplates
[
"index.tpl"
]
.
ExecuteTemplate
(
os
.
Stdout
,
"index.tpl"
,
nil
);
err
!=
nil
{
if
err
:=
b
eeTemplates
[
"index.tpl"
]
.
ExecuteTemplate
(
os
.
Stdout
,
"index.tpl"
,
nil
);
err
!=
nil
{
t
.
Fatal
(
err
)
}
for
_
,
name
:=
range
files
{
...
...
@@ -126,7 +126,7 @@ func TestRelativeTemplate(t *testing.T) {
if
err
:=
BuildTemplate
(
dir
,
files
[
1
]);
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
err
:=
B
eeTemplates
[
"easyui/rbac/user.tpl"
]
.
ExecuteTemplate
(
os
.
Stdout
,
"easyui/rbac/user.tpl"
,
nil
);
err
!=
nil
{
if
err
:=
b
eeTemplates
[
"easyui/rbac/user.tpl"
]
.
ExecuteTemplate
(
os
.
Stdout
,
"easyui/rbac/user.tpl"
,
nil
);
err
!=
nil
{
t
.
Fatal
(
err
)
}
for
_
,
name
:=
range
files
{
...
...
tree.go
View file @
92086288
...
...
@@ -265,15 +265,14 @@ func (t *Tree) addseg(segments []string, route interface{}, wildcards []string,
}
t
.
wildcard
.
addseg
(
segments
[
1
:
],
route
,
append
(
wildcards
,
params
...
),
reg
+
regexpStr
)
}
else
{
var
ok
bool
var
subTree
*
Tree
for
_
,
sub
Tree
=
range
t
.
fixrouters
{
if
t
.
prefix
==
seg
{
ok
=
true
for
_
,
sub
:
=
range
t
.
fixrouters
{
if
sub
.
prefix
==
seg
{
subTree
=
sub
break
}
}
if
!
ok
{
if
subTree
==
nil
{
subTree
=
NewTree
()
subTree
.
prefix
=
seg
t
.
fixrouters
=
append
(
t
.
fixrouters
,
subTree
)
...
...
tree_test.go
View file @
92086288
...
...
@@ -221,6 +221,18 @@ func TestAddTree4(t *testing.T) {
}
}
// Test for issue #1595
func
TestAddTree5
(
t
*
testing
.
T
)
{
tr
:=
NewTree
()
tr
.
AddRouter
(
"/v1/shop/:id"
,
"shopdetail"
)
tr
.
AddRouter
(
"/v1/shop/"
,
"shophome"
)
ctx
:=
context
.
NewContext
()
obj
:=
tr
.
Match
(
"/v1/shop/"
,
ctx
)
if
obj
==
nil
||
obj
.
(
string
)
!=
"shophome"
{
t
.
Fatal
(
"url /v1/shop/ need match router /v1/shop/ "
)
}
}
func
TestSplitPath
(
t
*
testing
.
T
)
{
a
:=
splitPath
(
""
)
if
len
(
a
)
!=
0
{
...
...
validation/validators.go
View file @
92086288
...
...
@@ -588,7 +588,7 @@ func (b Base64) GetLimitValue() interface{} {
}
// just for chinese mobile phone number
var
mobilePattern
=
regexp
.
MustCompile
(
"^((
\\
+86)|(86))?(1(([35][0-9])|[8][0-9]|[7][0679]|[4][579]))
\\
d{8}$"
)
var
mobilePattern
=
regexp
.
MustCompile
(
"^((
\\
+86)|(86))?(1(([35][0-9])|[8][0-9]|[7][067
8
9]|[4][579]))
\\
d{8}$"
)
// Mobile check struct
type
Mobile
struct
{
...
...
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