From 907cf269cdff3d21f281e806b09811a307ccd0a0 Mon Sep 17 00:00:00 2001 From: Steve Date: Sun, 25 Jul 2021 16:32:41 -0400 Subject: [PATCH] move websocket server out of internal --- cmd/server/main.go | 2 +- {internal/coordinator => cmd/server}/server.go | 11 ++++++----- 2 files changed, 7 insertions(+), 6 deletions(-) rename {internal/coordinator => cmd/server}/server.go (76%) diff --git a/cmd/server/main.go b/cmd/server/main.go index e0fc562..8f33468 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -5,5 +5,5 @@ import "git.saintnet.tech/stryan/snengame/internal/coordinator" func main() { c := coordinator.NewCoordinator() c.Start() - coordinator.Serve(c) + Serve(c) } diff --git a/internal/coordinator/server.go b/cmd/server/server.go similarity index 76% rename from internal/coordinator/server.go rename to cmd/server/server.go index 5bcea60..e5d3b36 100644 --- a/internal/coordinator/server.go +++ b/cmd/server/server.go @@ -1,15 +1,16 @@ -package coordinator +package main import ( "log" "net/http" + "git.saintnet.tech/stryan/snengame/internal/coordinator" "github.com/gorilla/websocket" ) var upgrader = websocket.Upgrader{} // use default options -func Serve(c *Coordinator) { +func Serve(c *coordinator.Coordinator) { http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) { serveWs(c, w, r) }) @@ -19,7 +20,7 @@ func Serve(c *Coordinator) { } } -func serveWs(c *Coordinator, w http.ResponseWriter, r *http.Request) { +func serveWs(c *coordinator.Coordinator, w http.ResponseWriter, r *http.Request) { // Upgrade our raw HTTP connection to a websocket based one conn, err := upgrader.Upgrade(w, r, nil) if err != nil { @@ -30,7 +31,7 @@ func serveWs(c *Coordinator, w http.ResponseWriter, r *http.Request) { // The event loop for { - var cmd SessionCommand + var cmd coordinator.SessionCommand err := conn.ReadJSON(&cmd) if err != nil { log.Println("Error during message reading:", err) @@ -47,7 +48,7 @@ func serveWs(c *Coordinator, w http.ResponseWriter, r *http.Request) { log.Println("Error during message writing:", err) break } - if resp.Result == SessionRespLeft { + if resp.Result == coordinator.SessionRespLeft { break } }