🔨 Makes structural changes to API
All checks were successful
Gitea Build Action / build (push) Successful in 25s

This commit is contained in:
Daniel Svitan 2025-06-01 10:44:22 +02:00
parent d1080e6e15
commit cb07efede6

View File

@ -8,7 +8,6 @@ import (
"io"
"net/http"
"os"
"strconv"
"strings"
"sync"
"time"
@ -36,10 +35,19 @@ var gotifyURL string
var mut sync.Mutex
type WriteReq struct {
type ChangeOpenReq struct {
Opened bool `json:"opened"`
}
type ChangeLockReq struct {
Locked bool `json:"locked"`
}
type ChangeAlertReq struct {
Alert bool `json:"alert"`
For int `json:"for"`
}
func main() {
_ = godotenv.Load()
token = os.Getenv("TOKEN")
@ -106,7 +114,7 @@ func main() {
return c.JSON(http.StatusOK, "Hello world!")
})
app.GET("/read", authed(func(c echo.Context) error {
app.GET("/open", authed(func(c echo.Context) error {
mut.Lock()
o := opened
mut.Unlock()
@ -114,19 +122,19 @@ func main() {
return c.JSON(http.StatusOK, o)
}))
app.POST("/write", authed(func(c echo.Context) error {
var data WriteReq
app.POST("/open", authed(func(c echo.Context) error {
var data ChangeOpenReq
err := c.Bind(&data)
if err != nil {
return c.NoContent(http.StatusBadRequest)
}
mut.Lock()
if data.Opened == opened {
mut.Unlock()
return c.NoContent(http.StatusOK)
}
mut.Lock()
opened = data.Opened
if locked && alert {
var action string
@ -151,23 +159,25 @@ func main() {
return c.JSON(http.StatusOK, l)
}))
app.POST("/lock/do", authed(func(c echo.Context) error {
app.POST("/lock", authed(func(c echo.Context) error {
var data ChangeLockReq
err := c.Bind(&data)
if err != nil {
return err
}
if data.Locked == locked {
return c.NoContent(http.StatusOK)
}
mut.Lock()
locked = true
locked = data.Locked
mut.Unlock()
return c.NoContent(http.StatusOK)
}))
app.POST("/lock/undo", authed(func(c echo.Context) error {
mut.Lock()
locked = false
mut.Unlock()
return c.NoContent(http.StatusOK)
}))
app.GET("/alerts", authed(func(c echo.Context) error {
app.GET("/alert", authed(func(c echo.Context) error {
mut.Lock()
a := alert
mut.Unlock()
@ -175,33 +185,25 @@ func main() {
return c.JSON(http.StatusOK, a)
}))
app.POST("/alerts/pause", authed(func(c echo.Context) error {
pauseForRaw := c.QueryParam("for")
if pauseForRaw != "" {
pauseFor, err := strconv.Atoi(pauseForRaw)
if err != nil || pauseFor <= 0 {
return c.JSON(http.StatusBadRequest, "query param 'for' not valid")
}
app.POST("/alert", authed(func(c echo.Context) error {
var data ChangeAlertReq
err := c.Bind(&data)
if err != nil {
return err
}
if data.For > 0 {
go func() {
time.Sleep(time.Duration(pauseFor) * time.Second)
time.Sleep(time.Duration(data.For) * time.Second)
mut.Lock()
alert = true
alert = !data.Alert
mut.Unlock()
}()
}
mut.Lock()
alert = false
mut.Unlock()
return c.NoContent(http.StatusOK)
}))
app.POST("/alerts/resume", authed(func(c echo.Context) error {
mut.Lock()
alert = false
alert = data.Alert
mut.Unlock()
return c.NoContent(http.StatusOK)