feat: initial implementation of EnvGuard with improved TUI layout
This commit is contained in:
67
internal/api/client.go
Normal file
67
internal/api/client.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"envguard/internal/models"
|
||||
)
|
||||
|
||||
const baseURL = "http://localhost:8080"
|
||||
|
||||
var client = &http.Client{Timeout: 5 * time.Second}
|
||||
|
||||
func GetServices() ([]models.Service, error) {
|
||||
resp, err := client.Get(baseURL + "/services")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
var services []models.Service
|
||||
if err := json.NewDecoder(resp.Body).Decode(&services); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return services, nil
|
||||
}
|
||||
|
||||
func LockService(serviceName, user string) error {
|
||||
reqBody := models.LockRequest{
|
||||
ServiceName: serviceName,
|
||||
User: user,
|
||||
}
|
||||
data, _ := json.Marshal(reqBody)
|
||||
|
||||
resp, err := client.Post(baseURL+"/lock", "application/json", bytes.NewBuffer(data))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("failed to lock: status %d", resp.StatusCode)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func UnlockService(serviceName, user string) error {
|
||||
reqBody := models.UnlockRequest{
|
||||
ServiceName: serviceName,
|
||||
User: user,
|
||||
}
|
||||
data, _ := json.Marshal(reqBody)
|
||||
|
||||
resp, err := client.Post(baseURL+"/unlock", "application/json", bytes.NewBuffer(data))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("failed to unlock: status %d", resp.StatusCode)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user