Skip to content

Commit

Permalink
Merge branch 'pagefaultgames:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
StrayanDropbear authored May 17, 2024
2 parents 3cab1c8 + df92ff8 commit d386424
Show file tree
Hide file tree
Showing 9 changed files with 471 additions and 102 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ jobs:
- name: Install dependencies
run: go mod download
- name: Lint Codebase
continue-on-error: true
uses: golangci/golangci-lint-action@v6
with:
args: --timeout=10m
version: latest
args: --config .golangci.yml
- name: Test
run: go test -v
- name: Build
Expand Down
8 changes: 8 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
run:
timeout: 10m
severity:
default-severity: error
rules:
- linters:
- unused
severity: info
3 changes: 2 additions & 1 deletion api/account/logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package account

import (
"database/sql"
"errors"
"fmt"

"github.com/pagefaultgames/rogueserver/db"
Expand All @@ -28,7 +29,7 @@ import (
func Logout(token []byte) error {
err := db.RemoveSessionFromToken(token)
if err != nil {
if err == sql.ErrNoRows {
if errors.Is(err, sql.ErrNoRows) {
return fmt.Errorf("token not found")
}

Expand Down
41 changes: 29 additions & 12 deletions api/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ package api

import (
"encoding/base64"
"encoding/json"
"fmt"
"log"
"net/http"

"github.com/pagefaultgames/rogueserver/api/account"
"github.com/pagefaultgames/rogueserver/api/daily"
"github.com/pagefaultgames/rogueserver/db"
"log"
"net/http"
)

func Init(mux *http.ServeMux) error {
Expand All @@ -48,14 +48,17 @@ func Init(mux *http.ServeMux) error {
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)
mux.HandleFunc("GET /savedata/newclear", handleNewClear)
mux.HandleFunc("GET /savedata/get", legacyHandleGetSaveData)
mux.HandleFunc("POST /savedata/update", legacyHandleSaveData)
mux.HandleFunc("GET /savedata/delete", legacyHandleSaveData) // TODO use deleteSystemSave
mux.HandleFunc("POST /savedata/clear", legacyHandleSaveData) // TODO use clearSessionData
mux.HandleFunc("GET /savedata/newclear", legacyHandleNewClear)

// new session
mux.HandleFunc("POST /savedata/updateall", handleSaveData2)
mux.HandleFunc("POST /savedata/updateall", handleUpdateAll)
mux.HandleFunc("POST /savedata/system/verify", handleSystemVerify)
mux.HandleFunc("GET /savedata/system", handleGetSystemData)
mux.HandleFunc("GET /savedata/session", handleGetSessionData)

// daily
mux.HandleFunc("GET /daily/seed", handleDailySeed)
Expand All @@ -82,20 +85,34 @@ func tokenFromRequest(r *http.Request) ([]byte, error) {
}

func uuidFromRequest(r *http.Request) ([]byte, error) {
_, uuid, err := tokenAndUuidFromRequest(r)
return uuid, err
}

func tokenAndUuidFromRequest(r *http.Request) ([]byte, []byte, error) {
token, err := tokenFromRequest(r)
if err != nil {
return nil, err
return nil, nil, err
}

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

return uuid, nil
return token, 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)
}

func jsonResponse(w http.ResponseWriter, r *http.Request, data any) {
w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(data)
if err != nil {
httpError(w, r, fmt.Errorf("failed to encode response json: %s", err), http.StatusInternalServerError)
return
}
}
Loading

0 comments on commit d386424

Please sign in to comment.