This commit is contained in:
Mitsu
2025-07-10 20:17:00 +02:00
parent f331492999
commit 5e9433f4ae
3 changed files with 22 additions and 8 deletions
+17 -5
View File
@@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/siestaw/laterna/server/cmd/internal/config"
"github.com/siestaw/laterna/server/cmd/internal/db"
@@ -39,6 +40,7 @@ func getCurrent(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(state)
defer r.Body.Close()
}
func setCurrent(w http.ResponseWriter, r *http.Request) {
@@ -48,6 +50,16 @@ func setCurrent(w http.ResponseWriter, r *http.Request) {
utils.HTTPErrorHandling(w, r, http.StatusBadRequest, err.Error())
return
}
currentState, err := db.ViewColor(id)
if err != nil {
utils.HTTPErrorHandling(w, r, http.StatusInternalServerError, "Failed to get current lamp color")
return
}
if time.Since(currentState.UpdatedAt).Seconds() < config.AppConfig.HTTP.Cooldown {
utils.HTTPErrorHandling(w, r, http.StatusTooManyRequests, "Slow down!")
return
}
var req models.LampUpdateRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
@@ -63,11 +75,11 @@ func setCurrent(w http.ResponseWriter, r *http.Request) {
return
}
updatedState, err := db.ViewColor(id)
if err != nil {
utils.HTTPErrorHandling(w, r, http.StatusInternalServerError, "Failed to fetch lamp state")
return
}
updatedState := currentState
updatedState.Color = req.Color
updatedState.UpdatedAt = time.Now()
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(updatedState)
defer r.Body.Close()
}
+3 -2
View File
@@ -3,8 +3,9 @@ package models
import "time"
type HTTPConfig struct {
AdminToken string `json:"adminToken"`
Port int `json:"port"`
AdminToken string `json:"adminToken"`
Port int `json:"port"`
Cooldown float64 `json:"cooldown"`
}
type Config struct {