mirror of
https://github.com/kaaninchen/Laterna.git
synced 2026-09-17 19:12:48 +00:00
cooldown
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/siestaw/laterna/server/cmd/internal/config"
|
"github.com/siestaw/laterna/server/cmd/internal/config"
|
||||||
"github.com/siestaw/laterna/server/cmd/internal/db"
|
"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")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
json.NewEncoder(w).Encode(state)
|
json.NewEncoder(w).Encode(state)
|
||||||
|
defer r.Body.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
func setCurrent(w http.ResponseWriter, r *http.Request) {
|
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())
|
utils.HTTPErrorHandling(w, r, http.StatusBadRequest, err.Error())
|
||||||
return
|
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
|
var req models.LampUpdateRequest
|
||||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||||
@@ -63,11 +75,11 @@ func setCurrent(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
updatedState, err := db.ViewColor(id)
|
updatedState := currentState
|
||||||
if err != nil {
|
updatedState.Color = req.Color
|
||||||
utils.HTTPErrorHandling(w, r, http.StatusInternalServerError, "Failed to fetch lamp state")
|
updatedState.UpdatedAt = time.Now()
|
||||||
return
|
|
||||||
}
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
json.NewEncoder(w).Encode(updatedState)
|
json.NewEncoder(w).Encode(updatedState)
|
||||||
|
defer r.Body.Close()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,8 +3,9 @@ package models
|
|||||||
import "time"
|
import "time"
|
||||||
|
|
||||||
type HTTPConfig struct {
|
type HTTPConfig struct {
|
||||||
AdminToken string `json:"adminToken"`
|
AdminToken string `json:"adminToken"`
|
||||||
Port int `json:"port"`
|
Port int `json:"port"`
|
||||||
|
Cooldown float64 `json:"cooldown"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
|
|||||||
+2
-1
@@ -3,6 +3,7 @@
|
|||||||
"verboseLogging": true,
|
"verboseLogging": true,
|
||||||
"http": {
|
"http": {
|
||||||
"adminToken": "",
|
"adminToken": "",
|
||||||
"port": 8080
|
"port": 8080,
|
||||||
|
"cooldown": 5,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user