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 (
6
+ json "encoding/json"
8
7
"errors"
9
8
"fmt"
10
9
"log"
11
10
"net/http"
11
+ "strconv"
12
12
"strings"
13
13
14
14
"github.com/gofrs/uuid/v5"
15
- "github.com/mailru/easyjson"
16
15
"github.com/scribble-rs/scribble.rs/internal/config"
17
16
"github.com/scribble-rs/scribble.rs/internal/game"
18
17
"github.com/scribble-rs/scribble.rs/internal/state"
@@ -22,7 +21,6 @@ import (
22
21
23
22
var ErrLobbyNotExistent = errors .New ("the requested lobby doesn't exist" )
24
23
25
- //easyjson:skip
26
24
type V1Handler struct {
27
25
cfg * config.Config
28
26
}
@@ -33,7 +31,18 @@ func NewHandler(cfg *config.Config) *V1Handler {
33
31
}
34
32
}
35
33
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
+
37
46
type LobbyEntries []* LobbyEntry
38
47
39
48
// 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
}
@@ -180,7 +189,7 @@ func (handler *V1Handler) postLobby(writer http.ResponseWriter, request *http.Re
180
189
181
190
lobbyData := CreateLobbyData (handler .cfg , lobby )
182
191
183
- if started , _ , err := easyjson . MarshalToHTTPResponseWriter (lobbyData , writer ); err != nil {
192
+ if started , err := marshalToHTTPWriter (lobbyData , writer ); err != nil {
184
193
if ! started {
185
194
http .Error (writer , err .Error (), http .StatusInternalServerError )
186
195
}
@@ -229,7 +238,7 @@ func (handler *V1Handler) postPlayer(writer http.ResponseWriter, request *http.R
229
238
})
230
239
231
240
if lobbyData != nil {
232
- if started , _ , err := easyjson . MarshalToHTTPResponseWriter (lobbyData , writer ); err != nil {
241
+ if started , err := marshalToHTTPWriter (lobbyData , writer ); err != nil {
233
242
if ! started {
234
243
http .Error (writer , err .Error (), http .StatusInternalServerError )
235
244
}
@@ -415,7 +424,7 @@ func (handler *V1Handler) patchLobby(writer http.ResponseWriter, request *http.R
415
424
}
416
425
417
426
func (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 {
419
428
if ! started {
420
429
http .Error (writer , err .Error (), http .StatusInternalServerError )
421
430
}
0 commit comments