Create lobby
This commit is contained in:
parent
6b1a5c948f
commit
4866c6fe6b
40
controller/lobby.go
Normal file
40
controller/lobby.go
Normal 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
20
controller/utils.go
Normal 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
|
||||
}
|
22
main.go
22
main.go
@ -1,13 +1,13 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"card-jong-be/controller"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/nrednav/cuid2"
|
||||
@ -30,6 +30,7 @@ func main() {
|
||||
|
||||
router.HandleFunc("/register", Register)
|
||||
router.HandleFunc("/validate", ValidateId)
|
||||
router.HandleFunc("/lobby/new", controller.CreateLobby).Methods("POST")
|
||||
|
||||
port, ok := os.LookupEnv("PORT")
|
||||
if !ok {
|
||||
@ -94,22 +95,9 @@ func Register(writer http.ResponseWriter, request *http.Request) {
|
||||
}
|
||||
|
||||
func ValidateId(writer http.ResponseWriter, request *http.Request) {
|
||||
// (try to) get the Bearer token
|
||||
reqToken := request.Header.Get("Authorization")
|
||||
if !strings.HasPrefix(reqToken, "Bearer ") {
|
||||
// return 401
|
||||
if controller.AuthHeaderIsValid(&users, request.Header.Get("Authorization")) {
|
||||
writer.WriteHeader(http.StatusOK)
|
||||
} else {
|
||||
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)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user