Compare commits
No commits in common. "da97b0b7b682c80e763b16f7fc46eeff4ebec0e2" and "af51a0acd5473e005fbff47a889859f1f5b552cb" have entirely different histories.
da97b0b7b6
...
af51a0acd5
@ -1,46 +0,0 @@
|
||||
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")
|
||||
|
||||
authOk := AuthHeaderIsValid(request.Header.Get("Authorization"))
|
||||
if !authOk {
|
||||
writer.WriteHeader(http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"github.com/nrednav/cuid2"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
var Users map[string]string = make(map[string]string)
|
||||
|
||||
func Register(username string) string {
|
||||
uid := cuid2.Generate()
|
||||
|
||||
// Store in the users map
|
||||
Users[uid] = username
|
||||
|
||||
return uid
|
||||
}
|
||||
|
||||
func ValidateId(writer http.ResponseWriter, request *http.Request) {
|
||||
if AuthHeaderIsValid(request.Header.Get("Authorization")) {
|
||||
writer.WriteHeader(http.StatusOK)
|
||||
} else {
|
||||
writer.WriteHeader(http.StatusUnauthorized)
|
||||
}
|
||||
}
|
@ -1,29 +0,0 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func WriteError(err error, message string, writer *http.ResponseWriter) {
|
||||
fmt.Printf("Error: %s\n", err)
|
||||
(*writer).WriteHeader(http.StatusInternalServerError)
|
||||
fmt.Fprintf(*writer, "{\"error\": \"%s\"}", message)
|
||||
return
|
||||
}
|
||||
|
||||
func AuthHeaderIsValid(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
|
||||
}
|
32
main.go
32
main.go
@ -1,7 +1,6 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"card-jong-be/controller"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
@ -10,6 +9,7 @@ import (
|
||||
"os"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/nrednav/cuid2"
|
||||
"github.com/rs/cors"
|
||||
)
|
||||
|
||||
@ -18,13 +18,16 @@ type PersonInfo struct {
|
||||
Username string
|
||||
}
|
||||
|
||||
var users map[string]string
|
||||
|
||||
func main() {
|
||||
fmt.Println("hello SEKAI!!")
|
||||
router := mux.NewRouter().PathPrefix("/api").Subrouter()
|
||||
|
||||
// initialize the global users map
|
||||
users = make(map[string]string)
|
||||
|
||||
router.HandleFunc("/register", Register)
|
||||
router.HandleFunc("/validate", controller.ValidateId)
|
||||
router.HandleFunc("/lobby/new", controller.CreateLobby).Methods("POST")
|
||||
|
||||
port, ok := os.LookupEnv("PORT")
|
||||
if !ok {
|
||||
@ -47,20 +50,29 @@ func Register(writer http.ResponseWriter, request *http.Request) {
|
||||
requestUrl := request.URL
|
||||
params, err := url.ParseQuery(requestUrl.RawQuery)
|
||||
if err != nil {
|
||||
controller.WriteError(err, "Error parsing URL parameters", &writer)
|
||||
fmt.Printf("Error parsing URL parameters: %s\n", err)
|
||||
writer.WriteHeader(http.StatusInternalServerError)
|
||||
fmt.Fprintf(writer, "{\"error\": \"%s\"}", err)
|
||||
return
|
||||
}
|
||||
|
||||
usernameArr, ok := params["username"]
|
||||
if !ok {
|
||||
controller.WriteError(err, "username not found", &writer)
|
||||
fmt.Println("username GET param not found")
|
||||
writer.WriteHeader(http.StatusBadRequest)
|
||||
fmt.Fprintf(writer, "{\"error\": \"username not found\"}")
|
||||
return
|
||||
}
|
||||
username := usernameArr[0]
|
||||
|
||||
uid := cuid2.Generate()
|
||||
|
||||
// Store in the users map
|
||||
users[uid] = username
|
||||
|
||||
// The result json
|
||||
result := PersonInfo{
|
||||
UserId: controller.Register(username),
|
||||
UserId: uid,
|
||||
Username: username,
|
||||
}
|
||||
|
||||
@ -68,7 +80,9 @@ func Register(writer http.ResponseWriter, request *http.Request) {
|
||||
|
||||
jsonData, err := json.Marshal(result)
|
||||
if err != nil {
|
||||
controller.WriteError(err, "Error serializing JSON", &writer)
|
||||
fmt.Printf("Error in JSON marshal: %s\n", err)
|
||||
writer.WriteHeader(http.StatusInternalServerError)
|
||||
fmt.Fprintf(writer, "{\"error\": \"%s\"}", err)
|
||||
return
|
||||
}
|
||||
|
||||
@ -76,3 +90,7 @@ func Register(writer http.ResponseWriter, request *http.Request) {
|
||||
|
||||
fmt.Fprintf(writer, "%s", jsonData)
|
||||
}
|
||||
|
||||
func ValidateId(writer http.ResponseWriter, request *http.Request) {
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user