Create lobby

master
Araozu 2024-05-16 16:17:41 -05:00
parent 6b1a5c948f
commit 4866c6fe6b
4 changed files with 66 additions and 18 deletions

40
controller/lobby.go Normal file
View File

@ -0,0 +1,40 @@
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")
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)
}

20
controller/utils.go Normal file
View File

@ -0,0 +1,20 @@
package controller
import (
"strings"
)
func AuthHeaderIsValid(users *map[string]string, authHeader string) bool {
// (try to) get the Bearer token
reqToken := authHeader
if !strings.HasPrefix(reqToken, "Bearer ") {
return false
}
bearerToken := reqToken[7:]
// Check that the token is in the global map
_, ok := (*users)[bearerToken]
return ok
}

2
go.mod
View File

@ -1,4 +1,4 @@
module git.araozu.dev/card-jong-be module card-jong-be
go 1.22.2 go 1.22.2

24
main.go
View File

@ -1,13 +1,13 @@
package main package main
import ( import (
"card-jong-be/controller"
"encoding/json" "encoding/json"
"fmt" "fmt"
"log" "log"
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
"strings"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/nrednav/cuid2" "github.com/nrednav/cuid2"
@ -30,6 +30,7 @@ func main() {
router.HandleFunc("/register", Register) router.HandleFunc("/register", Register)
router.HandleFunc("/validate", ValidateId) router.HandleFunc("/validate", ValidateId)
router.HandleFunc("/lobby/new", controller.CreateLobby).Methods("POST")
port, ok := os.LookupEnv("PORT") port, ok := os.LookupEnv("PORT")
if !ok { if !ok {
@ -94,22 +95,9 @@ func Register(writer http.ResponseWriter, request *http.Request) {
} }
func ValidateId(writer http.ResponseWriter, request *http.Request) { func ValidateId(writer http.ResponseWriter, request *http.Request) {
// (try to) get the Bearer token if controller.AuthHeaderIsValid(&users, request.Header.Get("Authorization")) {
reqToken := request.Header.Get("Authorization")
if !strings.HasPrefix(reqToken, "Bearer ") {
// return 401
writer.WriteHeader(http.StatusUnauthorized)
}
bearerToken := reqToken[7:]
// Check that the token is in the global map
_, ok := users[bearerToken]
if !ok {
// Return 401
writer.WriteHeader(http.StatusUnauthorized)
}
// Return Ok
writer.WriteHeader(http.StatusOK) writer.WriteHeader(http.StatusOK)
} else {
writer.WriteHeader(http.StatusUnauthorized)
}
} }