コード例 #1
0
ファイル: main.go プロジェクト: vebin/reborn
func main() {
	fmt.Print(banner)
	log.SetLevelByString("info")

	args, err := docopt.Parse(usage, nil, true, "reborn proxy v0.1", true)
	if err != nil {
		log.Error(err)
	}

	// set config file
	setStringFromOpt(&configFile, args, "-c")

	// set output log file
	if v := args["-L"]; v != nil {
		log.SetHighlighting(false)
		log.SetOutputByName(v.(string))
	}

	// set log level
	if v := args["--log-level"]; v != nil {
		log.SetLevelByString(v.(string))
	}

	// set cpu
	setIntArgFromOpt(&cpus, args, "--cpu")

	// set addr
	setStringFromOpt(&addr, args, "--addr")

	// set proto
	setStringFromOpt(&proto, args, "--proto")

	// set http addr
	setStringFromOpt(&httpAddr, args, "--http-addr")

	// set proxy id
	setStringFromOpt(&proxyID, args, "--id")
	if len(proxyID) == 0 {
		log.Fatalf("invalid empty proxy id")
	}

	// set log dump path
	dumppath := utils.GetExecutorPath()
	setStringFromOpt(&dumppath, args, "--dump-path")

	log.Info("dump file path:", dumppath)
	log.CrashLog(path.Join(dumppath, "reborn-proxy.dump"))

	// set pidfile
	setStringFromOpt(&pidfile, args, "--pidfile")

	// set proxy auth
	setStringFromOpt(&proxyAuth, args, "--proxy-auth")

	// set net time
	setIntArgFromOpt(&netTimeout, args, "--net-timeout")

	router.CheckUlimit(1024)
	runtime.GOMAXPROCS(cpus)

	http.HandleFunc("/setloglevel", handleSetLogLevel)
	go http.ListenAndServe(httpAddr, nil)

	conf, err := router.LoadConf(configFile)
	if err != nil {
		log.Fatal(err)
	}

	conf.Addr = addr
	conf.HTTPAddr = httpAddr
	conf.ProxyID = proxyID
	conf.PidFile = pidfile
	conf.NetTimeout = netTimeout
	conf.Proto = proto
	conf.ProxyAuth = proxyAuth

	if err := utils.CreatePidFile(conf.PidFile); err != nil {
		log.Fatal(err)
	}

	log.Info("running on ", addr)

	s := router.NewServer(conf)
	s.Run()
	log.Warning("exit")
}
コード例 #2
0
func main() {
	log.SetLevelByString("info")

	args, err := docopt.Parse(usage, nil, true, "reborn config v0.1", true)
	if err != nil {
		log.Error(err)
	}

	if v := args["--pidfile"]; v != nil {
		pidFile = v.(string)
	}

	// set config file
	var configFile string
	var config *cfg.Cfg
	if args["-c"] != nil {
		configFile = args["-c"].(string)
		config, err = utils.InitConfigFromFile(configFile)
		if err != nil {
			Fatal(err)
		}
	} else {
		config, err = utils.InitConfig()
		if err != nil {
			Fatal(err)
		}
	}

	// load global vars
	globalEnv = env.LoadRebornEnv(config)
	globalConn = CreateCoordConn()

	// set output log file
	if args["-L"] != nil {
		log.SetHighlighting(false)
		log.SetOutputByName(args["-L"].(string))
	}

	// set log level
	if args["--log-level"] != nil {
		log.SetLevelByString(args["--log-level"].(string))
	}

	cmd := args["<command>"].(string)
	cmdArgs := args["<args>"].([]string)

	httpAddr := ":10086"
	if v := args["--http-addr"]; v != nil {
		httpAddr = v.(string)
	}

	go http.ListenAndServe(httpAddr, nil)

	c := make(chan os.Signal, 1)
	signal.Notify(c, syscall.SIGTERM, os.Interrupt, os.Kill)
	go func() {
		<-c

		Fatal("ctrl-c or SIGTERM found, exit")
	}()

	utils.CreatePidFile(pidFile)

	err = runCommand(cmd, cmdArgs)
	if err != nil {
		log.Fatal(errors.ErrorStack(err))
	}
}