https dev fix + util cleanup

This commit is contained in:
JurajKubrican
2025-08-04 21:02:30 +02:00
parent db8fd17376
commit 71cab63397
7 changed files with 31 additions and 28 deletions

View File

@@ -12,7 +12,7 @@ RUN go mod download
COPY ./src ./src COPY ./src ./src
# Build the Go binary with static linking for Linux # 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 ./views ./views
COPY ./build_number ./build_number COPY ./build_number ./build_number

View File

@@ -1,7 +1,6 @@
package boxes package boxes
import ( import (
"fmt"
"math/rand" "math/rand"
"time" "time"
) )
@@ -16,12 +15,9 @@ func initStore() []Box {
store := make([]Box, 1024) store := make([]Box, 1024)
for i := 0; i < 1024; i++ { for i := 0; i < 1024; i++ {
state := random.Intn(2) == 1 state := random.Intn(2) == 1
fmt.Println(Box{i, state})
store[i] = Box{i, state} store[i] = Box{i, state}
} }
return store return store
} }
func GetBoxes() []Box { func GetBoxes() []Box {

View File

@@ -12,7 +12,6 @@ func RegisterTicker() {
for range ticker { for range ticker {
if len(wsConnections) > 0 { if len(wsConnections) > 0 {
GameOfLifeTick() GameOfLifeTick()
// fmt.Print("g")
} }
} }
} }

View File

@@ -158,7 +158,7 @@ func handleMessage(msg string, ws *websocket.Conn) error {
return nil return nil
} }
func InitWs(c echo.Context) error { func HandleBoxesWs(c echo.Context) error {
websocket.Handler(func(ws *websocket.Conn) { websocket.Handler(func(ws *websocket.Conn) {
wsMutex.Lock() wsMutex.Lock()
wsConnections[ws] = c.Request().UserAgent() wsConnections[ws] = c.Request().UserAgent()
@@ -166,10 +166,12 @@ func InitWs(c echo.Context) error {
boxes := GetBoxes() boxes := GetBoxes()
msg := "u:" + strconv.Itoa(len(wsConnections)) + ":" msg := "u:" + strconv.Itoa(len(wsConnections)) + ":"
for _, addr := range wsConnections { for _, value := range wsConnections {
msg += addr + ",,," msg += value + ",,,"
} }
msg += "\n" msg += "\n"
broadcastMessage(msg)
msg = ""
for _, box := range boxes { for _, box := range boxes {
msg += box.serialize() msg += box.serialize()

View File

@@ -50,10 +50,12 @@ func main() {
e.Logger.SetLevel(log.DEBUG) e.Logger.SetLevel(log.DEBUG)
e.Use(middleware.Logger()) e.Use(middleware.Logger())
e.Use(middleware.Gzip()) if util.IsProd() {
e.Use(middleware.HTTPSRedirect()) 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 { e.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error { return func(c echo.Context) error {
@@ -68,12 +70,10 @@ func main() {
e.Static("/css", "css") e.Static("/css", "css")
e.Static("/js", "js") e.Static("/js", "js")
e.Logger.Info(boxes.GetBoxes())
e.GET("/", func(c echo.Context) error { e.GET("/", func(c echo.Context) error {
return c.Render(200, "index", newPage(boxes.GetBoxes())) 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") e.File("/favicon.ico", "images/favicon.ico")
@@ -84,7 +84,3 @@ func main() {
e.Logger.Fatal(e.Start(":54321")) e.Logger.Fatal(e.Start(":54321"))
} }
func healthCheck(c echo.Context) error {
return c.String(200, "OK")
}

View File

@@ -1,9 +0,0 @@
package util
import (
"os"
)
func GetBuildNumber() string {
return os.Getenv("BUILD_NUMBER")
}

19
src/util/util.go Normal file
View File

@@ -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")
}