Introduce handler to get lobby info from passphrase

This commit is contained in:
Marco 2024-05-07 23:31:25 +02:00
parent 2514e9fffd
commit 16a652366e
6 changed files with 51 additions and 1 deletions

7
api/lobby_info.go Normal file
View File

@ -0,0 +1,7 @@
package api
import "github.com/google/uuid"
type LobbyInfo struct {
ID *uuid.UUID `json:"id,omitempty"`
}

5
api/passphrase.go Normal file
View File

@ -0,0 +1,5 @@
package api
type Passphrase struct {
Value *string `json:"value,omitempty"`
}

1
go.mod
View File

@ -22,7 +22,6 @@ require (
github.com/go-playground/universal-translator v0.18.1 // indirect github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.20.0 // indirect github.com/go-playground/validator/v10 v10.20.0 // indirect
github.com/goccy/go-json v0.10.2 // 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/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/leodido/go-urn v1.4.0 // indirect github.com/leodido/go-urn v1.4.0 // indirect

34
handler/handler.go Normal file
View File

@ -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)
}

View File

@ -40,6 +40,7 @@ func main() {
router.POST("/api/joinPrivate", joinPrivateGame) router.POST("/api/joinPrivate", joinPrivateGame)
router.GET("/api/ws", registerWebSocketConnection) router.GET("/api/ws", registerWebSocketConnection)
debugMode = true
if debugMode { if debugMode {
log.Println("Starting service WITHOUT TLS") log.Println("Starting service WITHOUT TLS")
log.Fatal(router.Run(":8080")) log.Fatal(router.Run(":8080"))

View File

@ -20,6 +20,10 @@ func NewPassphrase() Passphrase {
return Passphrase(strings.TrimSpace(phrase)) return Passphrase(strings.TrimSpace(phrase))
} }
func NewPassphraseFromString(s string) Passphrase {
return Passphrase(s)
}
func (p Passphrase) String() string { func (p Passphrase) String() string {
return string(p) return string(p)
} }