conf.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // Copyright 2013-2014 Fuzamei tech Ltd. All rights reserved.
  2. package tick
  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. Download bool
  21. CfgFile string
  22. SymbolsFile string
  23. Symbols string
  24. }
  25. type DBConf struct {
  26. DBDriver string // mysql, sqlite
  27. DSN string // dsn = fmt.Sprintf("root:fzm@1001@/%s?charset=%s", dbName, "utf8")
  28. DBName string // fzmdb
  29. }
  30. type ServerConf struct {
  31. DBConf
  32. DsMap map[string]*DsConf // 数据源配置
  33. DataDir string // 数据保存地址
  34. HttpAddr string // Rpc 监听地址 ":19528"
  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. Download: true,
  72. }
  73. oandaConf := &DsConf{
  74. SaveDir: filepath.Join(dataDir, Oanda),
  75. Run: true,
  76. Download: true,
  77. }
  78. easyForexConf := &DsConf{
  79. SaveDir: filepath.Join(dataDir, EasyForex),
  80. Run: true,
  81. Download: true,
  82. }
  83. ctpConf := &DsConf{
  84. BrokerId: "66666",
  85. User: "1011000",
  86. PassWord: "317496",
  87. Url: "tcp://ctp1-front5.citicsf.com:41205",
  88. Url2: "tcp://ctp1-md5.citicsf.com:41213",
  89. SaveDir: filepath.Join(dataDir, Ctp),
  90. Run: true,
  91. Download: true,
  92. }
  93. fixConf := &DsConf{
  94. User: "20410767",
  95. PassWord: "491823",
  96. SaveDir: filepath.Join(dataDir, Fix),
  97. CfgFile: "fix-mk.cfg",
  98. SymbolsFile: "fix-symbols.json",
  99. Run: true,
  100. Download: true,
  101. }
  102. saxoConf := &DsConf{
  103. User: "",
  104. PassWord: "",
  105. SaveDir: filepath.Join(dataDir, Saxo),
  106. CfgFile: "saxo-mk.cfg",
  107. SymbolsFile: "saxo-symbols.csv",
  108. Run: true,
  109. Download: true,
  110. }
  111. dzhConf := &DsConf{
  112. Url: "115.29.238.128:19526",
  113. SaveDir: filepath.Join(dataDir, Dzh),
  114. Run: true,
  115. Download: true,
  116. }
  117. sinaConf := &DsConf{
  118. SaveDir: filepath.Join(dataDir, Sina),
  119. Run: true,
  120. Download: true,
  121. }
  122. sinaFutureConf := &DsConf{
  123. SaveDir: filepath.Join(dataDir, SinaFuture),
  124. Run: true,
  125. Download: true,
  126. }
  127. m := map[string]*DsConf{
  128. Lmax: lmaxConf,
  129. Oanda: oandaConf,
  130. EasyForex: easyForexConf,
  131. Ctp: ctpConf,
  132. Fix: fixConf,
  133. Dzh: dzhConf,
  134. Saxo: saxoConf,
  135. Sina: sinaConf,
  136. SinaFuture: sinaFutureConf,
  137. }
  138. dbConf := DBConf{
  139. DBDriver: "mysql", // mysql
  140. DSN: "root:kingweb@tcp(localhost:3306)/tick_server?charset=utf8", // root:fzm@1001@/fzmdb?charset=utf8"
  141. DBName: "fzmnewdb", //
  142. }
  143. return &ServerConf{
  144. DBConf: dbConf,
  145. DsMap: m,
  146. DataDir: dataDir,
  147. HttpAddr: ":2107",
  148. }
  149. }