diff --git a/dockerfile b/dockerfile index f2c4026..503c1f7 100644 --- a/dockerfile +++ b/dockerfile @@ -12,7 +12,7 @@ RUN go mod download COPY ./src ./src # Build the Go binary with static linking for Linux -RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o /server ./src +RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -v -o /server ./src COPY ./views ./views COPY ./build_number ./build_number diff --git a/src/boxes/box-db.go b/src/boxes/box-db.go index 5f52762..065e32c 100644 --- a/src/boxes/box-db.go +++ b/src/boxes/box-db.go @@ -1,7 +1,6 @@ package boxes import ( - "fmt" "math/rand" "time" ) @@ -16,12 +15,9 @@ func initStore() []Box { store := make([]Box, 1024) for i := 0; i < 1024; i++ { state := random.Intn(2) == 1 - fmt.Println(Box{i, state}) store[i] = Box{i, state} } - return store - } func GetBoxes() []Box { diff --git a/src/boxes/box-ticker.go b/src/boxes/box-ticker.go index 4627c8e..01981b9 100644 --- a/src/boxes/box-ticker.go +++ b/src/boxes/box-ticker.go @@ -12,7 +12,6 @@ func RegisterTicker() { for range ticker { if len(wsConnections) > 0 { GameOfLifeTick() - // fmt.Print("g") } } } diff --git a/src/boxes/boxes.go b/src/boxes/boxes.go index 2869ebb..a8bf1bb 100644 --- a/src/boxes/boxes.go +++ b/src/boxes/boxes.go @@ -158,7 +158,7 @@ func handleMessage(msg string, ws *websocket.Conn) error { return nil } -func InitWs(c echo.Context) error { +func HandleBoxesWs(c echo.Context) error { websocket.Handler(func(ws *websocket.Conn) { wsMutex.Lock() wsConnections[ws] = c.Request().UserAgent() @@ -166,10 +166,12 @@ func InitWs(c echo.Context) error { boxes := GetBoxes() msg := "u:" + strconv.Itoa(len(wsConnections)) + ":" - for _, addr := range wsConnections { - msg += addr + ",,," + for _, value := range wsConnections { + msg += value + ",,," } msg += "\n" + broadcastMessage(msg) + msg = "" for _, box := range boxes { msg += box.serialize() diff --git a/src/main.go b/src/main.go index 258e7f6..34462f7 100644 --- a/src/main.go +++ b/src/main.go @@ -50,10 +50,12 @@ func main() { e.Logger.SetLevel(log.DEBUG) e.Use(middleware.Logger()) - e.Use(middleware.Gzip()) - e.Use(middleware.HTTPSRedirect()) + if util.IsProd() { + e.Use(middleware.Gzip()) + e.Use(middleware.HTTPSRedirect()) + } - e.GET("/health", healthCheck) + e.GET("/health", util.HealthCheck) e.Use(func(next echo.HandlerFunc) echo.HandlerFunc { return func(c echo.Context) error { @@ -68,12 +70,10 @@ func main() { e.Static("/css", "css") e.Static("/js", "js") - e.Logger.Info(boxes.GetBoxes()) - e.GET("/", func(c echo.Context) error { return c.Render(200, "index", newPage(boxes.GetBoxes())) }) - e.GET("/boxes/ws", boxes.InitWs) + e.GET("/boxes/ws", boxes.HandleBoxesWs) e.File("/favicon.ico", "images/favicon.ico") @@ -84,7 +84,3 @@ func main() { e.Logger.Fatal(e.Start(":54321")) } - -func healthCheck(c echo.Context) error { - return c.String(200, "OK") -} diff --git a/src/util/build-number.go b/src/util/build-number.go deleted file mode 100644 index 3501a9c..0000000 --- a/src/util/build-number.go +++ /dev/null @@ -1,9 +0,0 @@ -package util - -import ( - "os" -) - -func GetBuildNumber() string { - return os.Getenv("BUILD_NUMBER") -} diff --git a/src/util/util.go b/src/util/util.go new file mode 100644 index 0000000..9c3e851 --- /dev/null +++ b/src/util/util.go @@ -0,0 +1,19 @@ +package util + +import ( + "os" + + "github.com/labstack/echo/v4" +) + +func GetBuildNumber() string { + return os.Getenv("BUILD_NUMBER") +} + +func IsProd() bool { + return len(GetBuildNumber()) > 0 +} + +func HealthCheck(c echo.Context) error { + return c.String(200, "OK") +}