Ver código fonte

Verplaats url routing, stub voor showPlayer

Harry de Boer 10 anos atrás
pai
commit
0eade8f4af
3 arquivos alterados com 129 adições e 125 exclusões
  1. 2 100
      main.go
  2. 0 25
      router.go
  3. 127 0
      urlrouter.go

+ 2 - 100
main.go

@@ -2,14 +2,11 @@
 package main
 
 import (
-	"encoding/json"
 	"log"
 	"net/http"
-	"strconv"
 
-	"github.com/julienschmidt/httprouter"
 	extStomp "github.com/go-stomp/stomp"
-	
+
 	"battlecamp-go-server/games"
 	"battlecamp-go-server/stomp"
 )
@@ -17,103 +14,8 @@ import (
 var currentGames games.Games = games.New()
 var stompConnection *extStomp.Conn
 
-
 func main() {
 	stompConnection = stomp.DailStomp()
 	defer stompConnection.Disconnect()
-	log.Fatal(http.ListenAndServe(":8080", initRouter()))
-}
-
-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) {
-
-	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 listGames(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
-	w.Header().Set("Content-Type", "application/json")
-	e := json.NewEncoder(w)
-	e.Encode(currentGames.ListGames())
-}
-
-func createGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
-	xString := ps.ByName("x")
-	yString := ps.ByName("y")
-	var game *games.Game
-
-	if xString != "" && yString != "" {
-		x, _ := strconv.Atoi(xString)
-		y, _ := strconv.Atoi(yString)
-		game = games.NewGame(x, y)
-	} else {
-		game = games.NewGame(56, 35)
-	}
-
-	currentGames.AddGame(game, stompConnection)
-	w.WriteHeader(http.StatusCreated)
-	e := json.NewEncoder(w)
-	e.Encode(game)
-}
-
-func joinGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
-	http.Error(w, "joinGame not implemented", http.StatusNotImplemented)
+	log.Fatal(http.ListenAndServe(":8080", newUrlRouter()))
 }
-
-func showGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
-	w.Header().Set("Content-Type", "application/json")
-	id, _ := strconv.ParseInt(ps.ByName("gameid"), 10, 64)
-	e := json.NewEncoder(w)
-	e.Encode(currentGames.GetGame(id))
-}
-
-func listPlayers(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
-	http.Error(w, "listPlayers not implemented", http.StatusNotImplemented)
-}
-
-func movePlayer(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
-	http.Error(w, "movePlayer not implemented", http.StatusNotImplemented)
-}
-
-// Request params: x={x}&y={y}&rows={rows}&cols={cols}
-func showBoard(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
-	w.Header().Set("Content-Type", "application/json")
-	xString := req.FormValue("x")
-	yString := req.FormValue("y")
-	colsString := req.FormValue("cols")
-	rowsString := req.FormValue("rows")
-
-	id, _ := strconv.ParseInt(ps.ByName("boardid"), 10, 64)
-	b := currentGames.GetGame(id).Board
-
-	if xString != "" && yString != "" && colsString != "" && rowsString != "" {
-		x, _ := strconv.Atoi(xString)
-		y, _ := strconv.Atoi(yString)
-		cols, _ := strconv.Atoi(colsString)
-		rows, _ := strconv.Atoi(rowsString)
-		b.WriteJSON(w, x, y, cols, rows)
-	} else if xString != "" || yString != "" || colsString != "" || rowsString != "" {
-		http.Error(w, "Bad request", http.StatusBadRequest)
-	} else {
-		b.WriteJSON(w, 0, 0, b.Cols, b.Rows)
-	}
-}
-
-func showAsciiBoard(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
-	w.Header().Set("Content-Type", "text/plain;charset=utf-8")
-
-	id, _ := strconv.ParseInt(ps.ByName("boardid"), 10, 64)
-	b := currentGames.GetGame(id).Board.String()
-	w.Write([]byte(b))
-}
-

+ 0 - 25
router.go

@@ -1,25 +0,0 @@
-package main
-
-import (
-	"net/http"
-
-	"github.com/julienschmidt/httprouter"
-)
-
-func initRouter() *httprouter.Router {
-	router := httprouter.New()
-	router.GET("/", showIndex)
-	router.GET("/games", listGames)
-	router.PUT("/games", createGame)
-	router.PUT("/games/:rows/:cols", createGame)
-	router.GET("/games/:gameid", showGame)
-	router.GET("/games/:gameid/:boardid", proxyHandler)
-	//router.GET("/games/:gameid/players", playerListHandler)
-	// TODO: /games/{id}/players/{playerId}
-	router.POST("/games/:gameid/move/:playerId/:direction", movePlayer)
-
-	router.PanicHandler = func(w http.ResponseWriter, r *http.Request, something interface{}) {
-		http.Error(w, "Internal server error", http.StatusInternalServerError)
-	}
-	return router
-}

