1
- //go:generate easyjson -all ${GOFILE}
2
-
3
1
// This file contains the API methods for the public API
4
2
5
3
package api
6
4
7
5
import (
8
6
"encoding/base64"
7
+ "encoding/json"
9
8
"errors"
10
9
"fmt"
11
10
"log"
12
11
"net/http"
12
+ "strconv"
13
13
"strings"
14
14
15
15
"github.com/gofrs/uuid/v5"
16
- "github.com/mailru/easyjson"
17
16
"github.com/scribble-rs/scribble.rs/internal/config"
18
17
"github.com/scribble-rs/scribble.rs/internal/game"
19
18
"github.com/scribble-rs/scribble.rs/internal/state"
@@ -23,7 +22,6 @@ import (
23
22
24
23
var ErrLobbyNotExistent = errors .New ("the requested lobby doesn't exist" )
25
24
26
- //easyjson:skip
27
25
type V1Handler struct {
28
26
cfg * config.Config
29
27
}
@@ -34,7 +32,18 @@ func NewHandler(cfg *config.Config) *V1Handler {
34
32
}
35
33
}
36
34
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
+
38
47
type LobbyEntries []* LobbyEntry
39
48
40
49
// 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
75
84
})
76
85
}
77
86
78
- if started , _ , err := easyjson . MarshalToHTTPResponseWriter (lobbyEntries , writer ); err != nil {
87
+ if started , err := marshalToHTTPWriter (lobbyEntries , writer ); err != nil {
79
88
if ! started {
80
89
http .Error (writer , err .Error (), http .StatusInternalServerError )
81
90
}
@@ -86,7 +95,7 @@ func (handler *V1Handler) getLobbies(writer http.ResponseWriter, _ *http.Request
86
95
func (handler * V1Handler ) resurrectLobby (writer http.ResponseWriter , request * http.Request ) {
87
96
var data game.LobbyRestoreData
88
97
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 {
90
99
log .Println ("Error unmarshalling lobby resurrection data:" , err )
91
100
http .Error (writer , http .StatusText (http .StatusInternalServerError ), http .StatusInternalServerError )
92
101
return
@@ -202,7 +211,7 @@ func (handler *V1Handler) postLobby(writer http.ResponseWriter, request *http.Re
202
211
203
212
lobbyData := CreateLobbyData (handler .cfg , lobby )
204
213
205
- if started , _ , err := easyjson . MarshalToHTTPResponseWriter (lobbyData , writer ); err != nil {
214
+ if started , err := marshalToHTTPWriter (lobbyData , writer ); err != nil {
206
215
if ! started {
207
216
http .Error (writer , err .Error (), http .StatusInternalServerError )
208
217
}
@@ -251,7 +260,7 @@ func (handler *V1Handler) postPlayer(writer http.ResponseWriter, request *http.R
251
260
})
252
261
253
262
if lobbyData != nil {
254
- if started , _ , err := easyjson . MarshalToHTTPResponseWriter (lobbyData , writer ); err != nil {
263
+ if started , err := marshalToHTTPWriter (lobbyData , writer ); err != nil {
255
264
if ! started {
256
265
http .Error (writer , err .Error (), http .StatusInternalServerError )
257
266
}
@@ -437,7 +446,7 @@ func (handler *V1Handler) patchLobby(writer http.ResponseWriter, request *http.R
437
446
}
438
447
439
448
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 {
441
450
if ! started {
442
451
http .Error (writer , err .Error (), http .StatusInternalServerError )
443
452
}
0 commit comments