diff --git a/backend/heats.go b/backend/heats.go index 31c0288..c0eaafc 100644 --- a/backend/heats.go +++ b/backend/heats.go @@ -5,8 +5,10 @@ import ( "log" "net/http" "strings" + "time" "github.com/gin-gonic/gin" + scribble "github.com/nanobox-io/golang-scribble" ) type Surfer struct { @@ -91,3 +93,78 @@ func (app *App) DeleteHeat(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"status": "deleted"}) } + +func (app *App) StartHeatTimer(c *gin.Context) { + var msg Message + var err error + var timer time.Duration + + if app.Stream.Start { + c.JSON(http.StatusOK, app.Stream.Duration) + return + } + + err = c.ShouldBind(&msg) + if err != nil { + log.Printf("req error: %+v", err) + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + + timer, err = time.ParseDuration(msg.Duration) + if err != nil { + log.Printf("req error: %+v", err) + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + app.Stream.Duration = timer + app.Stream.Start = true + + startHeat(app.DB, *app.Stream.Heat) + + log.Printf("start timer %s - received %s", app.Stream.Duration, msg.Duration) + c.JSON(http.StatusOK, app.Stream.Duration) +} + +func (app *App) StopHeatTimer(c *gin.Context) { + if !app.Stream.Start { + c.JSON(http.StatusOK, app.Stream.Duration) + return + } + + stopHeat(app.DB, *app.Stream.Heat) + app.Stream.Start = false + app.Stream.Duration = 0 + + log.Printf("start timer %s", app.Stream.Duration) + c.JSON(http.StatusOK, app.Stream.Duration) +} + +func startHeat(db *scribble.Driver, heat Heat) error { + log.Printf("heat: %+v", heat) + + heat.Status = "running" + + err := db.Write("Heat", heatName(heat), heat) + if err != nil { + log.Printf("set error: %+v", err) + return err + } + + return nil +} + +func stopHeat(db *scribble.Driver, heat Heat) error { + + log.Printf("heat: %+v", heat) + + heat.Status = "ended" + + err := db.Write("Heat", heatName(heat), heat) + if err != nil { + log.Printf("set error: %+v", err) + return err + } + + return nil +} diff --git a/sse/be/be b/sse/be/be index bbab047..a241f47 100755 Binary files a/sse/be/be and b/sse/be/be differ diff --git a/sse/be/main.go b/sse/be/main.go index 4639bd2..49596f1 100644 --- a/sse/be/main.go +++ b/sse/be/main.go @@ -95,7 +95,7 @@ func (e *Event) Stream(c *gin.Context) { client := &Client{ Ip: c.Request.RemoteAddr, Chan: make(ClientChan), - Events: c.QueryArray("events"), + Events: c.QueryArray("event"), } client.Id = Id(client.Ip, client.Events, 8) diff --git a/sse/be/static/_app/version.json b/sse/be/static/_app/version.json index 3276484..ada3707 100644 --- a/sse/be/static/_app/version.json +++ b/sse/be/static/_app/version.json @@ -1 +1 @@ -{"version":"1704454074172"} \ No newline at end of file +{"version":"1704556541085"} \ No newline at end of file diff --git a/sse/be/static/_app/version.json.br b/sse/be/static/_app/version.json.br index 45115cb..146aaa1 100644 Binary files a/sse/be/static/_app/version.json.br and b/sse/be/static/_app/version.json.br differ diff --git a/sse/be/static/_app/version.json.gz b/sse/be/static/_app/version.json.gz index 7765d67..0596cbf 100644 Binary files a/sse/be/static/_app/version.json.gz and b/sse/be/static/_app/version.json.gz differ diff --git a/sse/be/static/index.html b/sse/be/static/index.html index c955f1c..4151202 100644 --- a/sse/be/static/index.html +++ b/sse/be/static/index.html @@ -5,17 +5,17 @@ - + - - + +

{msg}

-
+

Ping: {dping}

-
+

Pong: {dpong}

- -
+
-
+