@@ -4,17 +4,16 @@ import (
4
4
"encoding/json"
5
5
"fmt"
6
6
"io"
7
- "io/ioutil"
8
- "log"
9
7
"net/http"
10
8
"net/url"
9
+ "os"
11
10
"strconv"
12
11
"strings"
13
12
)
14
13
15
14
type server struct {
16
15
router router
17
- log printfer
16
+ log logger
18
17
gm gameManager
19
18
pm playerManager
20
19
rootHandler http.Handler
@@ -26,8 +25,10 @@ type router interface {
26
25
HandleFunc (string , string , func (http.ResponseWriter , * http.Request ))
27
26
}
28
27
29
- type printfer interface {
28
+ type logger interface {
30
29
Printf (string , ... interface {})
30
+ Info (any , ... any )
31
+ Error (any , ... any )
31
32
}
32
33
33
34
type gameManager interface {
@@ -42,8 +43,8 @@ type playerManager interface {
42
43
NewPlayer (string ) * Player
43
44
}
44
45
45
- func newServer (l printfer , gm gameManager , pm playerManager ) (* server , error ) {
46
- l .Printf ("Setting up new server" )
46
+ func newServer (l logger , gm gameManager , pm playerManager ) (* server , error ) {
47
+ l .Info ("Setting up new server" )
47
48
s := & server {
48
49
router : & methodRouter {},
49
50
log : l ,
@@ -57,7 +58,7 @@ func newServer(l printfer, gm gameManager, pm playerManager) (*server, error) {
57
58
58
59
func (s * server ) handleRoot () http.HandlerFunc {
59
60
return func (w http.ResponseWriter , r * http.Request ) {
60
- i , err := ioutil .ReadFile ("index.html" )
61
+ i , err := os .ReadFile ("index.html" )
61
62
if err != nil {
62
63
http .NotFound (w , r )
63
64
return
@@ -67,14 +68,15 @@ func (s *server) handleRoot() http.HandlerFunc {
67
68
}
68
69
69
70
func (s * server ) ServeHTTP (w http.ResponseWriter , r * http.Request ) {
70
- s .log .Printf ( "%v %v %v%v" , r .Proto , r .Method , r .Host , r .URL )
71
+ s .log .Info ( "request" , "protocol" , r .Proto , "method" , r .Method , "host" , r .Host , "uri" , r .URL )
71
72
s .router .ServeHTTP (w , r )
72
73
}
73
74
74
75
func (s * server ) getGames () http.HandlerFunc {
75
76
return func (w http.ResponseWriter , r * http.Request ) {
76
77
data , err := json .Marshal (s .gm .List ())
77
78
if err != nil {
79
+ s .log .Error ("Error marshaling games" , "err" , err )
78
80
http .Error (w , "Error marshaling games" , http .StatusInternalServerError )
79
81
return
80
82
}
@@ -87,16 +89,16 @@ func (s *server) postGamesHandler() http.HandlerFunc {
87
89
return func (w http.ResponseWriter , r * http.Request ) {
88
90
player , err := PlayerFromReq (r , s .pm )
89
91
if err != nil {
90
- s .log .Printf ( err .Error ())
92
+ s .log .Error ( "Error reading body" , "err" , err .Error ())
91
93
http .Error (w , "Error occured reading body" , http .StatusBadRequest )
92
94
return
93
95
}
94
96
pathElements := strings .Split (r .URL .Path , "/" )
95
- s .log .Printf ("Last element of path is %v" , pathElements [len (pathElements )- 1 ])
97
+ s .log .Info ("Last element of path is %v" , pathElements [len (pathElements )- 1 ])
96
98
if pathElements [len (pathElements )- 1 ] == "join" {
97
99
gidint , err := strconv .ParseUint (pathElements [len (pathElements )- 2 ], 10 , 64 )
98
100
if err != nil {
99
- s .log .Printf ( err . Error () )
101
+ s .log .Error ("parsing game id" , "err" , err )
100
102
http .Error (w , "Error occured parsing game id" , http .StatusBadRequest )
101
103
}
102
104
s .gm .AddPlayerToGame (player , gameId (gidint ))
@@ -136,51 +138,44 @@ func (s *server) getGamesHandler() http.HandlerFunc {
136
138
137
139
func (s * server ) createGameHandler () http.HandlerFunc {
138
140
return func (w http.ResponseWriter , r * http.Request ) {
139
- var err error
140
- p , err := PlayerFromReq (r , s .pm )
141
- if err != nil {
142
- s .log .Printf ( err . Error () )
143
- http .Error (w , "Unable to create game with this user" , http .StatusBadRequest )
144
- return
145
- }
146
- postData , _ := io .ReadAll (r .Body )
147
- var jd newGameData
148
- err = json .Unmarshal (postData , & jd )
149
- if err != nil {
150
- log .Printf ("ERROR: failed to unmarshal postData: %v" , err . Error () )
151
- http .Error (w , "Unable to create game with provided data" , http .StatusBadRequest )
152
- return
153
- }
154
- s .log .Printf ( "%v creating new game: %v\n " , p , jd )
155
- g := s .gm .CreateGame (jd .Name , p )
156
- if g == nil {
157
- http .Error (w , "Unable to create game" , http .StatusInternalServerError )
158
- return
159
- }
160
- w .Header ().Set ("Content-Type" , "application/json" )
161
- w .WriteHeader (http .StatusCreated )
162
- body , _ := json .Marshal (g )
163
- w .Write (body )
141
+ // var err error
142
+ // p, err := PlayerFromReq(r, s.pm)
143
+ // if err != nil {
144
+ // s.log.Error("PlayerFromReq", "err", err )
145
+ // http.Error(w, "Unable to create game with this user", http.StatusBadRequest)
146
+ // return
147
+ // }
148
+ // postData, _ := io.ReadAll(r.Body)
149
+ // var jd newGameData
150
+ // err = json.Unmarshal(postData, &jd)
151
+ // if err != nil {
152
+ // s. log.Error ("ERROR: failed to unmarshal postData", "err" , err)
153
+ // http.Error(w, "Unable to create game with provided data", http.StatusBadRequest)
154
+ // return
155
+ // }
156
+ // s.log.Info(" creating new game: %v\n", "player", p, "gameData" , jd)
157
+ // g := s.gm.CreateGame(jd.Name, p)
158
+ // if g == nil {
159
+ // http.Error(w, "Unable to create game", http.StatusInternalServerError)
160
+ // return
161
+ // }
162
+ // w.Header().Set("Content-Type", "application/json")
163
+ // w.WriteHeader(http.StatusCreated)
164
+ // body, _ := json.Marshal(g)
165
+ // w.Write(body)
164
166
}
165
167
}
166
168
167
169
func (s * server ) newUser () http.HandlerFunc {
168
170
return func (w http.ResponseWriter , r * http.Request ) {
169
- if r .Method != http .MethodPost {
170
- http .Error (w , "You can only POST to this endpoint" , http .StatusMethodNotAllowed )
171
- return
172
- }
173
- body , _ := io .ReadAll (r .Body )
174
- s .log .Printf ("%v" , string (body ))
175
- pd := struct { Name string }{}
176
- err := json .Unmarshal (body , & pd )
171
+ err := r .ParseForm ()
177
172
if err != nil {
178
- s .log .Printf ("Unable to create player with data: %+v, error: %v" , string (body ), err .Error ())
179
- http .Error (w , "Invalid format" , http .StatusBadRequest )
180
- return
173
+ s .log .Error ("Unable to parse form" , "err" , err )
181
174
}
182
- p := s .pm .NewPlayer (pd .Name )
183
- s .log .Printf ("Creating new player: %v(%d)" , p .Name , p .Id )
175
+ data := r .PostForm
176
+ s .log .Info ("newUser" , "data" , data )
177
+ p := s .pm .NewPlayer (data .Get ("name" ))
178
+ s .log .Info ("Creating new player" , "name" , p .Name , "id" , p .Id )
184
179
rbody , _ := json .Marshal (p )
185
180
w .WriteHeader (http .StatusCreated )
186
181
w .Write (rbody )
@@ -212,11 +207,11 @@ func PlayerFromReq(r *http.Request, pm playerManager) (*Player, error) {
212
207
pidstring := r .Header .Get ("X-Player-Id" )
213
208
pidint , err := strconv .ParseUint (pidstring , 10 , 64 )
214
209
if err != nil {
215
- return nil , err
210
+ return nil , fmt . Errorf ( "unable to parse uint from string ''%s': %w" , pidstring , err )
216
211
}
217
212
player , err := pm .FindPlayer (PlayerId (pidint ))
218
213
if err != nil {
219
- return nil , err
214
+ return nil , fmt . Errorf ( "unable to find player with pid %d: %w" , pidint , err )
220
215
}
221
216
return player , nil
222
217
}
0 commit comments