Skip to content

Commit

Permalink
Use fasthttp instead of net/http
Browse files Browse the repository at this point in the history
  • Loading branch information
patapancakes committed Apr 19, 2024
1 parent 6bd7deb commit d701ce4
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 137 deletions.
45 changes: 11 additions & 34 deletions api/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,77 +3,54 @@ 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"
)

func Init(mux *http.ServeMux) {
func Init() {
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)
}

func getUsernameFromRequest(r *http.Request) (string, error) {
if r.Header.Get("Authorization") == "" {
func usernameFromTokenHeader(token string) (string, error) {
if token == "" {
return "", fmt.Errorf("missing token")
}

token, err := base64.StdEncoding.DecodeString(r.Header.Get("Authorization"))
decoded, err := base64.StdEncoding.DecodeString(token)
if err != nil {
return "", fmt.Errorf("failed to decode token: %s", err)
}

if len(token) != account.TokenSize {
if len(decoded) != account.TokenSize {
return "", fmt.Errorf("invalid token length: got %d, expected %d", len(token), account.TokenSize)
}

username, err := db.FetchUsernameFromToken(token)
username, err := db.FetchUsernameFromToken(decoded)
if err != nil {
return "", fmt.Errorf("failed to validate token: %s", err)
}

return username, nil
}

func getUUIDFromRequest(r *http.Request) ([]byte, error) {
if r.Header.Get("Authorization") == "" {
func uuidFromTokenHeader(token string) ([]byte, error) {
if token == "" {
return nil, fmt.Errorf("missing token")
}

token, err := base64.StdEncoding.DecodeString(r.Header.Get("Authorization"))
decoded, err := base64.StdEncoding.DecodeString(token)
if err != nil {
return nil, fmt.Errorf("failed to decode token: %s", err)
}

if len(token) != account.TokenSize {
if len(decoded) != account.TokenSize {
return nil, fmt.Errorf("invalid token length: got %d, expected %d", len(token), account.TokenSize)
}

uuid, err := db.FetchUUIDFromToken(token)
uuid, err := db.FetchUUIDFromToken(decoded)
if err != nil {
return nil, fmt.Errorf("failed to validate token: %s", err)
}
Expand Down
Loading

0 comments on commit d701ce4

Please sign in to comment.