~xenrox/faceit_checker

915fd0cbc9a48ee9dfdee69d0c4fcd21bbafefee — Thorben Günther 3 months ago 61e2759
Move API code to own file
2 files changed, 61 insertions(+), 52 deletions(-)

A api.go
M faceit.go
A api.go => api.go +60 -0
@@ 0,0 1,60 @@
package main

import (
	"fmt"
	"io"
	"log/slog"
	"net/http"
	"strings"
)

func (fc *faceitChecker) getElo(w http.ResponseWriter, r *http.Request) {
	logger := fc.logger.With(slog.String("handler", "/api/elo"))

	w.Header().Set("Content-Type", "application/json")

	if r.Method != http.MethodGet {
		http.Error(w, "Only GET allowed", http.StatusMethodNotAllowed)
		return
	}

	steamID64 := strings.TrimPrefix(r.URL.Path, "/api/elo/")

	data, err := fc.getFaceitPlayer(r.Context(), steamID64)
	if err != nil {
		// Validate steamid; there are two possibilies: User entered correct steamID
		// but has no Faceit account -> give him default elo or user entered wrong steamID
		url := "https://steamcommunity.com/profiles/" + steamID64
		steamProfile, errSteam := fc.getSteamProfile(r.Context(), url)
		if errSteam == nil && steamProfile.ID64 == steamID64 {
			if err == ErrNoCSGOFaceit {
				// Set default elo
				data.Games.CSGO.Elo = 1000
			} else {
				logger.Error("Internal error",
					slog.String("error", err.Error()))
				w.WriteHeader(http.StatusInternalServerError)
				_, err = io.WriteString(w, `{"Msg": Internal Server Error}`)
				if err != nil {
					logger.Error("Internal error",
						slog.String("error", err.Error()))
				}
				return
			}
		} else {
			w.WriteHeader(404)
			_, err := io.WriteString(w, `{"Msg": Faulty steamID64}`)
			if err != nil {
				logger.Error("Internal error",
					slog.String("error", err.Error()))
			}
			return
		}
	}

	_, err = io.WriteString(w, fmt.Sprintf(`{"Elo": %d}`, data.Games.CSGO.Elo))
	if err != nil {
		logger.Error("Internal error",
			slog.String("error", err.Error()))
	}
}

M faceit.go => faceit.go +1 -52
@@ 1,4 1,4 @@
// Check FACEIT CS:GO stats for a Steam profile
// Check FACEIT Counter-Strike stats for a Steam profile
package main

import (


@@ 513,54 513,3 @@ func (fc *faceitChecker) requestHandler(w http.ResponseWriter, r *http.Request) 
		http.Error(w, internalError, http.StatusInternalServerError)
	}
}

func (fc *faceitChecker) getElo(w http.ResponseWriter, r *http.Request) {
	logger := fc.logger.With(slog.String("handler", "/api/elo"))

	w.Header().Set("Content-Type", "application/json")

	if r.Method != http.MethodGet {
		http.Error(w, "Only GET allowed", http.StatusMethodNotAllowed)
		return
	}

	steamID64 := strings.TrimPrefix(r.URL.Path, "/api/elo/")

	data, err := fc.getFaceitPlayer(r.Context(), steamID64)
	if err != nil {
		// Validate steamid; there are two possibilies: User entered correct steamID
		// but has no Faceit account -> give him default elo or user entered wrong steamID
		url := "https://steamcommunity.com/profiles/" + steamID64
		steamProfile, errSteam := fc.getSteamProfile(r.Context(), url)
		if errSteam == nil && steamProfile.ID64 == steamID64 {
			if err == ErrNoCSGOFaceit {
				// Set default elo
				data.Games.CSGO.Elo = 1000
			} else {
				logger.Error("Internal error",
					slog.String("error", err.Error()))
				w.WriteHeader(http.StatusInternalServerError)
				_, err = io.WriteString(w, `{"Msg": Internal Server Error}`)
				if err != nil {
					logger.Error("Internal error",
						slog.String("error", err.Error()))
				}
				return
			}
		} else {
			w.WriteHeader(404)
			_, err := io.WriteString(w, `{"Msg": Faulty steamID64}`)
			if err != nil {
				logger.Error("Internal error",
					slog.String("error", err.Error()))
			}
			return
		}
	}

	_, err = io.WriteString(w, fmt.Sprintf(`{"Elo": %d}`, data.Games.CSGO.Elo))
	if err != nil {
		logger.Error("Internal error",
			slog.String("error", err.Error()))
	}
}