1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- // Copyright 2013-2014 Fuzamei tech Ltd. All rights reserved.
- package main
- import (
- "flag"
- "fmt"
- "log"
- "os"
- "runtime"
- "time"
- "tickserver/server/tick"
- "github.com/niniwzw/http2"
- )
- var confFile = flag.String("conf", "datasource.json", "配置文件")
- var debug = flag.Bool("d", false, "调试模式")
- var http2model = flag.Bool("http2", true, "是否开启http2模式")
- var snap = flag.String("snap", "no", "快照")
- var pid = flag.String("pid", "/var/log/ds.pid", "pid file")
- func logName() string {
- t := time.Now()
- return fmt.Sprintf("mk%4d-%02d-%02d-%02d-%02d-%02d.log", t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second())
- }
- func main() {
- runtime.GOMAXPROCS(runtime.NumCPU())
- flag.Parse()
- // set debug
- tick.SetDebug(*debug)
- if *debug {
- http2.VerboseLogs = true
- }
- conf, err := tick.ReadConf(*confFile)
- if err != nil {
- conf = tick.DefaultServerConf
- err = tick.WriteConf(*confFile, conf)
- if err != nil {
- log.Fatal(err)
- }
- log.Println("please config the conf file:", *confFile)
- return
- }
- tick.SetConf(conf)
- if runtime.GOOS == "windows" {
- *pid = "ds.pid"
- }
- if *snap != "no" {
- fp, err := lock(*pid + ".snap")
- if err != nil {
- log.Fatal(err)
- }
- defer fp.Close()
- tick.InitDb()
- if *snap == "all" {
- err = tick.SnapAll()
- } else {
- err = tick.SnapHour(*snap)
- }
- if err != nil {
- log.Println("snap error::", err)
- }
- return
- }
- fp, err := lock(*pid)
- if err != nil {
- log.Fatal(err)
- }
- defer fp.Close()
- log.Println("datasource start")
- tick.Init()
- log.Println("run server")
- err = tick.RunServer(conf.HttpAddr, logName(), *http2model)
- if err != nil {
- log.Println(err)
- }
- }
- func lock(path string) (*os.File, error) {
- file, err := os.Create(path)
- if err != nil {
- return nil, err
- }
- err = tick.LockFile(file)
- if err != nil {
- file.Close()
- return nil, err
- }
- return file, nil
- }
|