Skip to content
Toggle navigation
Toggle navigation
This project
Loading...
Sign in
张磊
/
FileStorageBeego
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
14114018
authored
2014-10-24 19:03:27 +0800
by
astaxie
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
config ini support include
1 parent
8ac2b9bf
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
52 additions
and
8 deletions
config.go
config/ini.go
config.go
View file @
1411401
...
...
@@ -88,13 +88,13 @@ type beegoAppConfig struct {
innerConfig
config
.
ConfigContainer
}
func
newAppConfig
(
AppConfigProvider
,
AppConfigPath
string
)
*
beegoAppConfig
{
func
newAppConfig
(
AppConfigProvider
,
AppConfigPath
string
)
(
*
beegoAppConfig
,
error
)
{
ac
,
err
:=
config
.
NewConfig
(
AppConfigProvider
,
AppConfigPath
)
if
err
!=
nil
{
ac
=
config
.
NewFakeConfig
()
return
nil
,
err
}
rac
:=
&
beegoAppConfig
{
ac
}
return
rac
return
rac
,
nil
}
func
(
b
*
beegoAppConfig
)
Set
(
key
,
val
string
)
error
{
...
...
@@ -281,15 +281,19 @@ func init() {
err
=
ParseConfig
()
if
err
!=
nil
&&
!
os
.
IsNotExist
(
err
)
{
// for init if doesn't have app.conf will not panic
Info
(
err
)
ac
:=
config
.
NewFakeConfig
()
AppConfig
=
&
beegoAppConfig
{
ac
}
Warning
(
err
)
}
}
// ParseConfig parsed default config file.
// now only support ini, next will support json.
func
ParseConfig
()
(
err
error
)
{
AppConfig
=
newAppConfig
(
AppConfigProvider
,
AppConfigPath
)
AppConfig
,
err
=
newAppConfig
(
AppConfigProvider
,
AppConfigPath
)
if
err
!=
nil
{
return
err
}
envRunMode
:=
os
.
Getenv
(
"BEEGO_RUNMODE"
)
// set the runmode first
if
envRunMode
!=
""
{
...
...
config/ini.go
View file @
1411401
...
...
@@ -48,6 +48,10 @@ type IniConfig struct {
// ParseFile creates a new Config and parses the file configuration from the named file.
func
(
ini
*
IniConfig
)
Parse
(
name
string
)
(
ConfigContainer
,
error
)
{
return
ini
.
parseFile
(
name
)
}
func
(
ini
*
IniConfig
)
parseFile
(
name
string
)
(
*
IniConfigContainer
,
error
)
{
file
,
err
:=
os
.
Open
(
name
)
if
err
!=
nil
{
return
nil
,
err
...
...
@@ -66,6 +70,7 @@ func (ini *IniConfig) Parse(name string) (ConfigContainer, error) {
var
comment
bytes
.
Buffer
buf
:=
bufio
.
NewReader
(
file
)
// check the BOM
head
,
err
:=
buf
.
Peek
(
3
)
if
err
==
nil
&&
head
[
0
]
==
239
&&
head
[
1
]
==
187
&&
head
[
2
]
==
191
{
for
i
:=
1
;
i
<=
3
;
i
++
{
...
...
@@ -114,13 +119,48 @@ func (ini *IniConfig) Parse(name string) (ConfigContainer, error) {
cfg
.
data
[
section
]
=
make
(
map
[
string
]
string
)
}
keyValue
:=
bytes
.
SplitN
(
line
,
bEqual
,
2
)
key
:=
string
(
bytes
.
TrimSpace
(
keyValue
[
0
]))
// key name case insensitive
key
=
strings
.
ToLower
(
key
)
// handle include "other.conf"
if
len
(
keyValue
)
==
1
&&
strings
.
HasPrefix
(
key
,
"include"
)
{
includefiles
:=
strings
.
Fields
(
key
)
if
includefiles
[
0
]
==
"include"
&&
len
(
includefiles
)
==
2
{
otherfile
:=
strings
.
Trim
(
includefiles
[
1
],
"
\"
"
)
if
!
path
.
IsAbs
(
otherfile
)
{
otherfile
=
path
.
Join
(
path
.
Dir
(
name
),
otherfile
)
}
i
,
err
:=
ini
.
parseFile
(
otherfile
)
if
err
!=
nil
{
return
nil
,
err
}
for
sec
,
dt
:=
range
i
.
data
{
if
_
,
ok
:=
cfg
.
data
[
sec
];
!
ok
{
cfg
.
data
[
sec
]
=
make
(
map
[
string
]
string
)
}
for
k
,
v
:=
range
dt
{
cfg
.
data
[
sec
][
k
]
=
v
}
}
for
sec
,
comm
:=
range
i
.
sectionComment
{
cfg
.
sectionComment
[
sec
]
=
comm
}
for
k
,
comm
:=
range
i
.
keyComment
{
cfg
.
keyComment
[
k
]
=
comm
}
continue
}
}
if
len
(
keyValue
)
!=
2
{
return
nil
,
errors
.
New
(
"read the content error:
\"
"
+
string
(
line
)
+
"
\"
, should key = val"
)
}
val
:=
bytes
.
TrimSpace
(
keyValue
[
1
])
if
bytes
.
HasPrefix
(
val
,
bDQuote
)
{
val
=
bytes
.
Trim
(
val
,
`"`
)
}
key
:=
string
(
bytes
.
TrimSpace
(
keyValue
[
0
]))
// key name case insensitive
key
=
strings
.
ToLower
(
key
)
cfg
.
data
[
section
][
key
]
=
string
(
val
)
if
comment
.
Len
()
>
0
{
cfg
.
keyComment
[
section
+
"."
+
key
]
=
comment
.
String
()
...
...
Write
Preview
Styling with
Markdown
is supported
Attach a file
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 post a comment