feat: add api security, update services list and add gitignore

This commit is contained in:
Jose Luis Montañes Ojados
2026-01-28 15:35:22 +01:00
parent a857c1a051
commit f0b4a0142d
4 changed files with 254 additions and 11 deletions

View File

@@ -16,6 +16,7 @@ var (
services = []models.Service{}
mu sync.Mutex
dataFile = "services.json"
apiToken = "ENVGUARD_SECRET_TOKEN"
)
func main() {
@@ -32,9 +33,9 @@ func main() {
fmt.Printf("✅ %d servicios cargados desde disco\n", len(services))
}
http.HandleFunc("/services", handleServices)
http.HandleFunc("/lock", handleLock)
http.HandleFunc("/unlock", handleUnlock)
http.HandleFunc("/services", authMiddleware(handleServices))
http.HandleFunc("/lock", authMiddleware(handleLock))
http.HandleFunc("/unlock", authMiddleware(handleUnlock))
fmt.Println("🚦 Lock Server corriendo en :8080")
if err := http.ListenAndServe(":8080", nil); err != nil {
@@ -148,3 +149,14 @@ func handleUnlock(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Service not found", http.StatusNotFound)
}
func authMiddleware(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
token := r.Header.Get("X-API-Key")
if token != apiToken {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
next(w, r)
}
}