gigibox
2023-06-15 ff3cadba4a63cd1b63cd0e36358f49ccedb88bef
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
package config
 
import (
    "bytes"
    "encoding/json"
    "fmt"
    "io/ioutil"
    "os"
)
 
type Config struct {
    WebPort        string `json:"web_port"`        // 本地web服务绑定接口
    SqlAddr        string `json:"sql_addr"`        // 数据库ip地址, 不加端口号, 默认使用1433端口
    SqlDBName      string `json:"sql_db_name"`     // 数据库名称
    SqlUsername    string `json:"sql_username"`    // 数据库用户
    SqlPassword    string `json:"sql_password"`    // 数据库密码
    NsqServer      string `json:"nsq_server"`      // nsq TCP服务端地址
    NsqWebApi      string `json:"nsq_server"`      // nsq HTTP接口地址
    OrderTopic     string `json:"order_topic"`     // 订单上报的topic
    InventoryTopic string `json:"inventory_topic"` // 库存上报的topic
    SyncInterval   int    `json:"interval"`        // 同步的时间间隔, 单位/秒
}
 
const configPath = "config.json"
 
var Options Config
 
func DefaultConfig() {
    Options.WebPort = "10210"
    Options.SqlAddr = "127.0.0.1"
    Options.SqlDBName = "dbname"
    Options.SqlUsername = "sa"
    Options.SqlPassword = "123456"
    Options.NsqServer = "fai365.com:4150"
    Options.NsqWebApi = "http://121.31.232.83:9080/api/nsq/pub?topic=your_topic"
    Options.OrderTopic = "/province/city/factoryNo/kingdee_seOrder"
    Options.InventoryTopic = "/province/city/factoryNo/kingdee_inventory"
    Options.SyncInterval = 60
}
 
func Load() {
    if !pathExists(configPath) {
        DefaultConfig()
        SaveConfig()
    } else {
        file, _ := os.Open(configPath)
        defer file.Close()
 
        fd := json.NewDecoder(file)
 
        fd.Decode(&Options)
 
        fmt.Printf("%v\n", Options)
    }
}
 
func SaveConfig() error {
    b, err := json.Marshal(Options)
    if err == nil {
        var out bytes.Buffer
 
        err = json.Indent(&out, b, "", "    ")
        if err == nil {
            err = ioutil.WriteFile(configPath, out.Bytes(), 0644)
        }
    }
 
    return err
}
 
func pathExists(path string) bool {
    _, err := os.Stat(path)
    if err == nil {
        return true
    }
 
    fmt.Printf("%v", err.Error())
    return false
}