card-jong-be/controller/lobby.go

47 lines
999 B
Go
Raw Normal View History

2024-05-16 21:17:41 +00:00
package controller
import (
"encoding/json"
"fmt"
"github.com/nrednav/cuid2"
"net/http"
)
// TODO: This struct should have a creation time
type Lobby struct {
LobbyOwner string
LobbyPlayers [3]string
}
type LobbyResult struct {
LobbyId string
}
// TODO: We should remove entries from this map when they expire.
// TODO: Define how long lobbies last
var lobbies = make(map[string]Lobby)
func CreateLobby(writer http.ResponseWriter, request *http.Request) {
writer.Header().Set("Content-Type", "application/json")
2024-05-16 22:15:52 +00:00
authOk := AuthHeaderIsValid(request.Header.Get("Authorization"))
if !authOk {
writer.WriteHeader(http.StatusUnauthorized)
return
}
2024-05-16 21:17:41 +00:00
lobbyId := cuid2.Generate()
result := LobbyResult{LobbyId: lobbyId}
jsonData, err := json.Marshal(result)
if err != nil {
fmt.Printf("Error in JSON marshal: %s\n", err)
writer.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(writer, "{\"error\": \"%s\"}", err)
return
}
fmt.Fprintf(writer, "%s", jsonData)
}