main.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright 2013-2014 Fuzamei tech Ltd. All rights reserved.
  2. package main
  3. import (
  4. "flag"
  5. "fmt"
  6. "log"
  7. "os"
  8. "runtime"
  9. "time"
  10. "tickserver/server/tick"
  11. "github.com/niniwzw/http2"
  12. )
  13. var confFile = flag.String("conf", "datasource.json", "配置文件")
  14. var debug = flag.Bool("d", false, "调试模式")
  15. var http2model = flag.Bool("http2", true, "是否开启http2模式")
  16. var snap = flag.String("snap", "no", "快照")
  17. var pid = flag.String("pid", "/var/log/ds.pid", "pid file")
  18. func logName() string {
  19. t := time.Now()
  20. return fmt.Sprintf("mk%4d-%02d-%02d-%02d-%02d-%02d.log", t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second())
  21. }
  22. func main() {
  23. runtime.GOMAXPROCS(runtime.NumCPU())
  24. flag.Parse()
  25. // set debug
  26. tick.SetDebug(*debug)
  27. if *debug {
  28. http2.VerboseLogs = true
  29. }
  30. conf, err := tick.ReadConf(*confFile)
  31. if err != nil {
  32. conf = tick.DefaultServerConf
  33. err = tick.WriteConf(*confFile, conf)
  34. if err != nil {
  35. log.Fatal(err)
  36. }
  37. log.Println("please config the conf file:", *confFile)
  38. return
  39. }
  40. tick.SetConf(conf)
  41. if runtime.GOOS == "windows" {
  42. *pid = "ds.pid"
  43. }
  44. if *snap != "no" {
  45. fp, err := lock(*pid + ".snap")
  46. if err != nil {
  47. log.Fatal(err)
  48. }
  49. defer fp.Close()
  50. tick.InitDb()
  51. if *snap == "all" {
  52. err = tick.SnapAll()
  53. } else {
  54. err = tick.SnapHour(*snap)
  55. }
  56. if err != nil {
  57. log.Println("snap error::", err)
  58. }
  59. return
  60. }
  61. fp, err := lock(*pid)
  62. if err != nil {
  63. log.Fatal(err)
  64. }
  65. defer fp.Close()
  66. log.Println("datasource start")
  67. tick.Init()
  68. log.Println("run server")
  69. err = tick.RunServer(conf.HttpAddr, logName(), *http2model)
  70. if err != nil {
  71. log.Println(err)
  72. }
  73. }
  74. func lock(path string) (*os.File, error) {
  75. file, err := os.Create(path)
  76. if err != nil {
  77. return nil, err
  78. }
  79. err = tick.LockFile(file)
  80. if err != nil {
  81. file.Close()
  82. return nil, err
  83. }
  84. return file, nil
  85. }