Skip to content

Commit

Permalink
Load configuration file.
Browse files Browse the repository at this point in the history
  • Loading branch information
liudng committed Sep 21, 2015
1 parent 833243f commit ec4fbff
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
2 changes: 1 addition & 1 deletion define.go
Expand Up @@ -5,7 +5,7 @@
package db

import (
"database/sql"
"database/sql"
)

// Alias of map[string]interface{}
Expand Down
54 changes: 54 additions & 0 deletions init.go
@@ -0,0 +1,54 @@
// Copyright 2014 The zhgo Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package db

import (
"github.com/zhgo/config"
)

// Server
type conf struct {
// Server
DB Server `json:"db"`

// module list
Modules map[string]confModule `json:"modules"`
}

// Module
type confModule struct {
// DB Server
DB Server `json:"db"`

// module name
Name string `json:"name"`
}

func init() {
var c conf

// Load config file
replaces := map[string]string{"{WorkingDir}": config.WorkingDir()}
config.NewConfig("zhgo.json").Replace(replaces).Parse(c)

// Module
if c.Modules != nil {
for k, v := range c.Modules {
if v.Name == "" {
v.Name = k
}

// db.Connections
if v.DB.DSN == "" && c.DB.DSN != "" {
v.DB = c.DB
}

if v.DB.DSN != "" {
Servers[v.Name] = &v.DB
}
}
}

}

0 comments on commit ec4fbff

Please sign in to comment.