main.go 1.95 KB
package main

import (
	"HttpServer/conf"
	"HttpServer/jsonconf"
	"HttpServer/logic"
	"HttpServer/redishandler"
	"common/logger"
	"flag"
	"os"
	"runtime/pprof"
	"time"
)

func InitLogger(file string, lvl int) {
	logger.New(file, lvl, logger.Rotate{Size: logger.GB, Expired: time.Hour * 24 * 7, Interval: time.Hour * 24})
}

func main() {
	//defer utils.PrintPanicStack()
	filename := flag.String("conf_path", "./conf/world.xml", "config file path")
	logpath := flag.String("logpath", "./log", "log file path")
	cpuprofile := flag.String("cpuprofile", "", "write cpu profile to file")
	secs := flag.Int("exitsecs", 0, "how many secs to exit, defaut 0 to never exit")
	lvl := flag.Int("lvl", 6, "log level default -debug")
	flag.Parse()
	InitLogger(*logpath+"/world.log", *lvl)
	logger.Info("config file path:%v", *filename)

	if *cpuprofile != "" {
		f, err := os.Create(*cpuprofile)
		if err != nil {
			logger.Info("open cpu profile error %v:", err)
		} else {
			pprof.StartCPUProfile(f)
			defer pprof.StopCPUProfile()
		}
	}
	var ch chan int = make(chan int)
	go waitFor(ch, *secs)

	err := conf.LoadConf(*filename)
	if err != nil {
		logger.Info("error load conf fail %v:", err)
		return
	}

	err = redishandler.Init()
	if err != nil {
		logger.Error("err init redis err=%v", err)
		return
	}

	/*err = mysql.InitMysql()
	if err != nil {
		logger.Error("err init mysql err=%v", err)
		return
	}*/

	logger.Info("before LoadJsonConf")
	err = jsonconf.LoadJsonConf()
	if err != nil {
		logger.Error("err load jsonconfl err=%v", err)
		return
	}

	//測試
	//logic.Test()
	//go logic.StartHttpServe()
	//go logic.StartHttpTicker()
	//time.Sleep(time.Duration(2) * time.Second)
	go logic.ClearTaskAndAchieve()
	//go logic.Testsendhttp()

	select {
	case _ = <-ch:
		logger.Info("---I'm done----")
		break
	}
}

func waitFor(ch chan int, secs int) {
	if secs <= 0 {
		return
	}
	time.Sleep(time.Duration(secs) * time.Second)

	logger.Info("-------------ready to exit--------------------")
	ch <- 1
}