Esempio n. 1
0
// HomeHandler is the home of our website
func HomeHandler(w http.ResponseWriter, r *http.Request) {

	// Try to refresh player count
	refreshPlayerCount()

	// Get leaderboards
	leaderboards, err := steam.GetLeaderboards()
	if err != nil {
		common.Err("SERVER", fmt.Sprintf("%v", err))
	}

	// Prepare view
	var data IndexData

	// -- Paginate
	page := getPage(r)

	u, d, pagination := paginate.Paginate(len(leaderboards.List), page, LEADERBOARDSHOMECOUNT)

	data.Leaderboards = leaderboards.List[u:d]
	data.Page = pagination
	data.PlayerCount = playerCount

	// -- HTML rendering
	t, err := template.ParseFiles(layoutPath("default"), templatePath("index"))
	if err != nil {
		common.Err("SERVER", fmt.Sprintf("%v", err))
	} else {
		t.Execute(w, data)
	}
}
Esempio n. 2
0
// Start the server.
func Start() {

	common.Info("SERVER", "Loading...")

	steam.DebugRequests = false

	// Check API key
	// Request API Key: http://steamcommunity.com/dev/apikey
	err := godotenv.Load()
	if err != nil {
		common.Err("SERVER", fmt.Sprintf("Error loading .env file: %v ", err))
	} else {
		steamAPIkey := getAPIKey()

		if steamAPIkey == "" {
			common.Warn("SERVER", "Missing Steam API key, some features will not work. Get one at http://steamcommunity.com/dev/apikey and add it to a .env file for key STEAM_API_KEY.")
		} else {
			common.Info("SERVER", "Steam key found!")
		}
	}

	// Configure
	common.ConfigureDB(config.DbFile)
	steam.Configure(getAPIKey(), STEREDENN, []int{1006063}, steredenn.Steredenn{})

	// Get player count
	refreshPlayerCount()

	common.Info("SERVER", "Loading completed!")

	// Initial loading?
	if config.LoadAllOnStart {

		common.Info("SERVER", "Starting complete data reload...")

		leaderboards, _ := steam.GetLeaderboards()

		for _, l := range leaderboards.List {
			steam.GetLeaderboard(l.SteamID)
		}

		common.Info("SERVER", "Data reload completed!")
	}

	// Routing
	r := mux.NewRouter()
	r.HandleFunc("/", HomeHandler)
	r.HandleFunc("/{page:[0-9]+}", HomeHandler)
	r.HandleFunc("/leaderboard/{id:[0-9]+}", LeaderboardHandler)
	r.HandleFunc("/leaderboard/{id:[0-9]+}/{page:[0-9]+}", LeaderboardHandler)
	r.HandleFunc("/player/{id:[0-9]+}", PlayerHandler)

	// Use a custom file server in dev mode to serve static file.
	// Useless in production (nginx will handle that).
	if config.Dev {
		r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir(config.StaticFolder))))
	}

	http.Handle("/", r)
	http.ListenAndServe(":3000", nil)
}