From 16a652366e3a4bb92936e9586ef60fa50db830a4 Mon Sep 17 00:00:00 2001 From: Marco Date: Tue, 7 May 2024 23:31:25 +0200 Subject: [PATCH] Introduce handler to get lobby info from passphrase --- api/lobby_info.go | 7 +++++++ api/passphrase.go | 5 +++++ go.mod | 1 - handler/handler.go | 34 ++++++++++++++++++++++++++++++++++ main.go | 1 + utils/passphrase.go | 4 ++++ 6 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 api/lobby_info.go create mode 100644 api/passphrase.go create mode 100644 handler/handler.go diff --git a/api/lobby_info.go b/api/lobby_info.go new file mode 100644 index 0000000..149bf20 --- /dev/null +++ b/api/lobby_info.go @@ -0,0 +1,7 @@ +package api + +import "github.com/google/uuid" + +type LobbyInfo struct { + ID *uuid.UUID `json:"id,omitempty"` +} diff --git a/api/passphrase.go b/api/passphrase.go new file mode 100644 index 0000000..c50b803 --- /dev/null +++ b/api/passphrase.go @@ -0,0 +1,5 @@ +package api + +type Passphrase struct { + Value *string `json:"value,omitempty"` +} diff --git a/go.mod b/go.mod index 6577234..901fad6 100644 --- a/go.mod +++ b/go.mod @@ -22,7 +22,6 @@ require ( github.com/go-playground/universal-translator v0.18.1 // indirect github.com/go-playground/validator/v10 v10.20.0 // indirect github.com/goccy/go-json v0.10.2 // indirect - github.com/google/go-cmp v0.6.0 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/klauspost/cpuid/v2 v2.2.7 // indirect github.com/leodido/go-urn v1.4.0 // indirect diff --git a/handler/handler.go b/handler/handler.go new file mode 100644 index 0000000..f3cf24e --- /dev/null +++ b/handler/handler.go @@ -0,0 +1,34 @@ +package handler + +import ( + "mchess_server/api" + lobbies "mchess_server/lobby_registry" + "mchess_server/utils" + "net/http" + + "github.com/gin-gonic/gin" +) + +func GetLobbyFromPassphraseHandler(c *gin.Context) { + reqPassphrase := api.Passphrase{} + + err := c.ShouldBindJSON(&reqPassphrase) + if err != nil || reqPassphrase.Value == nil || *reqPassphrase.Value == "" { + c.IndentedJSON(http.StatusNotFound, reqPassphrase) + return + } + + lobby := lobbies.GetLobbyRegistry().GetLobbyByPassphrase( + utils.NewPassphraseFromString(*reqPassphrase.Value)) + + if lobby == nil { + c.IndentedJSON(http.StatusNotFound, reqPassphrase) + return + } + + lobbyInfo := api.LobbyInfo{ + ID: &lobby.Uuid, + } + + c.IndentedJSON(http.StatusOK, lobbyInfo) +} diff --git a/main.go b/main.go index a2432ef..71f353e 100644 --- a/main.go +++ b/main.go @@ -40,6 +40,7 @@ func main() { router.POST("/api/joinPrivate", joinPrivateGame) router.GET("/api/ws", registerWebSocketConnection) + debugMode = true if debugMode { log.Println("Starting service WITHOUT TLS") log.Fatal(router.Run(":8080")) diff --git a/utils/passphrase.go b/utils/passphrase.go index d0762d0..1cbf6d9 100644 --- a/utils/passphrase.go +++ b/utils/passphrase.go @@ -20,6 +20,10 @@ func NewPassphrase() Passphrase { return Passphrase(strings.TrimSpace(phrase)) } +func NewPassphraseFromString(s string) Passphrase { + return Passphrase(s) +} + func (p Passphrase) String() string { return string(p) }