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

90 lines
2.4 KiB
Go

10 months ago
package api
import (
"encoding/base64"
"fmt"
"log"
10 months ago
"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 /account/info", handleAccountInfo)
mux.HandleFunc("POST /account/register", handleAccountRegister)
mux.HandleFunc("POST /account/login", handleAccountLogin)
mux.HandleFunc("POST /account/changepw", handleAccountChangePW)
mux.HandleFunc("GET /account/logout", handleAccountLogout)
// game
mux.HandleFunc("GET /game/playercount", handleGamePlayerCount)
mux.HandleFunc("GET /game/titlestats", handleGameTitleStats)
mux.HandleFunc("GET /game/classicsessioncount", handleGameClassicSessionCount)
// savedata
mux.HandleFunc("GET /savedata/get", handleSaveData)
mux.HandleFunc("POST /savedata/update", handleSaveData)
mux.HandleFunc("GET /savedata/delete", handleSaveData)
mux.HandleFunc("POST /savedata/clear", handleSaveData)
// daily
mux.HandleFunc("GET /daily/seed", handleDailySeed)
mux.HandleFunc("GET /daily/rankings", handleDailyRankings)
mux.HandleFunc("GET /daily/rankingpagecount", handleDailyRankingPageCount)
}
func tokenFromRequest(r *http.Request) ([]byte, error) {
7 months ago
if r.Header.Get("Authorization") == "" {
return nil, fmt.Errorf("missing token")
10 months ago
}
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)
10 months ago
}
if len(token) != account.TokenSize {
return nil, fmt.Errorf("invalid token length: got %d, expected %d", len(token), account.TokenSize)
}
return token, nil
}
func usernameFromRequest(r *http.Request) (string, error) {
token, err := tokenFromRequest(r)
if err != nil {
return "", err
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 uuidFromRequest(r *http.Request) ([]byte, error) {
token, err := tokenFromRequest(r)
10 months ago
if err != nil {
return nil, err
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
}
func httpError(w http.ResponseWriter, r *http.Request, err error, code int) {
log.Printf("%s: %s\n", r.URL.Path, err)
http.Error(w, err.Error(), code)
}