tsconf.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 GeneralItemConf struct {
  12. Run bool
  13. Download bool
  14. }
  15. type TickServerConf struct {
  16. DBConf
  17. DsMap map[string]*GeneralItemConf // 数据源配置
  18. Url string // 数据源地址
  19. UrlSSL string
  20. FileServerAddr string
  21. DataDir string // 数据保存地址
  22. InssFile string // 合约列表文件
  23. RpcAddr string // Rpc 监听地址 ":19528"
  24. StreamAddr string // 行情订阅地址 ":19529"
  25. }
  26. func WriteTSConf(fname string, conf *TickServerConf) error {
  27. b, err := json.MarshalIndent(conf, "", " ")
  28. if err != nil {
  29. return err
  30. }
  31. ioutil.WriteFile(fname, b, os.ModePerm)
  32. return nil
  33. }
  34. func ReadTSConf(fname string) (*TickServerConf, error) {
  35. f, err := os.Open(fname)
  36. if err != nil {
  37. return nil, err
  38. }
  39. defer f.Close()
  40. dec := json.NewDecoder(f)
  41. conf := &TickServerConf{}
  42. err = dec.Decode(conf)
  43. if err != nil {
  44. return nil, err
  45. }
  46. return conf, nil
  47. }
  48. var DefaultTickServerConf = makeDefaultTickServerConf()
  49. func makeDefaultTickServerConf() *TickServerConf {
  50. dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
  51. if err != nil {
  52. log.Fatal(err)
  53. }
  54. dataDir := filepath.Join(dir, "fzmnew")
  55. lmaxConf := &GeneralItemConf{
  56. Run: true,
  57. }
  58. sinaConf := &GeneralItemConf{
  59. Run: true,
  60. }
  61. dzhConf := &GeneralItemConf{
  62. Run: true,
  63. }
  64. m := map[string]*GeneralItemConf{
  65. Lmax: lmaxConf,
  66. Sina: sinaConf,
  67. Dzh: dzhConf,
  68. }
  69. dbConf := DBConf{
  70. DBDriver: "sqlite3", // mysql
  71. DSN: "", // root:fzm@1001@/fzmdb?charset=utf8"
  72. DBName: "fzmnewdb", //
  73. }
  74. return &TickServerConf{
  75. DBConf: dbConf,
  76. DsMap: m,
  77. Url: "http://127.0.0.1:2107/",
  78. UrlSSL: "https://127.0.0.1:443/",
  79. FileServerAddr: "localhost:8080",
  80. DataDir: dataDir,
  81. InssFile: "fzmnewinss.json",
  82. RpcAddr: ":19528",
  83. StreamAddr: ":19529",
  84. }
  85. }