🔨 Makes structural changes to API
All checks were successful
Gitea Build Action / build (push) Successful in 25s
All checks were successful
Gitea Build Action / build (push) Successful in 25s
This commit is contained in:
parent
d1080e6e15
commit
cb07efede6
@ -8,7 +8,6 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
@ -36,10 +35,19 @@ var gotifyURL string
|
|||||||
|
|
||||||
var mut sync.Mutex
|
var mut sync.Mutex
|
||||||
|
|
||||||
type WriteReq struct {
|
type ChangeOpenReq struct {
|
||||||
Opened bool `json:"opened"`
|
Opened bool `json:"opened"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ChangeLockReq struct {
|
||||||
|
Locked bool `json:"locked"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type ChangeAlertReq struct {
|
||||||
|
Alert bool `json:"alert"`
|
||||||
|
For int `json:"for"`
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
_ = godotenv.Load()
|
_ = godotenv.Load()
|
||||||
token = os.Getenv("TOKEN")
|
token = os.Getenv("TOKEN")
|
||||||
@ -106,7 +114,7 @@ func main() {
|
|||||||
return c.JSON(http.StatusOK, "Hello world!")
|
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()
|
mut.Lock()
|
||||||
o := opened
|
o := opened
|
||||||
mut.Unlock()
|
mut.Unlock()
|
||||||
@ -114,19 +122,19 @@ func main() {
|
|||||||
return c.JSON(http.StatusOK, o)
|
return c.JSON(http.StatusOK, o)
|
||||||
}))
|
}))
|
||||||
|
|
||||||
app.POST("/write", authed(func(c echo.Context) error {
|
app.POST("/open", authed(func(c echo.Context) error {
|
||||||
var data WriteReq
|
var data ChangeOpenReq
|
||||||
err := c.Bind(&data)
|
err := c.Bind(&data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return c.NoContent(http.StatusBadRequest)
|
return c.NoContent(http.StatusBadRequest)
|
||||||
}
|
}
|
||||||
|
|
||||||
mut.Lock()
|
|
||||||
if data.Opened == opened {
|
if data.Opened == opened {
|
||||||
mut.Unlock()
|
mut.Unlock()
|
||||||
return c.NoContent(http.StatusOK)
|
return c.NoContent(http.StatusOK)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mut.Lock()
|
||||||
opened = data.Opened
|
opened = data.Opened
|
||||||
if locked && alert {
|
if locked && alert {
|
||||||
var action string
|
var action string
|
||||||
@ -151,23 +159,25 @@ func main() {
|
|||||||
return c.JSON(http.StatusOK, l)
|
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()
|
mut.Lock()
|
||||||
locked = true
|
locked = data.Locked
|
||||||
mut.Unlock()
|
mut.Unlock()
|
||||||
|
|
||||||
return c.NoContent(http.StatusOK)
|
return c.NoContent(http.StatusOK)
|
||||||
}))
|
}))
|
||||||
|
|
||||||
app.POST("/lock/undo", authed(func(c echo.Context) error {
|
app.GET("/alert", 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 {
|
|
||||||
mut.Lock()
|
mut.Lock()
|
||||||
a := alert
|
a := alert
|
||||||
mut.Unlock()
|
mut.Unlock()
|
||||||
@ -175,33 +185,25 @@ func main() {
|
|||||||
return c.JSON(http.StatusOK, a)
|
return c.JSON(http.StatusOK, a)
|
||||||
}))
|
}))
|
||||||
|
|
||||||
app.POST("/alerts/pause", authed(func(c echo.Context) error {
|
app.POST("/alert", authed(func(c echo.Context) error {
|
||||||
pauseForRaw := c.QueryParam("for")
|
var data ChangeAlertReq
|
||||||
if pauseForRaw != "" {
|
err := c.Bind(&data)
|
||||||
pauseFor, err := strconv.Atoi(pauseForRaw)
|
if err != nil {
|
||||||
if err != nil || pauseFor <= 0 {
|
return err
|
||||||
return c.JSON(http.StatusBadRequest, "query param 'for' not valid")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if data.For > 0 {
|
||||||
go func() {
|
go func() {
|
||||||
time.Sleep(time.Duration(pauseFor) * time.Second)
|
time.Sleep(time.Duration(data.For) * time.Second)
|
||||||
|
|
||||||
mut.Lock()
|
mut.Lock()
|
||||||
alert = true
|
alert = !data.Alert
|
||||||
mut.Unlock()
|
mut.Unlock()
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
mut.Lock()
|
mut.Lock()
|
||||||
alert = false
|
alert = data.Alert
|
||||||
mut.Unlock()
|
|
||||||
|
|
||||||
return c.NoContent(http.StatusOK)
|
|
||||||
}))
|
|
||||||
|
|
||||||
app.POST("/alerts/resume", authed(func(c echo.Context) error {
|
|
||||||
mut.Lock()
|
|
||||||
alert = false
|
|
||||||
mut.Unlock()
|
mut.Unlock()
|
||||||
|
|
||||||
return c.NoContent(http.StatusOK)
|
return c.NoContent(http.StatusOK)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user