1- //go:generate easyjson -all ${GOFILE}
2-
31// This file contains the API methods for the public API
42
53package api
64
75import (
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
2423var ErrLobbyNotExistent = errors .New ("the requested lobby doesn't exist" )
2524
26- //easyjson:skip
2725type 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+
3847type 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
8695func (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
439448func (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