1
0
Fork 0

Config bootstrap

This commit is contained in:
Gregory Eremin 2015-01-18 18:42:43 +07:00
parent f963ccc672
commit b02992c13d
3 changed files with 44 additions and 0 deletions

View File

@ -48,6 +48,7 @@ func (c *config) dump() ([]byte, error) {
if err = json.Indent(&out, b, "", " "); err != nil {
return nil, err
}
out.WriteByte('\n')
return out.Bytes(), nil
}

14
demo/config.json Normal file
View File

@ -0,0 +1,14 @@
{
"app_name": "",
"build_number": 0,
"enable_sign_in": false,
"database_driver": "",
"database_config": {
"hostname": "",
"port": 0,
"username": "",
"password": "",
"database": ""
},
"sensitive_data": ""
}

View File

@ -33,10 +33,15 @@ func New(conf interface{}) *Manager {
path: configPath,
},
}
mgr.bootstrap()
return mgr
}
func (m *Manager) Config() interface{} {
return m.conf.config
}
func (m *Manager) StartServer() {
srv := &server{
manager: m,
@ -44,3 +49,27 @@ func (m *Manager) StartServer() {
}
srv.start()
}
func (m *Manager) bootstrap() {
if m.file.isExist() {
b, err := m.file.read()
if err != nil {
panic(err)
}
err = m.conf.load(b)
if err != nil {
panic(err)
}
} else {
b, err := m.conf.dump()
if err != nil {
panic(err)
}
err = m.file.write(b)
if err != nil {
panic(err)
}
}
}