mirror of
https://github.com/kaaninchen/Laterna.git
synced 2026-09-17 19:12:48 +00:00
database setup and getcurrent
This commit is contained in:
+23
-12
@@ -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) {
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user