cache buster

This commit is contained in:
JurajKubrican
2025-03-04 22:01:23 +01:00
parent 00c209032f
commit cac9548058
14 changed files with 115 additions and 99 deletions

View File

@@ -57,13 +57,12 @@ func (box Box) persist() {
log.Fatal(err)
}
res, err := stmt.Exec(box.Id, box.Value)
_, err = stmt.Exec(box.Id, box.Value)
if err != nil {
log.Fatal(err)
}
stmt.Close()
log.Info(res.LastInsertId())
}
func getBox(id int) (Box, error) {

View File

@@ -58,7 +58,6 @@ func handleWsError(err error, conn *websocket.Conn) {
func broadcastMessage(message string) {
wsMutex.Lock()
defer wsMutex.Unlock()
for conn := range wsConnections {
err := websocket.Message.Send(conn, message)
handleWsError(err, conn)
@@ -66,6 +65,22 @@ func broadcastMessage(message string) {
}
}
func randomizeBoxes(count int) []Box {
boxes := make([]Box, count)
for i := 0; i < count; i++ {
index := random.Int() % 1000
box, err := getBox(index)
if err != nil {
log.Fatal(err)
}
box.Value = !box.Value
box.persist()
boxes[i] = box
}
return boxes
}
func handleMessage(msg string, ws *websocket.Conn) error {
if msg == "gol" {
matrix := make(map[string]Box)
@@ -77,7 +92,6 @@ func handleMessage(msg string, ws *websocket.Conn) error {
index++
}
}
log.Error(matrix)
nextGen := make([]Box, 0)
for id, item := range matrix {
nextItem := shouldBeAlive(matrix, id)
@@ -94,16 +108,17 @@ func handleMessage(msg string, ws *websocket.Conn) error {
}
broadcastMessage(nextMessage)
} else if msg == "random" {
index := random.Int() % 1000
box, err := getBox(index)
} else if strings.Contains(msg, "r:") {
count, err := strconv.Atoi(strings.Split(msg, ":")[1])
if err != nil {
log.Fatal(err)
log.Errorf("Failed to parse randomize count: %v", err)
}
boxes := randomizeBoxes(count)
message := ""
for _, box := range boxes {
message += box.serialize()
}
box.Value = !box.Value
box.persist()
message := box.serialize()
broadcastMessage(message)
} else if strings.Contains(msg, "b:") {

View File

@@ -8,6 +8,8 @@ import (
"github.com/labstack/echo/v4"
"github.com/labstack/gommon/log"
"golang.org/x/net/websocket"
"knet.sk/src/util"
)
var (
@@ -86,9 +88,9 @@ func InitWs(c echo.Context) error {
}
type Data struct {
Template string
BuildNumber string
}
func Page(c echo.Context) error {
return c.Render(200, "index2", Data{Template: "draw"})
return c.Render(200, "index2", Data{BuildNumber: util.GetBuildNumber()})
}

View File

@@ -9,6 +9,7 @@ import (
"github.com/labstack/gommon/log"
"knet.sk/src/boxes"
"knet.sk/src/draw"
"knet.sk/src/util"
)
type Templates struct {
@@ -26,12 +27,14 @@ func NewTemplates() *Templates {
}
type Page struct {
Boxes []boxes.Box
Boxes []boxes.Box
BuildNumber string
}
func newPage(boxes []boxes.Box) Page {
return Page{
Boxes: boxes,
Boxes: boxes,
BuildNumber: util.GetBuildNumber(),
}
}
@@ -41,6 +44,7 @@ var (
func main() {
e.Renderer = NewTemplates()
// read the ./build_number file
e.Logger.SetLevel(log.DEBUG)
e.Use(middleware.Logger())

20
src/util/build-number.go Normal file
View File

@@ -0,0 +1,20 @@
package util
import (
"fmt"
"os"
)
var buildNumber string
func GetBuildNumber() string {
if buildNumber == "" {
b, err := os.ReadFile("build_number") // just pass the file name
if err != nil {
fmt.Print(err)
}
buildNumber = string(b)
}
return buildNumber
}