You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
rogueserver/api/common.go

83 lines
2.4 KiB
Go

10 months ago
package api
import (
"encoding/base64"
"fmt"
"net/http"
"github.com/pagefaultgames/pokerogue-server/api/account"
"github.com/pagefaultgames/pokerogue-server/api/daily"
"github.com/pagefaultgames/pokerogue-server/db"
10 months ago
)
func Init(mux *http.ServeMux) {
scheduleStatRefresh()
daily.Init()
// account
mux.HandleFunc("GET /api/account/info", handleAccountInfo)
mux.HandleFunc("POST /api/account/register", handleAccountRegister)
mux.HandleFunc("POST /api/account/login", handleAccountLogin)
mux.HandleFunc("GET /api/account/logout", handleAccountLogout)
// game
mux.HandleFunc("GET /api/game/playercount", handleGamePlayerCount)
mux.HandleFunc("GET /api/game/titlestats", handleGameTitleStats)
mux.HandleFunc("GET /api/game/classicsessioncount", handleGameClassicSessionCount)
// savedata
mux.HandleFunc("GET /api/savedata/get", handleSaveData)
mux.HandleFunc("POST /api/savedata/update", handleSaveData)
mux.HandleFunc("GET /api/savedata/delete", handleSaveData)
mux.HandleFunc("POST /api/savedata/clear", handleSaveData)
// daily
mux.HandleFunc("GET /api/daily/seed", handleDailySeed)
mux.HandleFunc("GET /api/daily/rankings", handleDailyRankings)
mux.HandleFunc("GET /api/daily/rankingpagecount", handleDailyRankingPageCount)
}
7 months ago
func getUsernameFromRequest(r *http.Request) (string, error) {
if r.Header.Get("Authorization") == "" {
10 months ago
return "", fmt.Errorf("missing token")
}
7 months ago
token, err := base64.StdEncoding.DecodeString(r.Header.Get("Authorization"))
10 months ago
if err != nil {
return "", fmt.Errorf("failed to decode token: %s", err)
}
if len(token) != account.TokenSize {
return "", fmt.Errorf("invalid token length: got %d, expected %d", len(token), account.TokenSize)
10 months ago
}
username, err := db.FetchUsernameFromToken(token)
10 months ago
if err != nil {
return "", fmt.Errorf("failed to validate token: %s", err)
}
return username, nil
}
func getUUIDFromRequest(r *http.Request) ([]byte, error) {
7 months ago
if r.Header.Get("Authorization") == "" {
10 months ago
return nil, fmt.Errorf("missing token")
}
7 months ago
token, err := base64.StdEncoding.DecodeString(r.Header.Get("Authorization"))
10 months ago
if err != nil {
return nil, fmt.Errorf("failed to decode token: %s", err)
}
if len(token) != account.TokenSize {
return nil, fmt.Errorf("invalid token length: got %d, expected %d", len(token), account.TokenSize)
10 months ago
}
uuid, err := db.FetchUUIDFromToken(token)
10 months ago
if err != nil {
return nil, fmt.Errorf("failed to validate token: %s", err)
}
return uuid, nil
}