Harry de Boer 10 anni fa
parent
commit
a51a2ea0e9
2 ha cambiato i file con 17 aggiunte e 7 eliminazioni
  1. 4 1
      main.go
  2. 13 6
      urlrouter.go

+ 4 - 1
main.go

@@ -17,5 +17,8 @@ var stompConnection *extStomp.Conn
 func main() {
 	stompConnection = stomp.DailStomp()
 	defer stompConnection.Disconnect()
-	log.Fatal(http.ListenAndServe(":8080", newUrlRouter()))
+	log.Println("Creating url router...")
+	urlRouter := newUrlRouter()
+	log.Println("Starting server...")
+	log.Fatal(http.ListenAndServe(":8080", urlRouter))
 }

+ 13 - 6
urlrouter.go

@@ -16,9 +16,9 @@ func newUrlRouter() *httprouter.Router {
 	router.GET("/", showIndex)
 	router.GET("/games", listGames)
 	router.PUT("/games", createGame)
-	router.PUT("/games/:rows/:cols", createGame)
+	router.PUT("/games/:rows/:cols", putProxyHandler)
 	router.GET("/games/:gameid", showGame)
-	router.GET("/games/:gameid/:boardid", proxyHandler)
+	router.GET("/games/:gameid/:boardid", getProxyHandler)
 	router.GET("/games/:gameid/:boardid/:playerid", showPlayer)
 	router.POST("/games/:gameid/move/:playerId/:direction", movePlayer)
 
@@ -33,19 +33,26 @@ func showIndex(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
 	w.Write([]byte("Go battlecamp!"))
 }
 
-func proxyHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
+func getProxyHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
 
 	if ps.ByName("gameid") == "asciiboard" {
 		showAsciiBoard(w, r, ps)
 	} else if ps.ByName("gameid") == "board" {
 		showBoard(w, r, ps)
-	} else if ps.ByName("boardid") == "join" {
-		joinGame(w, r, ps)
 	} else {
 		listPlayers(w, r, ps)
 	}
 }
 
+func putProxyHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
+
+	if ps.ByName("cols") == "join" {
+		joinGame(w, r, ps)
+	} else {
+		createGame(w, r, ps)
+	}
+}
+
 func listGames(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
 	w.Header().Set("Content-Type", "application/json")
 	e := json.NewEncoder(w)
@@ -72,7 +79,7 @@ func createGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
 
 func joinGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
 	w.Header().Set("Content-Type", "application/json")
-	idString := ps.ByName("gameid")
+	idString := ps.ByName("rows") // gameId... zucht.
 	gameId, _ := strconv.ParseInt(idString, 10, 64)
 	var player player.Player
 	d := json.NewDecoder(r.Body)