+ 127 - 0
urlrouter.go

@@ -0,0 +1,127 @@
+package main
+
+import (
+	"battlecamp-go-server/games"
+	"encoding/json"
+	"fmt"
+	"net/http"
+	"strconv"
+
+	"github.com/julienschmidt/httprouter"
+)
+
+func newUrlRouter() *httprouter.Router {
+	router := httprouter.New()
+	router.GET("/", showIndex)
+	router.GET("/games", listGames)
+	router.PUT("/games", createGame)
+	router.PUT("/games/:rows/:cols", createGame)
+	router.GET("/games/:gameid", showGame)
+	router.GET("/games/:gameid/:boardid", proxyHandler)
+	router.GET("/games/:gameid/:boardid/:playerid", showPlayer)
+	router.POST("/games/:gameid/move/:playerId/:direction", movePlayer)
+
+	router.PanicHandler = func(w http.ResponseWriter, r *http.Request, something interface{}) {
+		http.Error(w, fmt.Sprintf("Internal server error: %v", something), http.StatusInternalServerError)
+	}
+
+	return router
+}
+
+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) {
+
+	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 listGames(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
+	w.Header().Set("Content-Type", "application/json")
+	e := json.NewEncoder(w)
+	e.Encode(currentGames.ListGames())
+}
+
+func createGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
+	xString := ps.ByName("x")
+	yString := ps.ByName("y")
+	var game *games.Game
+
+	if xString != "" && yString != "" {
+		x, _ := strconv.Atoi(xString)
+		y, _ := strconv.Atoi(yString)
+		game = games.NewGame(x, y)
+	} else {
+		game = games.NewGame(56, 35)
+	}
+
+	currentGames.AddGame(game, stompConnection)
+	w.WriteHeader(http.StatusCreated)
+	e := json.NewEncoder(w)
+	e.Encode(game)
+}
+
+func joinGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
+	http.Error(w, "joinGame not implemented", http.StatusNotImplemented)
+}
+
+func showGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
+	w.Header().Set("Content-Type", "application/json")
+	id, _ := strconv.ParseInt(ps.ByName("gameid"), 10, 64)
+	e := json.NewEncoder(w)
+	e.Encode(currentGames.GetGame(id))
+}
+
+func listPlayers(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
+	http.Error(w, "listPlayers not implemented", http.StatusNotImplemented)
+}
+
+func showPlayer(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
+	http.Error(w, "showPlayer not implemented", http.StatusNotImplemented)
+}
+
+func movePlayer(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
+
+	http.Error(w, "movePlayer not implemented", http.StatusNotImplemented)
+}
+
+// Request params: x={x}&y={y}&rows={rows}&cols={cols}
+func showBoard(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
+	w.Header().Set("Content-Type", "application/json")
+	xString := req.FormValue("x")
+	yString := req.FormValue("y")
+	colsString := req.FormValue("cols")
+	rowsString := req.FormValue("rows")
+
+	id, _ := strconv.ParseInt(ps.ByName("boardid"), 10, 64)
+	b := currentGames.GetGame(id).Board
+
+	if xString != "" && yString != "" && colsString != "" && rowsString != "" {
+		x, _ := strconv.Atoi(xString)
+		y, _ := strconv.Atoi(yString)
+		cols, _ := strconv.Atoi(colsString)
+		rows, _ := strconv.Atoi(rowsString)
+		b.WriteJSON(w, x, y, cols, rows)
+	} else if xString != "" || yString != "" || colsString != "" || rowsString != "" {
+		http.Error(w, "Bad request", http.StatusBadRequest)
+	} else {
+		b.WriteJSON(w, 0, 0, b.Cols, b.Rows)
+	}
+}
+
+func showAsciiBoard(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
+	w.Header().Set("Content-Type", "text/plain;charset=utf-8")
+
+	id, _ := strconv.ParseInt(ps.ByName("boardid"), 10, 64)
+	b := currentGames.GetGame(id).Board.String()
+	w.Write([]byte(b))
+}