feat: initial implementation of EnvGuard with improved TUI layout
This commit is contained in:
112
cmd/server/main.go
Normal file
112
cmd/server/main.go
Normal file
@@ -0,0 +1,112 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"envguard/internal/models"
|
||||
)
|
||||
|
||||
var (
|
||||
services = []models.Service{
|
||||
{Name: "auth-service"},
|
||||
{Name: "payments-api"},
|
||||
{Name: "user-db"},
|
||||
{Name: "notifications"},
|
||||
{Name: "front-web"},
|
||||
}
|
||||
mu sync.Mutex
|
||||
)
|
||||
|
||||
func main() {
|
||||
http.HandleFunc("/services", handleServices)
|
||||
http.HandleFunc("/lock", handleLock)
|
||||
http.HandleFunc("/unlock", handleUnlock)
|
||||
|
||||
fmt.Println("🚦 Lock Server corriendo en :8080")
|
||||
if err := http.ListenAndServe(":8080", nil); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func handleServices(w http.ResponseWriter, r *http.Request) {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(services)
|
||||
}
|
||||
|
||||
func handleLock(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
var req models.LockRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
|
||||
for i := range services {
|
||||
if services[i].Name == req.ServiceName {
|
||||
if services[i].IsLocked {
|
||||
http.Error(w, "Service already locked", http.StatusConflict)
|
||||
return
|
||||
}
|
||||
services[i].IsLocked = true
|
||||
services[i].LockedBy = req.User
|
||||
services[i].LockedAt = time.Now()
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
http.Error(w, "Service not found", http.StatusNotFound)
|
||||
}
|
||||
|
||||
func handleUnlock(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
var req models.UnlockRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
|
||||
for i := range services {
|
||||
if services[i].Name == req.ServiceName {
|
||||
if !services[i].IsLocked {
|
||||
http.Error(w, "Service is not locked", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if services[i].LockedBy != req.User {
|
||||
http.Error(w, "You cannot unlock a service locked by someone else", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
services[i].IsLocked = false
|
||||
services[i].LockedBy = ""
|
||||
services[i].LockedAt = time.Time{}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
http.Error(w, "Service not found", http.StatusNotFound)
|
||||
}
|
||||
Reference in New Issue
Block a user