1- //go:generate easyjson -all ${GOFILE}
2-
31// This file contains the API methods for the public API
42
53package api
64
75import (
6+ json "encoding/json"
87 "errors"
98 "fmt"
109 "log"
1110 "net/http"
11+ "strconv"
1212 "strings"
1313
1414 "github.com/gofrs/uuid/v5"
15- "github.com/mailru/easyjson"
1615 "github.com/scribble-rs/scribble.rs/internal/config"
1716 "github.com/scribble-rs/scribble.rs/internal/game"
1817 "github.com/scribble-rs/scribble.rs/internal/state"
@@ -22,7 +21,6 @@ import (
2221
2322var ErrLobbyNotExistent = errors .New ("the requested lobby doesn't exist" )
2423
25- //easyjson:skip
2624type V1Handler struct {
2725 cfg * config.Config
2826}
@@ -33,7 +31,18 @@ func NewHandler(cfg *config.Config) *V1Handler {
3331 }
3432}
3533
36- //easyjson:json
34+ func marshalToHTTPWriter (data any , writer http.ResponseWriter ) (bool , error ) {
35+ bytes , err := json .Marshal (data )
36+ if err != nil {
37+ return false , err
38+ }
39+
40+ writer .Header ().Set ("Content-Type" , "application/json" )
41+ writer .Header ().Set ("Content-Length" , strconv .Itoa (len (bytes )))
42+ _ , err = writer .Write (bytes )
43+ return true , err
44+ }
45+
3746type LobbyEntries []* LobbyEntry
3847
3948// 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 }
@@ -180,7 +189,7 @@ func (handler *V1Handler) postLobby(writer http.ResponseWriter, request *http.Re
180189
181190 lobbyData := CreateLobbyData (handler .cfg , lobby )
182191
183- if started , _ , err := easyjson . MarshalToHTTPResponseWriter (lobbyData , writer ); err != nil {
192+ if started , err := marshalToHTTPWriter (lobbyData , writer ); err != nil {
184193 if ! started {
185194 http .Error (writer , err .Error (), http .StatusInternalServerError )
186195 }
@@ -229,7 +238,7 @@ func (handler *V1Handler) postPlayer(writer http.ResponseWriter, request *http.R
229238 })
230239
231240 if lobbyData != nil {
232- if started , _ , err := easyjson . MarshalToHTTPResponseWriter (lobbyData , writer ); err != nil {
241+ if started , err := marshalToHTTPWriter (lobbyData , writer ); err != nil {
233242 if ! started {
234243 http .Error (writer , err .Error (), http .StatusInternalServerError )
235244 }
@@ -415,7 +424,7 @@ func (handler *V1Handler) patchLobby(writer http.ResponseWriter, request *http.R
415424}
416425
417426func (handler * V1Handler ) getStats (writer http.ResponseWriter , _ * http.Request ) {
418- if started , _ , err := easyjson . MarshalToHTTPResponseWriter (state .Stats (), writer ); err != nil {
427+ if started , err := marshalToHTTPWriter (state .Stats (), writer ); err != nil {
419428 if ! started {
420429 http .Error (writer , err .Error (), http .StatusInternalServerError )
421430 }
0 commit comments