/
config.go
62 lines (50 loc) · 1.46 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Created by LonelyPale at 2019-12-07
package config
import (
"log"
"os"
"github.com/LonelyPale/goutils/config"
)
const (
KeyDefaultDBName = "mongodb.default_db_name"
)
//支持 go-spring 属性绑定
type MongodbConfig struct {
URI string `value:"${mongodb.uri:=mongodb://localhost}"`
MinPoolSize int `value:"${mongodb.min_pool_size:=3}"`
MaxPoolSize int `value:"${mongodb.max_pool_size:=10}"`
Timeout int `value:"${mongodb.timeout:=10}"` //单位秒
EnableTransaction bool `value:"${mongodb.enable_transaction:=false}"`
DefaultDBName string `value:"${mongodb.default_db_name:=test}"`
}
func DefaultMongodbConfig() *MongodbConfig {
return &MongodbConfig{
URI: "mongodb://user:password@ip1:port,ip2:port/?replicaSet=replicaSet",
MinPoolSize: 3,
MaxPoolSize: 10,
Timeout: 10,
EnableTransaction: false,
DefaultDBName: "test",
}
}
type Config struct {
Mongodb *MongodbConfig
}
var (
CommonConfig = &Config{Mongodb: DefaultMongodbConfig()}
)
const DefaultConfigFile = "mongodb.conf.toml"
func ReadConfigFile(files ...string) *Config {
var configFile string
if len(files) == 0 || files[0] == "" {
configFile = "." + string(os.PathSeparator) + DefaultConfigFile
} else {
configFile = files[0]
}
log.Printf("read config file: %s\n", configFile)
err := config.ReadConfigFile(configFile, CommonConfig)
if err != nil {
log.Fatal(err)
}
return CommonConfig
}