Marco
d7c4f28f3a
Had to add several helpers (e.g. passphrase ones) to make the endpoint for getting lobby id work. Moved all handler functions into handler package. Added test for getting lobby from passphrase.
95 lines
2.6 KiB
Go
95 lines
2.6 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"flag"
|
|
"fmt"
|
|
"log"
|
|
"mchess_server/api"
|
|
"mchess_server/handler"
|
|
lobbies "mchess_server/lobby_registry"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"nhooyr.io/websocket"
|
|
)
|
|
|
|
var cert_path = "/etc/letsencrypt/live/chess.sw-gross.de/"
|
|
var cert_file = cert_path + "fullchain.pem"
|
|
var key_file = cert_path + "privkey.pem"
|
|
|
|
func main() {
|
|
var debugMode bool
|
|
|
|
debugModeLong := flag.Bool("debug", false, "activates debug mode")
|
|
debugModeShort := flag.Bool("d", false, "activates debug mode")
|
|
flag.Parse()
|
|
if *debugModeShort || *debugModeLong {
|
|
debugMode = true
|
|
}
|
|
|
|
router := gin.Default()
|
|
router.GET("/api/random", handler.RegisterForRandomGame)
|
|
router.GET("/api/hostPrivate", handler.HostPrivateGameHandler)
|
|
router.POST("/api/joinPrivate", handler.JoinPrivateGame)
|
|
router.GET("/api/ws", registerWebSocketConnection)
|
|
router.GET("/api/getLobbyForPassphrase/:phrase", handler.GetLobbyForPassphraseHandler)
|
|
|
|
if debugMode {
|
|
log.Println("Starting service WITHOUT TLS")
|
|
log.Fatal(router.Run(":8080"))
|
|
} else {
|
|
gin.SetMode(gin.ReleaseMode)
|
|
log.Println("Starting in release mode")
|
|
log.Println("Starting service with TLS")
|
|
log.Fatal(router.RunTLS("chess.sw-gross.de:9999", cert_file, key_file))
|
|
}
|
|
}
|
|
|
|
func registerWebSocketConnection(c *gin.Context) {
|
|
webSocketConn, err := websocket.Accept(c.Writer, c.Request, &websocket.AcceptOptions{OriginPatterns: []string{"chess.sw-gross.de", "localhost:*"}})
|
|
if err != nil {
|
|
log.Println(err)
|
|
return
|
|
}
|
|
go waitForAndHandlePlayerID(c, webSocketConn)
|
|
}
|
|
|
|
func waitForAndHandlePlayerID(ctx context.Context, conn *websocket.Conn) {
|
|
msgType, msg, err := conn.Read(ctx)
|
|
if err != nil {
|
|
errorMessage := fmt.Sprintf("Reading from websocket connection did not work: %s", err)
|
|
log.Println(errorMessage)
|
|
conn.Close(websocket.StatusCode(400), errorMessage)
|
|
return
|
|
}
|
|
|
|
log.Println("read from websocket endpoint: ", msgType, string(msg), err)
|
|
|
|
var info api.PlayerInfo
|
|
err = json.Unmarshal(msg, &info)
|
|
if err != nil {
|
|
errorMessage := fmt.Sprintf("Unmarshaling message did not work: %s", err)
|
|
log.Println(errorMessage)
|
|
conn.Close(websocket.StatusCode(400), errorMessage)
|
|
return
|
|
}
|
|
|
|
lobby := lobbies.GetLobbyRegistry().GetLobbyByUUID(*info.LobbyID)
|
|
if lobby == nil {
|
|
conn.Close(websocket.StatusCode(400), "lobby not found")
|
|
return
|
|
}
|
|
|
|
player, found := lobby.GetPlayerByUUID(*info.PlayerID)
|
|
if !found {
|
|
conn.Close(websocket.StatusCode(400), "player not found")
|
|
return
|
|
}
|
|
if player.Conn.HasWebsocketConnection() {
|
|
player.Conn.Close("closing existing connection")
|
|
}
|
|
lobby.Game.SetWebsocketConnectionFor(ctx, player, conn)
|
|
log.Println("player after setting connection: ", player)
|
|
}
|