mirror of
https://github.com/kaaninchen/Laterna.git
synced 2026-09-17 19:12:48 +00:00
rethought the auth system and realized that it was bullshit
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/siestaw/laterna/server/cmd/internal/config"
|
||||
"github.com/siestaw/laterna/server/cmd/internal/db"
|
||||
"github.com/siestaw/laterna/server/cmd/internal/logger"
|
||||
"github.com/siestaw/laterna/server/cmd/internal/models"
|
||||
"github.com/siestaw/laterna/server/cmd/utils"
|
||||
)
|
||||
|
||||
func RegisterRoutes(mux *http.ServeMux) {
|
||||
mux.HandleFunc("GET /api/v1/id/{ID}", getCurrent)
|
||||
mux.HandleFunc("PUT /api/v1/id/{ID}", setCurrent)
|
||||
|
||||
mux.HandleFunc("GET /api/v1/controllers", getControllers)
|
||||
mux.HandleFunc("PUT /api/v1/controllers", setControllers)
|
||||
mux.HandleFunc("DELETE /api/v1/controllers/{id}", deleteController)
|
||||
}
|
||||
|
||||
func getCurrent(w http.ResponseWriter, r *http.Request) {
|
||||
token := r.Header.Get("Authorization")
|
||||
if !db.IsAdmin(token) {
|
||||
utils.HTTPErrorHandling(w, r, http.StatusUnauthorized, "Invalid token")
|
||||
return
|
||||
}
|
||||
|
||||
idStr := r.PathValue("ID")
|
||||
id, err := utils.IDtoInt(idStr)
|
||||
if err != nil {
|
||||
utils.HTTPErrorHandling(w, r, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
state, err := db.ViewColor(id)
|
||||
if err != nil {
|
||||
utils.HTTPErrorHandling(w, r, http.StatusBadRequest, "Lamp not found")
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(state)
|
||||
defer r.Body.Close()
|
||||
}
|
||||
|
||||
func setCurrent(w http.ResponseWriter, r *http.Request) {
|
||||
token := r.Header.Get("Authorization")
|
||||
if !db.IsAdmin(token) {
|
||||
utils.HTTPErrorHandling(w, r, http.StatusUnauthorized, "Invalid token")
|
||||
return
|
||||
}
|
||||
|
||||
idStr := r.PathValue("ID")
|
||||
id, err := utils.IDtoInt(idStr)
|
||||
if err != nil {
|
||||
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 {
|
||||
logger.HTTPLogger.Print(err)
|
||||
utils.HTTPErrorHandling(w, r, http.StatusBadRequest, "Invalid JSON")
|
||||
return
|
||||
}
|
||||
|
||||
err = db.SetColor(id, req.Color)
|
||||
if err != nil {
|
||||
logger.HTTPLogger.Printf("Could not update lamp %d: %s", id, err)
|
||||
utils.HTTPErrorHandling(w, r, http.StatusInternalServerError, err.Error())
|
||||
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()
|
||||
}
|
||||
|
||||
func getControllers(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
func setControllers(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
func deleteController(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user