🚚 Moves files into separate directories

This commit is contained in:
Daniel Svitan
2025-05-08 12:54:25 +02:00
parent a23632d5df
commit 0e1e4643cc
6 changed files with 0 additions and 0 deletions

21
server/go.mod Normal file
View File

@@ -0,0 +1,21 @@
module gitea.svitan.dev/Streamer272/door-alarm
go 1.24.2
require (
github.com/labstack/echo/v4 v4.13.3
github.com/labstack/gommon v0.4.2
)
require (
github.com/joho/godotenv v1.5.1 // indirect
github.com/mattn/go-colorable v0.1.14 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
golang.org/x/crypto v0.36.0 // indirect
golang.org/x/net v0.37.0 // indirect
golang.org/x/sys v0.31.0 // indirect
golang.org/x/text v0.23.0 // indirect
golang.org/x/time v0.11.0 // indirect
)

33
server/go.sum Normal file
View File

@@ -0,0 +1,33 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/labstack/echo/v4 v4.13.3 h1:pwhpCPrTl5qry5HRdM5FwdXnhXSLSY+WE+YQSeCaafY=
github.com/labstack/echo/v4 v4.13.3/go.mod h1:o90YNEeQWjDozo584l7AwhJMHN0bOC4tAfg+Xox9q5g=
github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0=
github.com/labstack/gommon v0.4.2/go.mod h1:QlUFxVM+SNXhDL/Z7YhocGIBYOiwB0mXm1+1bAPHPyU=
github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE=
github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo=
github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
golang.org/x/crypto v0.36.0 h1:AnAEvhDddvBdpY+uR+MyHmuZzzNqXSe/GvuDeob5L34=
golang.org/x/crypto v0.36.0/go.mod h1:Y4J0ReaxCR1IMaabaSMugxJES1EpwhBHhv2bDHklZvc=
golang.org/x/net v0.37.0 h1:1zLorHbz+LYj7MQlSf1+2tPIIgibq2eL5xkrGk6f+2c=
golang.org/x/net v0.37.0/go.mod h1:ivrbrMbzFq5J41QOQh0siUuly180yBYtLp+CKbEaFx8=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik=
golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
golang.org/x/text v0.23.0 h1:D71I7dUrlY+VX0gQShAThNGHFxZ13dGLBHQLVl1mJlY=
golang.org/x/text v0.23.0/go.mod h1:/BLNzu4aZCJ1+kcD0DNRotWKage4q2rGVAg4o22unh4=
golang.org/x/time v0.11.0 h1:/bpjEDfN9tkoN/ryeYHnv5hcMlc8ncjMcM4XBk5NWV0=
golang.org/x/time v0.11.0/go.mod h1:CDIdPxbZBQxdj6cxyCIdrNogrJKMJ7pr37NYpMcMDSg=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

245
server/main.go Normal file
View File

