database setup and getcurrent

This commit is contained in:
Mitsu
2025-07-08 22:03:03 +02:00
parent 96eed5121f
commit 5281f73148
7 changed files with 119 additions and 30 deletions
+23 -12
View File
@@ -1,35 +1,46 @@
package http
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"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/utils"
)
func StartHTTPServer() {
router := http.NewServeMux()
router.HandleFunc("GET /api/v1/admin/token/new", createToken)
router.HandleFunc("GET /api/v1/id/{ID}", getCurrent)
router.HandleFunc("POST /api/v1/id/{ID}", setCurrent)
port := config.AppConfig.HTTP.Port
logger.HTTPLogger.Printf("HTTP Server running on :%v", port)
http.ListenAndServe(fmt.Sprintf(":%d", port), router)
}
func createToken(w http.ResponseWriter, r *http.Request) { // temporary, only for testing
authHeader := r.Header.Get("Authorization")
adminToken := config.AppConfig.HTTP.AdminToken
func getCurrent(w http.ResponseWriter, r *http.Request) {
idStr := r.PathValue("ID")
id, err := strconv.Atoi(idStr)
if err != nil {
utils.HTTPErrorHandling(w, r, http.StatusBadRequest, "Invalid ID")
return
}
if authHeader == "" {
http.Error(w, "Authorization header missing", http.StatusUnauthorized)
state, err := db.ViewColor(id)
if err != nil {
utils.HTTPErrorHandling(w, r, http.StatusBadRequest, "Lamp not found")
return
}
if authHeader != adminToken {
http.Error(w, "nuh uh", http.StatusUnauthorized)
return
}
print(authHeader)
fmt.Fprintf(w, "Success!!! Token: %v, authHeader: %v", adminToken, authHeader)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(state)
}
func setCurrent(w http.ResponseWriter, r *http.Request) {
}