Explorar el Código

Refactored router to separate file
Added stomp

Ronald Peterson hace 10 años
padre
commit
059cc1c6a8
Se han modificado 3 ficheros con 41 adiciones y 20 borrados
  1. 6 20
      main.go
  2. 25 0
      router.go
  3. 10 0
      stomp/stomp.go

+ 6 - 20
main.go

@@ -2,37 +2,23 @@
 package main
 
 import (
-	"battlecamp-go-server/games"
 	"encoding/json"
 	"log"
 	"net/http"
-	"runtime"
 	"strconv"
 
 	"github.com/julienschmidt/httprouter"
+
+	"battlecamp-go-server/games"
+	"battlecamp-go-server/stomp"
 )
 
 var currentGames games.Games = games.New()
 
 func main() {
-	runtime.GOMAXPROCS(runtime.NumCPU())
-
-	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)
-	}
-
-	log.Fatal(http.ListenAndServe(":8080", router))
+	conn := stomp.DailStomp()
+	defer conn.Disconnect()
+	log.Fatal(http.ListenAndServe(":8080", initRouter()))
 }
 
 func showIndex(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {

+ 25 - 0
router.go

@@ -0,0 +1,25 @@
+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
+}

+ 10 - 0
stomp/stomp.go

@@ -0,0 +1,10 @@
+package stomp
+
+import (
+	"github.com/go-stomp/stomp"
+)
+
+func DailStomp() *stomp.Conn {
+	conn, _ := stomp.Dial("tcp", "localhost:61613")
+	return conn
+}