@@ -0,0 +1,245 @@
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os"
"strconv"
"strings"
"sync"
"time"
"github.com/joho/godotenv"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/labstack/gommon/log"
)
const TimeFormat = "2006-01-02 15:04:05"
var token string
// the condition is: distance >= threshold (is door open)
var opened bool
// is door locked
var locked bool = false
// alert the user when door locked and but open?
var alert bool = false
var gotifyToken string
var gotifyURL string
var mut sync.Mutex
type WriteReq struct {
Opened bool `json:"opened"`
}
func main() {
_ = godotenv.Load()
token = os.Getenv("TOKEN")
alertRaw := strings.ToLower(os.Getenv("USE_ALERTS"))
alert = alertRaw == "true" || alertRaw == "t" || alertRaw == "1" || alertRaw == "y" || alertRaw == "yes"
gotifyToken = os.Getenv("GOTIFY_TOKEN")
gotifyURL = os.Getenv("GOTIFY_URL")
if alert && gotifyToken == "" {
log.Fatal("GOTIFY_TOKEN can't be empty when alerts are enabled")
}
if alert && gotifyURL == "" {
log.Fatal("GOTIFY_URL can't be empty when alerts are enabled")
}
app := echo.New()
app.Logger.SetLevel(log.INFO)
log.SetLevel(log.INFO)
app.Use(middleware.RecoverWithConfig(middleware.RecoverConfig{
StackSize: 1 << 10,
LogLevel: log.ERROR,
}))
app.Use(middleware.Secure())
app.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
Format: "${time_custom} ${method} ${uri} ---> ${status} in ${latency_human} (${bytes_out} bytes)\n",
CustomTimeFormat: TimeFormat,
}))
log.SetHeader("${time_rfc3339} ${level}")
app.Use(middleware.RemoveTrailingSlash())
app.OnAddRouteHandler = func(host string, route echo.Route, handler echo.HandlerFunc, middleware []echo.MiddlewareFunc) {
now := time.Now()
fmt.Printf("%s registered %-6s %s\n", now.Format(TimeFormat), route.Method, route.Path)
}
app.HTTPErrorHandler = func(err error, c echo.Context) {
if c.Response().Committed {
log.Error(err)
return
}
code := http.StatusInternalServerError
var response any = err.Error()
var httpErr *echo.HTTPError
if errors.As(err, &httpErr) {
code = httpErr.Code
response = httpErr
}
if code >= 500 {
log.Error(err)
}
err2 := c.JSON(code, response)
if err2 != nil {
log.Error(err2)
}
}
app.GET("/", func(c echo.Context) error {
return c.JSON(http.StatusOK, "Hello, World!")
})
app.GET("/read", authed(func(c echo.Context) error {
mut.Lock()
o := opened
mut.Unlock()
return c.JSON(http.StatusOK, o)
}))
app.POST("/write", authed(func(c echo.Context) error {
var data WriteReq
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)
}
opened = data.Opened
if locked && alert {
var action string
if opened {
action = "opened"
} else {
action = "closed"
}
go sendAlert(action)
}
mut.Unlock()
return c.NoContent(http.StatusOK)
}))
app.GET("/locked", authed(func(c echo.Context) error {
mut.Lock()
l := locked
mut.Unlock()
return c.JSON(http.StatusOK, l)
}))
app.POST("/lock", authed(func(c echo.Context) error {
mut.Lock()
locked = true
mut.Unlock()
return c.NoContent(http.StatusOK)
}))
app.POST("/unlock", authed(func(c echo.Context) error {
mut.Lock()
locked = false
mut.Unlock()
return c.NoContent(http.StatusOK)
}))
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")
}
go func() {
time.Sleep(time.Duration(pauseFor) * time.Second)
mut.Lock()
alert = true
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
mut.Unlock()
return c.NoContent(http.StatusOK)
}))
log.Fatal(app.Start(":1323"))
}
func authed(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
if c.Request().Header.Get("Authorization") != token {
return c.NoContent(http.StatusUnauthorized)
}
return next(c)
}
}
func sendAlert(action string) {
to := fmt.Sprintf("%s/message?token=%s", gotifyURL, gotifyToken)
what := echo.Map{
"title": fmt.Sprintf("Your door has been %s", action),
"priority": 1,
"message": fmt.Sprintf("Your locked door has been %s at %s", action, time.Now().Format(TimeFormat)),
}
b, err := json.Marshal(&what)
if err != nil {
log.Error(err)
return
}
res, err := http.Post(to, "application/json", bytes.NewBuffer(b))
if err != nil {
log.Error(err)
return
}
body, err := io.ReadAll(res.Body)
if err != nil {
log.Error(err)
return
}
if res.StatusCode != 200 {
log.Errorf("sendAlert response status code is not 200: %d, body:\n%s", res.StatusCode, body)
} else {
log.Infof("sent alert (%s) to gotify", action)
}
}