conf.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright 2013-2014 Fuzamei tech Ltd. All rights reserved.
  2. package market
  3. // 本文件实现配置文件的管理
  4. import (
  5. "encoding/json"
  6. "io/ioutil"
  7. "log"
  8. "os"
  9. "path/filepath"
  10. )
  11. type DsConf struct {
  12. BrokerId string
  13. User string
  14. PassWord string
  15. Url string
  16. Url2 string
  17. SaveDir string
  18. SaveDir1 string
  19. Run bool
  20. CfgFile string
  21. SymbolsFile string
  22. }
  23. type DBConf struct {
  24. DBDriver string // mysql, sqlite
  25. DSN string // dsn = fmt.Sprintf("root:fzm@1001@/%s?charset=%s", dbName, "utf8")
  26. DBName string // fzmdb
  27. }
  28. type ServerConf struct {
  29. DBConf
  30. DsMap map[string]*DsConf // 数据源配置
  31. DataDir string // 数据保存地址
  32. InssFile string // 合约列表文件
  33. RpcAddr string // Rpc 监听地址 ":19528"
  34. StreamAddr string // 行情订阅地址 ":19529"
  35. }
  36. func WriteConf(fname string, conf *ServerConf) error {
  37. b, err := json.MarshalIndent(conf, "", " ")
  38. if err != nil {
  39. return err
  40. }
  41. ioutil.WriteFile(fname, b, os.ModePerm)
  42. return nil
  43. }
  44. func ReadConf(fname string) (*ServerConf, error) {
  45. f, err := os.Open(fname)
  46. if err != nil {
  47. return nil, err
  48. }
  49. defer f.Close()
  50. dec := json.NewDecoder(f)
  51. conf := &ServerConf{}
  52. err = dec.Decode(conf)
  53. if err != nil {
  54. return nil, err
  55. }
  56. return conf, nil
  57. }
  58. var DefaultServerConf = makeDefaultServerConf()
  59. func makeDefaultServerConf() *ServerConf {
  60. dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
  61. if err != nil {
  62. log.Fatal(err)
  63. }
  64. dataDir := filepath.Join(dir, "fzmnew")
  65. lmaxConf := &DsConf{
  66. User: "wave3366",
  67. PassWord: "Tg417395",
  68. Url: "https://trade.lmaxtrader.com",
  69. SaveDir: filepath.Join(dataDir, Lmax),
  70. Run: true,
  71. }
  72. oandaConf := &DsConf{
  73. SaveDir: filepath.Join(dataDir, Oanda),
  74. Run: true,
  75. }
  76. easyForexConf := &DsConf{
  77. SaveDir: filepath.Join(dataDir, EasyForex),
  78. Run: true,
  79. }
  80. ctpConf := &DsConf{
  81. BrokerId: "66666",
  82. User: "1011000",
  83. PassWord: "317496",
  84. Url: "tcp://ctp1-front5.citicsf.com:41205",
  85. Url2: "tcp://ctp1-md5.citicsf.com:41213",
  86. SaveDir: filepath.Join(dataDir, Ctp),
  87. Run: true,
  88. }
  89. fixConf := &DsConf{
  90. User: "20410767",
  91. PassWord: "491823",
  92. SaveDir: filepath.Join(dataDir, Fix),
  93. CfgFile: "fix-mk.cfg",
  94. SymbolsFile: "fix-symbols.json",
  95. Run: true,
  96. }
  97. saxoConf := &DsConf{
  98. User: "",
  99. PassWord: "",
  100. SaveDir: filepath.Join(dataDir, Saxo),
  101. CfgFile: "saxo-mk.cfg",
  102. SymbolsFile: "saxo-symbols.csv",
  103. Run: true,
  104. }
  105. dzhConf := &DsConf{
  106. Url: "115.29.238.128:19526",
  107. SaveDir: filepath.Join(dataDir, Dzh),
  108. Run: true,
  109. }
  110. m := map[string]*DsConf{
  111. Lmax: lmaxConf,
  112. Oanda: oandaConf,
  113. EasyForex: easyForexConf,
  114. Ctp: ctpConf,
  115. Fix: fixConf,
  116. Dzh: dzhConf,
  117. Saxo: saxoConf,
  118. }
  119. dbConf := DBConf{
  120. DBDriver: "sqlite3", // mysql
  121. DSN: "", // root:fzm@1001@/fzmdb?charset=utf8"
  122. DBName: "fzmnewdb", //
  123. }
  124. return &ServerConf{
  125. DBConf: dbConf,
  126. DsMap: m,
  127. DataDir: dataDir,
  128. InssFile: "fzmnewinss.json",
  129. RpcAddr: ":19528",
  130. StreamAddr: ":19529",
  131. }
  132. }