完善配置管理功能

This commit is contained in:
John 2018-01-04 11:59:35 +08:00
parent 3e2a3cf2f2
commit 48c2878885
3 changed files with 90 additions and 74 deletions

149
README.MD
View File

@ -117,85 +117,88 @@ gf框架基于模块化设计实现了通用的HTTP/TCP/UDP Server并实
<!--
## 配置 ## 配置
1. 默认读取文件运行目录的config.json文件 1. 默认读取文件运行目录的config.json文件
1. 支持多配置文件管理,默认配置文件目录为当前运行目录; 1. 支持多配置文件管理,默认配置文件目录为当前运行目录;
1. config.json配置文件说明 1. config.json配置文件说明
```json ```json
{ {
"viewpath" : "视图文件存放路径", "viewpath" : "视图文件存放路径",
"database" : { "routings" : {
"数据库分组名称" : [ "dispatch" : {
{ "解析匹配规则1" : "解析替换规则1",
"host" : "地址", "解析匹配规则2" : "解析替换规则2"
"port" : "端口", },
"user" : "账号", "patch" : {
"pass" : "密码", "打包匹配规则1" : "打包替换规则1",
"name" : "数据库名称", "打包匹配规则2" : "打包替换规则2"
"type" : "数据库类型(mysql|pgsql)",
"role" : "数据库角色(master|slave)",
"charset" : "字符编码",
"priority" : "数据库优先级(负载均衡处理)"
}
]
} }
},
"database" : {
"数据库分组名称" : [
{
"host" : "地址",
"port" : "端口",
"user" : "账号",
"pass" : "密码",
"name" : "数据库名称",
"type" : "数据库类型(mysql|pgsql)",
"role" : "数据库角色(master|slave)",
"charset" : "字符编码",
"priority" : "数据库优先级(负载均衡处理)"
}
]
} }
``` }
```
1. config.json示例文件 1. config.json示例文件
```json ```json
{ {
"viewpath" : "/home/john/Workspace/Go/gitee.com/johng/gf/geg/frame/mvc/view", "viewpath" : "/home/www/templates/",
"database" : { "routines" : {
"default" : [ "dispatch" : {
{ "\/list\/page\/(\d+)[\/\?]*" : "/list?page=$1&"
"host" : "127.0.0.1",
"port" : "3306",
"user" : "root",
"pass" : "123456",
"name" : "test",
"type" : "mysql",
"role" : "master",
"charset" : "utf8",
"priority" : "100"
},
{
"host" : "127.0.0.1",
"port" : "3306",
"user" : "root",
"pass" : "123456",
"name" : "test",
"type" : "mysql",
"role" : "slave",
"charset" : "utf8",
"priority" : "100"
}
],
"ucenter" : [
{
"host" : "127.0.0.1",
"port" : "3306",
"user" : "root",
"pass" : "123456",
"name" : "ucenter",
"type" : "mysql",
"role" : "master",
"charset" : "utf8",
"priority" : "100"
}
]
} }
},
"database" : {
"default" : [
{
"host" : "127.0.0.1",
"port" : "3306",
"user" : "root",
"pass" : "123456",
"name" : "test",
"type" : "mysql",
"role" : "master",
"charset" : "utf8",
"priority" : "100"
},
{
"host" : "127.0.0.1",
"port" : "3306",
"user" : "root",
"pass" : "123456",
"name" : "test",
"type" : "mysql",
"role" : "slave",
"charset" : "utf8",
"priority" : "100"
}
],
"ucenter" : [
{
"host" : "127.0.0.1",
"port" : "3306",
"user" : "root",
"pass" : "123456",
"name" : "ucenter",
"type" : "mysql",
"role" : "master",
"charset" : "utf8",
"priority" : "100"
}
]
} }
``` }
--> ```

View File

@ -12,6 +12,7 @@ import (
"gitee.com/johng/gf/g/container/gmap" "gitee.com/johng/gf/g/container/gmap"
"gitee.com/johng/gf/g/encoding/gjson" "gitee.com/johng/gf/g/encoding/gjson"
"sync" "sync"
"strings"
) )
const ( const (
@ -48,7 +49,10 @@ func (c *Config) filePath(files []string) string {
// 设置配置管理器的配置文件存放目录绝对路径 // 设置配置管理器的配置文件存放目录绝对路径
func (c *Config) SetPath(path string) { func (c *Config) SetPath(path string) {
c.mu.Lock() c.mu.Lock()
c.path = path if strings.Compare(c.path, path) != 0 {
c.path = path
c.jsons = gmap.NewStringInterfaceMap()
}
c.mu.Unlock() c.mu.Unlock()
} }

View File

@ -0,0 +1,9 @@
package demo
import "gitee.com/johng/gf/g/net/ghttp"
func init() {
ghttp.GetServer().BindHandler("/config", func (r *ghttp.Request) {
r.Response.WriteString("Apple")
})
}