https dev fix + util cleanup
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -12,7 +12,6 @@ func RegisterTicker() {
|
||||
for range ticker {
|
||||
if len(wsConnections) > 0 {
|
||||
GameOfLifeTick()
|
||||
// fmt.Print("g")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
|
||||
16
src/main.go
16
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")
|
||||
}
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"os"
|
||||
)
|
||||
|
||||
func GetBuildNumber() string {
|
||||
return os.Getenv("BUILD_NUMBER")
|
||||
}
|
||||
19
src/util/util.go
Normal file
19
src/util/util.go
Normal 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")
|
||||
}
|
||||
Reference in New Issue
Block a user