-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmain.go
207 lines (167 loc) · 5.98 KB
/
main.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package main exposes the REST endpoints for the profile-service.
package main
import (
"context"
"fmt"
"log"
"net/http"
spanner "cloud.google.com/go/spanner"
"github.com/googleforgames/global-multiplayer-demo/profile-service/config"
"github.com/googleforgames/global-multiplayer-demo/profile-service/models"
"github.com/gin-gonic/gin"
)
// setSpannerConnection is a mutator to create spanner context and client, and set them in gin
func setSpannerConnection(c config.Config) gin.HandlerFunc {
ctx := context.Background()
client, err := spanner.NewClient(ctx, c.Spanner.DB())
if err != nil {
log.Fatal(err)
}
return func(c *gin.Context) {
c.Set("spanner_client", *client)
c.Set("spanner_context", ctx)
c.Next()
}
}
// getSpannerConnection is a helper function to retrieve spanner client and context
func getSpannerConnection(c *gin.Context) (context.Context, spanner.Client) {
return c.MustGet("spanner_context").(context.Context),
c.MustGet("spanner_client").(spanner.Client)
}
// getPlayerID responds to the GET /players/:id endpoint
// Returns a player's information when provided a valid player_google_id
func getPlayerByID(c *gin.Context) {
var playerGoogleId = c.Param("id")
ctx, client := getSpannerConnection(c)
player, err := models.GetPlayerByGoogleId(ctx, client, playerGoogleId)
if err != nil {
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "player not found"})
return
}
c.IndentedJSON(http.StatusOK, player)
}
// createPlayer responds to the POST /players endpoint
// When provided the required fields of player_name, profile_image and region, creates a player.
func createPlayer(c *gin.Context) {
var player models.Player
if err := c.BindJSON(&player); err != nil {
if err := c.AbortWithError(http.StatusBadRequest, err); err != nil {
fmt.Printf("could not abort: %s", err)
}
return
}
ctx, client := getSpannerConnection(c)
err := player.AddPlayer(ctx, client)
if err != nil {
if err := c.AbortWithError(http.StatusBadRequest, err); err != nil {
fmt.Printf("could not abort: %s", err)
}
return
}
c.IndentedJSON(http.StatusCreated, player.Player_google_id)
}
// updatePlayer responds to the PUT /players endpoint
// Updates the player's profile information. Does not include updating stats, which is handled
// by a different endpoint.
func updatePlayer(c *gin.Context) {
var player models.Player
if err := c.BindJSON(&player); err != nil {
if err := c.AbortWithError(http.StatusBadRequest, err); err != nil {
fmt.Printf("could not abort: %s", err)
}
return
}
ctx, client := getSpannerConnection(c)
err := player.UpdatePlayer(ctx, client)
if err != nil {
if err := c.AbortWithError(http.StatusBadRequest, err); err != nil {
fmt.Printf("could not abort: %s", err)
}
return
}
c.IndentedJSON(http.StatusOK, player)
}
// ReturnPlayerStats provides player's identifier and their stats
type ReturnPlayerStats struct {
Player_google_id string `json:"player_google_id"`
Stats spanner.NullJSON `json:"stats"`
Skill_level int64 `json:"skill_level"`
Tier string `json:"tier"`
}
// getPlayerStats responds to the GET /players/:id/stats endpoint
// Returns a player's stats when provided a valid player_google_id
func getPlayerStats(c *gin.Context) {
var playerGoogleId = c.Param("id")
ctx, client := getSpannerConnection(c)
player, err := models.GetPlayerStats(ctx, client, playerGoogleId)
if err != nil {
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "player not found"})
return
}
rStats := ReturnPlayerStats{Player_google_id: player.Player_google_id, Stats: player.Stats, Skill_level: player.Skill_level,
Tier: player.Tier}
c.IndentedJSON(http.StatusOK, rStats)
}
// updatePlayerStats responds to the PUT /player/stats endpoint
// Updates the player's stats based on provided payload
func updatePlayerStats(c *gin.Context) {
game_stats := models.SingleGameStats{}
err := c.BindUri(&game_stats)
// Error binding the google_id from the URI
if err != nil {
if err := c.AbortWithError(http.StatusBadRequest, err); err != nil {
fmt.Printf("could not abort: %s", err)
}
return
}
if err := c.BindJSON(&game_stats); err != nil {
if err := c.AbortWithError(http.StatusBadRequest, err); err != nil {
fmt.Printf("could not abort: %s", err)
}
return
}
ctx, client := getSpannerConnection(c)
player, err := models.UpdateStats(ctx, client, game_stats)
if err != nil {
if err := c.AbortWithError(http.StatusBadRequest, err); err != nil {
fmt.Printf("could not abort: %s", err)
}
return
}
rStats := ReturnPlayerStats{Player_google_id: player.Player_google_id, Stats: player.Stats, Skill_level: player.Skill_level,
Tier: player.Tier}
c.IndentedJSON(http.StatusOK, rStats)
}
// main initializes the gin router and configures the endpoints
func main() {
configuration, _ := config.NewConfig()
router := gin.Default()
// TODO: Better configuration of trusted proxy
if err := router.SetTrustedProxies(nil); err != nil {
fmt.Printf("could not set trusted proxies: %s", err)
return
}
router.Use(setSpannerConnection(configuration))
router.POST("/players", createPlayer)
router.GET("/players/:id", getPlayerByID)
router.PUT("/players", updatePlayer)
router.GET("/players/:id/stats", getPlayerStats)
router.PUT("/players/:id/stats", updatePlayerStats)
if err := router.Run(configuration.Server.URL()); err != nil {
fmt.Printf("could not run gin router: %s", err)
return
}
}