forked from GoBelieveIO/im_service
/
config.go
164 lines (141 loc) · 4.21 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/**
* Copyright (c) 2014-2015, GoBelieve
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package main
import "strconv"
import "log"
import "strings"
import "github.com/jimlawless/cfg"
type Config struct {
port int
mysqldb_datasource string
redis_address string
http_listen_address string
socket_io_address string
storage_addrs []string
route_addrs []string
}
type StorageConfig struct {
listen string
storage_root string
mysqldb_datasource string
redis_address string
sync_listen string
master_address string
}
type RouteConfig struct {
listen string
}
type APIConfig struct {
port int
redis_address string
mysqldb_datasource string
appdb_datasource string
//发送群组通知消息
im_url string
}
func get_int(app_cfg map[string]string, key string) int {
concurrency, present := app_cfg[key]
if !present {
log.Fatalf("key:%s non exist", key)
}
n, err := strconv.Atoi(concurrency)
if err != nil {
log.Fatalf("key:%s is't integer", key)
}
return n
}
func get_string(app_cfg map[string]string, key string) string {
concurrency, present := app_cfg[key]
if !present {
log.Fatalf("key:%s non exist", key)
}
return concurrency
}
func get_opt_string(app_cfg map[string]string, key string) string {
concurrency, present := app_cfg[key]
if !present {
return ""
}
return concurrency
}
func read_cfg(cfg_path string) *Config {
config := new(Config)
app_cfg := make(map[string]string)
err := cfg.Load(cfg_path, app_cfg)
if err != nil {
log.Fatal(err)
}
config.port = get_int(app_cfg, "port")
config.http_listen_address = get_string(app_cfg, "http_listen_address")
config.redis_address = get_string(app_cfg, "redis_address")
config.mysqldb_datasource = get_string(app_cfg, "mysqldb_source")
config.socket_io_address = get_string(app_cfg, "socket_io_address")
str := get_string(app_cfg, "storage_pool")
array := strings.Split(str, " ")
config.storage_addrs = array
if len(config.storage_addrs) == 0 {
log.Fatal("storage pool config")
}
str = get_string(app_cfg, "route_pool")
array = strings.Split(str, " ")
config.route_addrs = array
if len(config.route_addrs) == 0 {
log.Fatal("route pool config")
}
return config
}
func read_storage_cfg(cfg_path string) *StorageConfig {
config := new(StorageConfig)
app_cfg := make(map[string]string)
err := cfg.Load(cfg_path, app_cfg)
if err != nil {
log.Fatal(err)
}
config.listen = get_string(app_cfg, "listen")
config.storage_root = get_string(app_cfg, "storage_root")
config.redis_address = get_string(app_cfg, "redis_address")
config.mysqldb_datasource = get_string(app_cfg, "mysqldb_source")
config.sync_listen = get_string(app_cfg, "sync_listen")
config.master_address = get_opt_string(app_cfg, "master_address")
return config
}
func read_route_cfg(cfg_path string) *RouteConfig {
config := new(RouteConfig)
app_cfg := make(map[string]string)
err := cfg.Load(cfg_path, app_cfg)
if err != nil {
log.Fatal(err)
}
config.listen = get_string(app_cfg, "listen")
return config
}
func read_api_cfg(cfg_path string) *APIConfig {
config := new(APIConfig)
app_cfg := make(map[string]string)
err := cfg.Load(cfg_path, app_cfg)
if err != nil {
log.Fatal(err)
}
config.port = get_int(app_cfg, "port")
config.redis_address = get_string(app_cfg, "redis_address")
config.mysqldb_datasource = get_string(app_cfg, "mysqldb_source")
config.appdb_datasource = get_string(app_cfg, "appdb_source")
config.im_url = get_string(app_cfg, "im_url")
return config
}