Skip to content

Commit 36fe9f6

Browse files
committed
Multiple changes
* Throw out easyjson, the generator bugs annoyed me * move usersessions into the lobby for resurrection
1 parent ab82b80 commit 36fe9f6

13 files changed

+63
-3763
lines changed

go.mod

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ require (
99
github.com/go-chi/cors v1.2.1
1010
github.com/gofrs/uuid/v5 v5.3.0
1111
github.com/lxzan/gws v1.8.8
12-
github.com/mailru/easyjson v0.9.0
1312
github.com/prometheus/client_golang v1.20.5
1413
github.com/stretchr/testify v1.10.0
1514
github.com/subosito/gotenv v1.6.0
@@ -21,7 +20,6 @@ require (
2120
github.com/cespare/xxhash/v2 v2.3.0 // indirect
2221
github.com/davecgh/go-spew v1.1.1 // indirect
2322
github.com/dolthub/maphash v0.1.0 // indirect
24-
github.com/josharian/intern v1.0.0 // indirect
2523
github.com/klauspost/compress v1.17.9 // indirect
2624
github.com/kr/text v0.2.0 // indirect
2725
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect

go.sum

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ github.com/gofrs/uuid/v5 v5.3.0 h1:m0mUMr+oVYUdxpMLgSYCZiXe7PuVPnI94+OMeVBNedk=
1919
github.com/gofrs/uuid/v5 v5.3.0/go.mod h1:CDOjlDMVAtN56jqyRUZh58JT31Tiw7/oQyEXZV+9bD8=
2020
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
2121
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
22-
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
23-
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
2422
github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=
2523
github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
2624
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
@@ -31,8 +29,6 @@ github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0
3129
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
3230
github.com/lxzan/gws v1.8.8 h1:st193ZG8qN8sSw8/g/UituFhs7etmKzS7jUqhijg5wM=
3331
github.com/lxzan/gws v1.8.8/go.mod h1:FcGeRMB7HwGuTvMLR24ku0Zx0p6RXqeKASeMc4VYgi4=
34-
github.com/mailru/easyjson v0.9.0 h1:PrnmzHw7262yW8sTBwxi1PdJA3Iw/EKBa8psRf7d9a4=
35-
github.com/mailru/easyjson v0.9.0/go.mod h1:1+xMtQp2MRNVL/V1bOzuP3aP8VNwRW55fQUto+XFtTU=
3632
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
3733
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
3834
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=

internal/api/v1.go

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
1-
//go:generate easyjson -all ${GOFILE}
2-
31
// This file contains the API methods for the public API
42

53
package api
64

75
import (
86
"encoding/base64"
7+
"encoding/json"
98
"errors"
109
"fmt"
1110
"log"
1211
"net/http"
12+
"strconv"
1313
"strings"
1414

1515
"github.com/gofrs/uuid/v5"
16-
"github.com/mailru/easyjson"
1716
"github.com/scribble-rs/scribble.rs/internal/config"
1817
"github.com/scribble-rs/scribble.rs/internal/game"
1918
"github.com/scribble-rs/scribble.rs/internal/state"
@@ -23,7 +22,6 @@ import (
2322

2423
var ErrLobbyNotExistent = errors.New("the requested lobby doesn't exist")
2524

26-
//easyjson:skip
2725
type V1Handler struct {
2826
cfg *config.Config
2927
}
@@ -34,7 +32,18 @@ func NewHandler(cfg *config.Config) *V1Handler {
3432
}
3533
}
3634

37-
//easyjson:json
35+
func marshalToHTTPWriter(data any, writer http.ResponseWriter) (bool, error) {
36+
bytes, err := json.Marshal(data)
37+
if err != nil {
38+
return false, err
39+
}
40+
41+
writer.Header().Set("Content-Type", "application/json")
42+
writer.Header().Set("Content-Length", strconv.Itoa(len(bytes)))
43+
_, err = writer.Write(bytes)
44+
return true, err
45+
}
46+
3847
type LobbyEntries []*LobbyEntry
3948

4049
// LobbyEntry is an API object for representing a join-able public lobby.
@@ -75,7 +84,7 @@ func (handler *V1Handler) getLobbies(writer http.ResponseWriter, _ *http.Request
7584
})
7685
}
7786

78-
if started, _, err := easyjson.MarshalToHTTPResponseWriter(lobbyEntries, writer); err != nil {
87+
if started, err := marshalToHTTPWriter(lobbyEntries, writer); err != nil {
7988
if !started {
8089
http.Error(writer, err.Error(), http.StatusInternalServerError)
8190
}
@@ -86,7 +95,7 @@ func (handler *V1Handler) getLobbies(writer http.ResponseWriter, _ *http.Request
8695
func (handler *V1Handler) resurrectLobby(writer http.ResponseWriter, request *http.Request) {
8796
var data game.LobbyRestoreData
8897
base64Decoder := base64.NewDecoder(base64.StdEncoding, request.Body)
89-
if err := easyjson.UnmarshalFromReader(base64Decoder, &data); err != nil {
98+
if err := json.NewDecoder(base64Decoder).Decode(&data); err != nil {
9099
log.Println("Error unmarshalling lobby resurrection data:", err)
91100
http.Error(writer, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
92101
return
@@ -202,7 +211,7 @@ func (handler *V1Handler) postLobby(writer http.ResponseWriter, request *http.Re
202211

203212
lobbyData := CreateLobbyData(handler.cfg, lobby)
204213

205-
if started, _, err := easyjson.MarshalToHTTPResponseWriter(lobbyData, writer); err != nil {
214+
if started, err := marshalToHTTPWriter(lobbyData, writer); err != nil {
206215
if !started {
207216
http.Error(writer, err.Error(), http.StatusInternalServerError)
208217
}
@@ -251,7 +260,7 @@ func (handler *V1Handler) postPlayer(writer http.ResponseWriter, request *http.R
251260
})
252261

253262
if lobbyData != nil {
254-
if started, _, err := easyjson.MarshalToHTTPResponseWriter(lobbyData, writer); err != nil {
263+
if started, err := marshalToHTTPWriter(lobbyData, writer); err != nil {
255264
if !started {
256265
http.Error(writer, err.Error(), http.StatusInternalServerError)
257266
}
@@ -437,7 +446,7 @@ func (handler *V1Handler) patchLobby(writer http.ResponseWriter, request *http.R
437446
}
438447

439448
func (handler *V1Handler) getStats(writer http.ResponseWriter, _ *http.Request) {
440-
if started, _, err := easyjson.MarshalToHTTPResponseWriter(state.Stats(), writer); err != nil {
449+
if started, err := marshalToHTTPWriter(state.Stats(), writer); err != nil {
441450
if !started {
442451
http.Error(writer, err.Error(), http.StatusInternalServerError)
443452
}

0 commit comments

Comments
 (0)