Exemplo n.º 1
0
func main() {

	logLevel := flag.String("log_level", "info", "logger level")
	// parse cmd-line arguments
	flag.Parse()
	log.SetLevel(*logLevel)
	logger.Infof("comet ver: \"%s\" start", ver.Version)

	// init config
	if err := InitConfig(); err != nil {
		logger.Errorf("InitConfig() error(%v)", err)
		return
	}
	//init db config
	if err := db.InitConfig(); err != nil {
		logger.Error("db-InitConfig() wrror(%v)", err)
		return
	}

	db.InitDB()
	defer db.CloseDB()

	// set max routine
	runtime.GOMAXPROCS(Conf.MaxProc)
	// start pprof
	perf.Init(Conf.PprofBind)
	// create channel
	// if process exit, close channel
	UserChannel = NewChannelList()
	defer UserChannel.Close()
	// start stats
	StartStats()
	// start rpc
	StartRPC()
	// start comet
	StartComet()
	// init zookeeper
	zkConn, err := InitZK()
	if err != nil {
		logger.Errorf("InitZookeeper() error(%v)", err)
		return
	}
	// if process exit, close zk
	defer zkConn.Close()
	// init process
	// sleep one second, let the listen start
	time.Sleep(time.Second)
	if err = process.Init(Conf.User, Conf.Dir, Conf.PidFile); err != nil {
		logger.Errorf("process.Init(\"%s\", \"%s\", \"%s\") error(%v)", Conf.User, Conf.Dir, Conf.PidFile, err)
		return
	}
	// init signals, block wait signals
	signalCH := InitSignal()
	HandleSignal(signalCH)
	// exit
	logger.Info("comet stop")
}
Exemplo n.º 2
0
func main() {
	logLevel := flag.String("log_level", "info", "logger level")
	flag.Parse()
	log.SetLevel(*logLevel)
	logger.Infof("message ver: \"%s\" start", ver.Version)

	if err := InitConfig(); err != nil {
		logger.Errorf("InitConfig() error(%v)", err)
		return
	}
	// Set max routine
	runtime.GOMAXPROCS(Conf.MaxProc)
	// start pprof http
	perf.Init(Conf.PprofBind)
	// Initialize redis
	if err := InitStorage(); err != nil {
		logger.Errorf("InitStorage() error(%v)", err)
		return
	}
	// init rpc service
	InitRPC()
	// init zookeeper
	zk, err := InitZK()
	if err != nil {
		logger.Errorf("InitZK() error(%v)", err)
		if zk != nil {
			zk.Close()
		}
		return
	}
	// if process exit, close zk
	defer zk.Close()
	// init process
	// sleep one second, let the listen start
	time.Sleep(time.Second)
	if err = process.Init(Conf.User, Conf.Dir, Conf.PidFile); err != nil {
		logger.Errorf("process.Init(\"%s\", \"%s\", \"%s\") error(%v)", Conf.User, Conf.Dir, Conf.PidFile, err)
		return
	}
	// init signals, block wait signals
	sig := InitSignal()
	HandleSignal(sig)
	// exit
	logger.Info("message stop")
}
Exemplo n.º 3
0
Arquivo: main.go Projeto: toyang/wide
// The only one init function in Wide.
func init() {
	confPath := flag.String("conf", "conf/wide.json", "path of wide.json")
	confIP := flag.String("ip", "", "this will overwrite Wide.IP if specified")
	confPort := flag.String("port", "", "this will overwrite Wide.Port if specified")
	confServer := flag.String("server", "", "this will overwrite Wide.Server if specified")
	confLogLevel := flag.String("log_level", "", "this will overwrite Wide.LogLevel if specified")
	confStaticServer := flag.String("static_server", "", "this will overwrite Wide.StaticServer if specified")
	confContext := flag.String("context", "", "this will overwrite Wide.Context if specified")
	confChannel := flag.String("channel", "", "this will overwrite Wide.Channel if specified")
	confStat := flag.Bool("stat", false, "whether report statistics periodically")
	confDocker := flag.Bool("docker", false, "whether run in a docker container")
	confPlayground := flag.String("playground", "", "this will overwrite Wide.Playground if specified")

	flag.Parse()

	log.SetLevel("warn")
	logger = log.NewLogger(os.Stdout)

	wd := util.OS.Pwd()
	if strings.HasPrefix(wd, os.TempDir()) {
		logger.Error("Don't run Wide in OS' temp directory or with `go run`")

		os.Exit(-1)
	}

	i18n.Load()

	event.Load()

	conf.Load(*confPath, *confIP, *confPort, *confServer, *confLogLevel, *confStaticServer, *confContext, *confChannel,
		*confPlayground, *confDocker)

	conf.FixedTimeCheckEnv()

	session.FixedTimeSave()
	session.FixedTimeRelease()

	if *confStat {
		session.FixedTimeReport()
	}

	logger.Debug("host ["+runtime.Version()+", "+runtime.GOOS+"_"+runtime.GOARCH+"], cross-compilation ",
		util.Go.GetCrossPlatforms())
}
Exemplo n.º 4
0
Arquivo: wide.go Projeto: nivance/wide
func initWide(confPath, confIP, confPort, confServer, confLogLevel, confStaticServer, confContext, confChannel,
	confPlayground string, confDocker bool) {
	bytes, err := ioutil.ReadFile(confPath)
	if nil != err {
		logger.Error(err)

		os.Exit(-1)
	}

	Wide = &conf{}

	err = json.Unmarshal(bytes, Wide)
	if err != nil {
		logger.Error("Parses [wide.json] error: ", err)

		os.Exit(-1)
	}

	// Logging Level
	log.SetLevel(Wide.LogLevel)
	if "" != confLogLevel {
		Wide.LogLevel = confLogLevel
		log.SetLevel(confLogLevel)
	}

	logger.Debug("Conf: \n" + string(bytes))

	// Working Driectory
	Wide.WD = util.OS.Pwd()
	logger.Debugf("${pwd} [%s]", Wide.WD)

	// User Home
	home, err := util.OS.Home()
	if nil != err {
		logger.Error("Can't get user's home, please report this issue to developer", err)

		os.Exit(-1)
	}

	logger.Debugf("${user.home} [%s]", home)

	// Playground Directory
	Wide.Playground = strings.Replace(Wide.Playground, "${home}", home, 1)
	if "" != confPlayground {
		Wide.Playground = confPlayground
	}

	if !util.File.IsExist(Wide.Playground) {
		if err := os.Mkdir(Wide.Playground, 0775); nil != err {
			logger.Errorf("Create Playground [%s] error", err)

			os.Exit(-1)
		}
	}

	// IP
	ip, err := util.Net.LocalIP()
	if err != nil {
		logger.Error(err)

		os.Exit(-1)
	}

	logger.Debugf("${ip} [%s]", ip)

	Docker = confDocker

	if "" != confIP {
		ip = confIP
	}

	Wide.IP = strings.Replace(Wide.IP, "${ip}", ip, 1)

	if "" != confPort {
		Wide.Port = confPort
	}

	// Server
	Wide.Server = strings.Replace(Wide.Server, "{IP}", Wide.IP, 1)
	Wide.Server = strings.Replace(Wide.Server, "{Port}", Wide.Port, 1)
	if "" != confServer {
		Wide.Server = confServer
	}

	// Static Server
	Wide.StaticServer = strings.Replace(Wide.StaticServer, "{IP}", Wide.IP, 1)
	Wide.StaticServer = strings.Replace(Wide.StaticServer, "{Port}", Wide.Port, 1)
	if "" != confStaticServer {
		Wide.StaticServer = confStaticServer
	}

	// Context
	if "" != confContext {
		Wide.Context = confContext
	}

	Wide.StaticResourceVersion = strings.Replace(Wide.StaticResourceVersion, "${time}", strconv.FormatInt(time.Now().UnixNano(), 10), 1)

	// Channel
	Wide.Channel = strings.Replace(Wide.Channel, "{IP}", Wide.IP, 1)
	Wide.Channel = strings.Replace(Wide.Channel, "{Port}", Wide.Port, 1)
	if "" != confChannel {
		Wide.Channel = confChannel
	}
}
Exemplo n.º 5
0
func main() {
	var err error
	// Parse cmd-line arguments
	logLevel := flag.String("log_level", "info", "logger level")
	flag.Parse()
	log.SetLevel(*logLevel)

	logger.Infof("web ver: \"%s\" start", ver.Version)

	if err = app.InitConfig(); err != nil {
		logger.Errorf("InitConfig() error(%v)", err)
		return
	}
	//init db config
	if err = db.InitConfig(); err != nil {
		logger.Error("db-InitConfig() error(%v)", err)
		return
	}
	// Set max routine
	runtime.GOMAXPROCS(app.Conf.MaxProc)
	// init zookeeper
	zkConn, err := InitZK()
	if err != nil {
		logger.Errorf("InitZookeeper() error(%v)", err)
		return
	}
	// if process exit, close zk
	defer zkConn.Close()
	// start pprof http
	perf.Init(app.Conf.PprofBind)
	// Init network router
	if app.Conf.Router != "" {
		if err := InitRouter(); err != nil {
			logger.Errorf("InitRouter() failed(%v)", err)
			return
		}
	}

	db.InitDB()
	defer db.CloseDB()

	app.InitRedisStorage()

	// start http listen.
	StartHTTP()
	// init process
	// sleep one second, let the listen start
	time.Sleep(time.Second)
	if err = process.Init(app.Conf.User, app.Conf.Dir, app.Conf.PidFile); err != nil {
		logger.Errorf("process.Init() error(%v)", err)
		return
	}

	defer db.MySQL.Close()

	//初始化配额配置
	app.InitQuotaAll()
	go app.LoadQuotaAll()

	//启动扫描过期文件
	go app.ScanExpireFileLink()

	// init signals, block wait signals
	signalCH := InitSignal()
	HandleSignal(signalCH)

	logger.Info("web stop")
}
Exemplo n.º 6
0
func loadConf(confIP, confPort, confChannel string) {
	bytes, err := ioutil.ReadFile("conf/coditor.json")
	if nil != err {
		logger.Error(err)

		os.Exit(-1)
	}

	conf = &config{}

	err = json.Unmarshal(bytes, conf)
	if err != nil {
		logger.Error("Parses [coditor.json] error: ", err)

		os.Exit(-1)
	}

	log.SetLevel(conf.LogLevel)

	logger = log.NewLogger(os.Stdout)

	logger.Debug("Conf: \n" + string(bytes))

	// Working Driectory
	conf.WD = util.OS.Pwd()
	logger.Debugf("${pwd} [%s]", conf.WD)

	// IP
	ip, err := util.Net.LocalIP()
	if err != nil {
		logger.Error(err)

		os.Exit(-1)
	}

	logger.Debugf("${ip} [%s]", ip)

	conf.IP = strings.Replace(conf.IP, "${ip}", ip, 1)
	if "" != confIP {
		conf.IP = confIP
	}

	if "" != confPort {
		conf.Port = confPort
	}

	// Server
	conf.Server = strings.Replace(conf.Server, "{IP}", conf.IP, 1)

	// Static Server
	conf.StaticServer = strings.Replace(conf.StaticServer, "{IP}", conf.IP, 1)
	conf.StaticServer = strings.Replace(conf.StaticServer, "{Port}", conf.Port, 1)

	conf.StaticResourceVersion = strings.Replace(conf.StaticResourceVersion, "${time}", strconv.FormatInt(time.Now().UnixNano(), 10), 1)

	// Channel
	conf.Channel = strings.Replace(conf.Channel, "{IP}", conf.IP, 1)
	conf.Channel = strings.Replace(conf.Channel, "{Port}", conf.Port, 1)

	if "" != confChannel {
		conf.Channel = confChannel
	}

	conf.Server = strings.Replace(conf.Server, "{Port}", conf.Port, 1)

}