コード例 #1
0
// LeaderboardHandler displays information for a given leaderboard
func LeaderboardHandler(w http.ResponseWriter, r *http.Request) {

	// Get leaderboard object
	vars := mux.Vars(r)
	ldbID, _ := strconv.Atoi(vars["id"])

	l, err := steam.GetLeaderboard(ldbID)

	if err != nil {
		http.Redirect(w, r, "/", http.StatusNotFound)
		return
	}

	// Pagination
	page := getPage(r)
	u, d, pagination := paginate.Paginate(len(l.Entries), page, 100)

	data := LeaderboardData{
		Entries: l.Entries[u:d],
		Page:    pagination,
		Name:    l.Name,
		Count:   len(l.Entries),
		ID:      l.UID,
		Daily:   l.IsDaily(),
		LastUpdateDisplayable: l.LastUpdate.Format(time.Stamp),
	}

	// HTML rendering
	t, err := template.ParseFiles(layoutPath("default"), templatePath("leaderboard"))
	if err != nil {
		common.Err("SERVER", fmt.Sprintf("%v", err))
	} else {
		t.Execute(w, data)
	}
}
コード例 #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)
}