-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_handlers.go
41 lines (30 loc) · 939 Bytes
/
api_handlers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package server
import (
"net/http"
"github.com/dhendry/kitchen-sink/solitaire/db"
"github.com/dhendry/kitchen-sink/solitaire/game"
"github.com/dhendry/kitchen-sink/solitaire/model"
"github.com/golang/protobuf/jsonpb"
"golang.org/x/net/context"
)
func RegisterApiHandlers() {
http.HandleFunc("/api/v1/new", newGame)
}
func newGame(resp http.ResponseWriter, req *http.Request) {
gs := game.NewGameState()
resp.Header().Set("Content-Type", "application/json; charset=utf-8")
err := (&jsonpb.Marshaler{Indent: " "}).Marshal(resp, gs)
if err != nil {
http.Error(resp, err.Error(), http.StatusInternalServerError)
return
}
log.Println("Served new game to", req.RemoteAddr, req.UserAgent())
}
type PlayServiceImpl struct {
}
func (ps *PlayServiceImpl) NewGame(ctx context.Context, ngr *NewGameRequest) (gs *model.GameState, err error){
log.Info(ctx)
gs = game.NewGameState()
db.Db.SaveNewGameState(gs)
return
}