// Copyright 2013-2014 Fuzamei tech Ltd. All rights reserved. package market // 本文件实现配置文件的管理 import ( "encoding/json" "io/ioutil" "log" "os" "path/filepath" ) type DsConf struct { BrokerId string User string PassWord string Url string Url2 string SaveDir string SaveDir1 string Run bool CfgFile string SymbolsFile string } type DBConf struct { DBDriver string // mysql, sqlite DSN string // dsn = fmt.Sprintf("root:fzm@1001@/%s?charset=%s", dbName, "utf8") DBName string // fzmdb } type ServerConf struct { DBConf DsMap map[string]*DsConf // 数据源配置 DataDir string // 数据保存地址 InssFile string // 合约列表文件 RpcAddr string // Rpc 监听地址 ":19528" StreamAddr string // 行情订阅地址 ":19529" } func WriteConf(fname string, conf *ServerConf) error { b, err := json.MarshalIndent(conf, "", " ") if err != nil { return err } ioutil.WriteFile(fname, b, os.ModePerm) return nil } func ReadConf(fname string) (*ServerConf, error) { f, err := os.Open(fname) if err != nil { return nil, err } defer f.Close() dec := json.NewDecoder(f) conf := &ServerConf{} err = dec.Decode(conf) if err != nil { return nil, err } return conf, nil } var DefaultServerConf = makeDefaultServerConf() func makeDefaultServerConf() *ServerConf { dir, err := filepath.Abs(filepath.Dir(os.Args[0])) if err != nil { log.Fatal(err) } dataDir := filepath.Join(dir, "fzmnew") lmaxConf := &DsConf{ User: "wave3366", PassWord: "Tg417395", Url: "https://trade.lmaxtrader.com", SaveDir: filepath.Join(dataDir, Lmax), Run: true, } oandaConf := &DsConf{ SaveDir: filepath.Join(dataDir, Oanda), Run: true, } easyForexConf := &DsConf{ SaveDir: filepath.Join(dataDir, EasyForex), Run: true, } ctpConf := &DsConf{ BrokerId: "66666", User: "1011000", PassWord: "317496", Url: "tcp://ctp1-front5.citicsf.com:41205", Url2: "tcp://ctp1-md5.citicsf.com:41213", SaveDir: filepath.Join(dataDir, Ctp), Run: true, } fixConf := &DsConf{ User: "20410767", PassWord: "491823", SaveDir: filepath.Join(dataDir, Fix), CfgFile: "fix-mk.cfg", SymbolsFile: "fix-symbols.json", Run: true, } saxoConf := &DsConf{ User: "", PassWord: "", SaveDir: filepath.Join(dataDir, Saxo), CfgFile: "saxo-mk.cfg", SymbolsFile: "saxo-symbols.csv", Run: true, } dzhConf := &DsConf{ Url: "115.29.238.128:19526", SaveDir: filepath.Join(dataDir, Dzh), Run: true, } m := map[string]*DsConf{ Lmax: lmaxConf, Oanda: oandaConf, EasyForex: easyForexConf, Ctp: ctpConf, Fix: fixConf, Dzh: dzhConf, Saxo: saxoConf, } dbConf := DBConf{ DBDriver: "sqlite3", // mysql DSN: "", // root:fzm@1001@/fzmdb?charset=utf8" DBName: "fzmnewdb", // } return &ServerConf{ DBConf: dbConf, DsMap: m, DataDir: dataDir, InssFile: "fzmnewinss.json", RpcAddr: ":19528", StreamAddr: ":19529", } }