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
0c32255d
Commit
0c32255d
authored
Apr 14, 2016
by
JessonChan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
config test
parent
4cd24082
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
150 additions
and
32 deletions
+150
-32
config.go
config.go
+37
-32
config_test.go
config_test.go
+113
-0
No files found.
config.go
View file @
0c32255d
...
...
@@ -117,7 +117,30 @@ var (
)
func
init
()
{
BConfig
=
&
Config
{
BConfig
=
newBConfig
()
var
err
error
if
AppPath
,
err
=
filepath
.
Abs
(
filepath
.
Dir
(
os
.
Args
[
0
]));
err
!=
nil
{
panic
(
err
)
}
workPath
,
err
:=
os
.
Getwd
()
if
err
!=
nil
{
panic
(
err
)
}
appConfigPath
=
filepath
.
Join
(
workPath
,
"conf"
,
"app.conf"
)
if
!
utils
.
FileExists
(
appConfigPath
)
{
appConfigPath
=
filepath
.
Join
(
AppPath
,
"conf"
,
"app.conf"
)
if
!
utils
.
FileExists
(
appConfigPath
)
{
AppConfig
=
&
beegoAppConfig
{
innerConfig
:
config
.
NewFakeConfig
()}
return
}
}
if
err
=
parseConfig
(
appConfigPath
);
err
!=
nil
{
panic
(
err
)
}
}
func
newBConfig
()
*
Config
{
return
&
Config
{
AppName
:
"beego"
,
RunMode
:
DEV
,
RouterCaseSensitive
:
true
,
...
...
@@ -176,25 +199,6 @@ func init() {
Outputs
:
map
[
string
]
string
{
"console"
:
""
},
},
}
var
err
error
if
AppPath
,
err
=
filepath
.
Abs
(
filepath
.
Dir
(
os
.
Args
[
0
]));
err
!=
nil
{
panic
(
err
)
}
workPath
,
err
:=
os
.
Getwd
()
if
err
!=
nil
{
panic
(
err
)
}
appConfigPath
=
filepath
.
Join
(
workPath
,
"conf"
,
"app.conf"
)
if
!
utils
.
FileExists
(
appConfigPath
)
{
appConfigPath
=
filepath
.
Join
(
AppPath
,
"conf"
,
"app.conf"
)
if
!
utils
.
FileExists
(
appConfigPath
)
{
AppConfig
=
&
beegoAppConfig
{
innerConfig
:
config
.
NewFakeConfig
()}
return
}
}
if
err
=
parseConfig
(
appConfigPath
);
err
!=
nil
{
panic
(
err
)
}
}
// now only support ini, next will support json.
...
...
@@ -203,21 +207,23 @@ func parseConfig(appConfigPath string) (err error) {
if
err
!=
nil
{
return
err
}
return
assignConfig
(
AppConfig
)
}
func
assignConfig
(
ac
config
.
Configer
)
error
{
// set the run mode first
if
envRunMode
:=
os
.
Getenv
(
"BEEGO_RUNMODE"
);
envRunMode
!=
""
{
BConfig
.
RunMode
=
envRunMode
}
else
if
runMode
:=
AppConfig
.
String
(
"RunMode"
);
runMode
!=
""
{
}
else
if
runMode
:=
ac
.
String
(
"RunMode"
);
runMode
!=
""
{
BConfig
.
RunMode
=
runMode
}
for
_
,
i
:=
range
[]
interface
{}{
BConfig
,
&
BConfig
.
Listen
,
&
BConfig
.
WebConfig
,
&
BConfig
.
Log
,
&
BConfig
.
WebConfig
.
Session
}
{
assign
Config
(
i
,
AppConfig
)
assign
SingleConfig
(
i
,
ac
)
}
if
sd
:=
AppConfig
.
String
(
"StaticDir"
);
sd
!=
""
{
for
k
:=
range
BConfig
.
WebConfig
.
StaticDir
{
delete
(
BConfig
.
WebConfig
.
StaticDir
,
k
)
}
if
sd
:=
ac
.
String
(
"StaticDir"
);
sd
!=
""
{
BConfig
.
WebConfig
.
StaticDir
=
map
[
string
]
string
{}
sds
:=
strings
.
Fields
(
sd
)
for
_
,
v
:=
range
sds
{
if
url2fsmap
:=
strings
.
SplitN
(
v
,
":"
,
2
);
len
(
url2fsmap
)
==
2
{
...
...
@@ -228,7 +234,7 @@ func parseConfig(appConfigPath string) (err error) {
}
}
if
sgz
:=
AppConfig
.
String
(
"StaticExtensionsToGzip"
);
sgz
!=
""
{
if
sgz
:=
ac
.
String
(
"StaticExtensionsToGzip"
);
sgz
!=
""
{
extensions
:=
strings
.
Split
(
sgz
,
","
)
fileExts
:=
[]
string
{}
for
_
,
ext
:=
range
extensions
{
...
...
@@ -246,7 +252,7 @@ func parseConfig(appConfigPath string) (err error) {
}
}
if
lo
:=
AppConfig
.
String
(
"LogOutputs"
);
lo
!=
""
{
if
lo
:=
ac
.
String
(
"LogOutputs"
);
lo
!=
""
{
los
:=
strings
.
Split
(
lo
,
";"
)
for
_
,
v
:=
range
los
{
if
logType2Config
:=
strings
.
SplitN
(
v
,
","
,
2
);
len
(
logType2Config
)
==
2
{
...
...
@@ -260,7 +266,7 @@ func parseConfig(appConfigPath string) (err error) {
//init log
logs
.
Reset
()
for
adaptor
,
config
:=
range
BConfig
.
Log
.
Outputs
{
err
=
logs
.
SetLogger
(
adaptor
,
config
)
err
:
=
logs
.
SetLogger
(
adaptor
,
config
)
if
err
!=
nil
{
fmt
.
Fprintln
(
os
.
Stderr
,
"%s with the config `%s` got err:%s
\n
"
,
adaptor
,
config
,
err
)
}
...
...
@@ -270,7 +276,7 @@ func parseConfig(appConfigPath string) (err error) {
return
nil
}
func
assignConfig
(
p
interface
{},
ac
config
.
Configer
)
{
func
assign
Single
Config
(
p
interface
{},
ac
config
.
Configer
)
{
pt
:=
reflect
.
TypeOf
(
p
)
if
pt
.
Kind
()
!=
reflect
.
Ptr
{
return
...
...
@@ -295,9 +301,8 @@ func assignConfig(p interface{}, ac config.Configer) {
case
reflect
.
Bool
:
pf
.
SetBool
(
ac
.
DefaultBool
(
name
,
pf
.
Bool
()))
case
reflect
.
Struct
:
//do nothing here
default
:
logs
.
Critical
(
"beego not support such kind of config filed"
,
pt
.
Name
(),
pf
.
Kind
())
//do nothing here
}
}
...
...
config_test.go
View file @
0c32255d
...
...
@@ -15,7 +15,11 @@
package
beego
import
(
"encoding/json"
"reflect"
"testing"
"github.com/astaxie/beego/config"
)
func
TestDefaults
(
t
*
testing
.
T
)
{
...
...
@@ -27,3 +31,112 @@ func TestDefaults(t *testing.T) {
t
.
Errorf
(
"FlashName was not set to default."
)
}
}
func
TestAssignConfig_01
(
t
*
testing
.
T
)
{
_BConfig
:=
&
Config
{}
_BConfig
.
AppName
=
"beego_test"
jcf
:=
&
config
.
JSONConfig
{}
ac
,
_
:=
jcf
.
ParseData
([]
byte
(
`{"AppName":"beego_json"}`
))
assignSingleConfig
(
_BConfig
,
ac
)
if
_BConfig
.
AppName
!=
"beego_json"
{
t
.
Log
(
_BConfig
)
t
.
FailNow
()
}
}
func
TestAssignConfig_02
(
t
*
testing
.
T
)
{
_BConfig
:=
&
Config
{}
bs
,
_
:=
json
.
Marshal
(
newBConfig
())
jsonMap
:=
map
[
string
]
interface
{}{}
json
.
Unmarshal
(
bs
,
&
jsonMap
)
configMap
:=
map
[
string
]
interface
{}{}
for
k
,
v
:=
range
jsonMap
{
if
reflect
.
TypeOf
(
v
)
.
Kind
()
==
reflect
.
Map
{
for
k1
,
v1
:=
range
v
.
(
map
[
string
]
interface
{})
{
if
reflect
.
TypeOf
(
v1
)
.
Kind
()
==
reflect
.
Map
{
for
k2
,
v2
:=
range
v1
.
(
map
[
string
]
interface
{})
{
configMap
[
k2
]
=
v2
}
}
else
{
configMap
[
k1
]
=
v1
}
}
}
else
{
configMap
[
k
]
=
v
}
}
configMap
[
"MaxMemory"
]
=
1024
configMap
[
"Graceful"
]
=
true
configMap
[
"XSRFExpire"
]
=
32
configMap
[
"SessionProviderConfig"
]
=
"file"
configMap
[
"FileLineNum"
]
=
true
jcf
:=
&
config
.
JSONConfig
{}
bs
,
_
=
json
.
Marshal
(
configMap
)
ac
,
_
:=
jcf
.
ParseData
([]
byte
(
bs
))
for
_
,
i
:=
range
[]
interface
{}{
_BConfig
,
&
_BConfig
.
Listen
,
&
_BConfig
.
WebConfig
,
&
_BConfig
.
Log
,
&
_BConfig
.
WebConfig
.
Session
}
{
assignSingleConfig
(
i
,
ac
)
}
if
_BConfig
.
MaxMemory
!=
1024
{
t
.
Log
(
_BConfig
.
MaxMemory
)
t
.
FailNow
()
}
if
!
_BConfig
.
Listen
.
Graceful
{
t
.
Log
(
_BConfig
.
Listen
.
Graceful
)
t
.
FailNow
()
}
if
_BConfig
.
WebConfig
.
XSRFExpire
!=
32
{
t
.
Log
(
_BConfig
.
WebConfig
.
XSRFExpire
)
t
.
FailNow
()
}
if
_BConfig
.
WebConfig
.
Session
.
SessionProviderConfig
!=
"file"
{
t
.
Log
(
_BConfig
.
WebConfig
.
Session
.
SessionProviderConfig
)
t
.
FailNow
()
}
if
!
_BConfig
.
Log
.
FileLineNum
{
t
.
Log
(
_BConfig
.
Log
.
FileLineNum
)
t
.
FailNow
()
}
}
func
TestAssignConfig_03
(
t
*
testing
.
T
)
{
jcf
:=
&
config
.
JSONConfig
{}
ac
,
_
:=
jcf
.
ParseData
([]
byte
(
`{"AppName":"beego"}`
))
ac
.
Set
(
"AppName"
,
"test_app"
)
ac
.
Set
(
"RunMode"
,
"online"
)
ac
.
Set
(
"StaticDir"
,
"download:down download2:down2"
)
ac
.
Set
(
"StaticExtensionsToGzip"
,
".css,.js,.html,.jpg,.png"
)
ac
.
Set
(
"LogOutputs"
,
`"file": ""`
)
assignConfig
(
ac
)
//t.Logf("%#v",BConfig)
if
BConfig
.
AppName
!=
"test_app"
{
t
.
FailNow
()
}
if
BConfig
.
RunMode
!=
"online"
{
t
.
FailNow
()
}
if
BConfig
.
WebConfig
.
StaticDir
[
"download"
]
!=
"down"
{
t
.
FailNow
()
}
if
BConfig
.
WebConfig
.
StaticDir
[
"download2"
]
!=
"down2"
{
t
.
FailNow
()
}
if
len
(
BConfig
.
WebConfig
.
StaticExtensionsToGzip
)
!=
5
{
t
.
FailNow
()
}
if
_
,
ok
:=
BConfig
.
Log
.
Outputs
[
"file"
];
!
ok
{
t
.
FailNow
()
}
}
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