feat: add api security, update services list and add gitignore
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user