示例#1
0
文件: main.go 项目: gpittarelli/Helen
func main() {
	helpers.InitLogger()

	flag.Parse()
	if *flagGen {
		key := securecookie.GenerateRandomKey(64)
		if len(key) == 0 {
			logrus.Fatal("Couldn't generate random key")
		}

		base64Key := base64.StdEncoding.EncodeToString(key)
		fmt.Println(base64Key)
		return
	}

	config.SetupConstants()
	go rpc.StartRPC()

	if config.Constants.ProfilerEnable {
		address := "localhost:" + config.Constants.ProfilerPort
		go func() {
			graceful.Run(address, 1*time.Second, nil)
		}()
		logrus.Info("Running Profiler at %s", address)
	}

	pid := &pid.Instance{}
	if pid.Create() == nil {
		defer pid.Remove()
	}

	authority.RegisterTypes()
	helpers.InitAuthorization()
	database.Init()
	migrations.Do()
	stores.SetupStores()
	models.InitializeLobbySettings("./lobbySettingsData.json")

	models.ConnectRPC()
	models.DeleteUnusedServerRecords()
	go models.Ping()

	chelpers.InitGeoIPDB()
	if config.Constants.SteamIDWhitelist != "" {
		go chelpers.WhitelistListener()
	}
	// lobby := models.NewLobby("cp_badlands", 10, "a", "a", 1)

	mux := http.NewServeMux()
	routes.SetupHTTP(mux)
	socket.RegisterHandlers()

	if val := os.Getenv("DEPLOYMENT_ENV"); strings.ToLower(val) != "production" {
		// init static FileServer
		// TODO be careful to set this to correct location when deploying
		mux.HandleFunc("/static/", func(w http.ResponseWriter, r *http.Request) {
			http.ServeFile(w, r, r.URL.Path[1:])
		})
	}
	corsHandler := cors.New(cors.Options{
		AllowedOrigins:   config.AllowedCorsOrigins,
		AllowCredentials: true,
	}).Handler(context.ClearHandler(mux))

	// start the server
	logrus.Info("Serving on", config.HTTPAddress())
	graceful.Run(config.Constants.Address, 1*time.Second, corsHandler)
}
示例#2
0
文件: main.go 项目: TF2Stadium/Helen
func main() {
	flag.Parse()

	if *flagGen {
		key := make([]byte, 64)
		_, err := rand.Read(key)
		if err != nil {
			logrus.Fatal(err)
		}

		base64Key := base64.StdEncoding.EncodeToString(key)
		fmt.Println(base64Key)
		return
	}
	if *docPrint {
		config.PrintConfigDoc()
		os.Exit(0)
	}

	if helpers.Raven != nil {
		hook, err := logrus_sentry.NewWithClientSentryHook(helpers.Raven, []logrus.Level{
			logrus.PanicLevel,
			logrus.FatalLevel,
			logrus.ErrorLevel,
			logrus.WarnLevel,
		})

		if err == nil {
			logrus.AddHook(hook)
		} else {
			logrus.Fatal(err)
		}
	}

	logrus.Debug("Commit: ", version.GitCommit)
	logrus.Debug("Branch: ", version.GitBranch)
	logrus.Debug("Build date: ", version.BuildDate)

	controllers.InitTemplates()
	//models.ReadServers()

	err := os.Mkdir(config.Constants.DemosFolder, 0755)
	if err != nil && !os.IsExist(err) {
		logrus.Fatal(err)
	}

	if config.Constants.ProfilerAddr != "" {
		go http.ListenAndServe(config.Constants.ProfilerAddr, nil)
		logrus.Info("Running Profiler at ", config.Constants.ProfilerAddr)
	}

	database.Init()
	database.DB.DB().SetMaxOpenConns(*dbMaxopen)
	migrations.Do()

	helpers.ConnectAMQP()
	event.StartListening()
	helpers.InitGeoIPDB()

	err = lobbySettings.LoadLobbySettingsFromFile("assets/lobbySettingsData.json")
	if err != nil {
		logrus.Fatal(err)
	}

	lobby.CreateLocks()
	rpc.ConnectRPC(helpers.AMQPConn)
	lobby.RestoreServemeChecks()
	//go models.TFTVStreamStatusUpdater()

	if config.Constants.SteamIDWhitelist != "" {
		go chelpers.WhitelistListener()
	}

	mux := http.NewServeMux()
	routes.SetupHTTP(mux)
	socket.RegisterHandlers()

	corsHandler := cors.New(cors.Options{
		AllowedOrigins:   config.Constants.AllowedOrigins,
		AllowedMethods:   []string{"GET", "POST", "DELETE", "OPTIONS"},
		AllowCredentials: true,
	}).Handler(mux)

	sig := make(chan os.Signal, 1)
	signal.Notify(sig, os.Interrupt, os.Kill, syscall.SIGTERM, syscall.SIGKILL)
	go func() {
		<-sig
		shutdown()
		os.Exit(0)
	}()

	logrus.Info("Serving on ", config.Constants.ListenAddress)
	logrus.Fatal(http.ListenAndServe(config.Constants.ListenAddress, corsHandler))
}