be6ad15efc
Security: apply AdminOnly middleware to mutating routes, require ENCRYPTION_KEY and ADMIN_PASSWORD (no insecure defaults), restrict CORS to same-origin, fix OIDC token delivery via cookie instead of URL query param, add rate limiting on login, add MaxBytesReader, validate volume paths against traversal, add security headers, validate user roles, add Secure flag to OIDC cookie. Performance: set SQLite MaxOpenConns(1) to prevent SQLITE_BUSY, add FK indexes on 8 columns, track notifier goroutines with WaitGroup for graceful shutdown, use GetRegistryByName instead of GetAllRegistries in deployer, pass basePath param to avoid redundant settings query, return empty slices from store to remove reflection. Quality: refactor TriggerDeploy to delegate to runDeploy (~100 lines removed), consolidate duplicated utilities (extractPort, boolToInt, now, isTerminalStatus) into shared exports, migrate all log.Printf to slog structured logging, use consistent webhook response envelope, remove dead code (parseEnvVars, duplicate auth types). UX: clean up NPM proxy on instance removal via API, add README with quickstart guide, add .env.example, require ADMIN_PASSWORD in docker-compose, document staging-net prerequisite.
94 lines
2.4 KiB
Go
94 lines
2.4 KiB
Go
package notify
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"log/slog"
|
|
"net/http"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
// Event represents a deployment notification payload.
|
|
type Event struct {
|
|
Type string `json:"type"` // "deploy_success" or "deploy_failure"
|
|
Project string `json:"project"`
|
|
Stage string `json:"stage"`
|
|
ImageTag string `json:"image_tag"`
|
|
Subdomain string `json:"subdomain"`
|
|
URL string `json:"url,omitempty"`
|
|
Error string `json:"error,omitempty"`
|
|
Timestamp string `json:"timestamp"`
|
|
}
|
|
|
|
// Notifier sends webhook notifications for deploy events.
|
|
// Notifications are fire-and-forget — failures are logged but do not propagate.
|
|
type Notifier struct {
|
|
httpClient *http.Client
|
|
wg sync.WaitGroup
|
|
}
|
|
|
|
// New creates a Notifier with sensible defaults.
|
|
func New() *Notifier {
|
|
return &Notifier{
|
|
httpClient: &http.Client{
|
|
Timeout: 10 * time.Second,
|
|
},
|
|
}
|
|
}
|
|
|
|
// Drain waits for all in-flight notifications to complete.
|
|
func (n *Notifier) Drain() {
|
|
n.wg.Wait()
|
|
}
|
|
|
|
// Send sends a notification event to the given webhook URL in a background goroutine.
|
|
// It does not block the caller. Errors are logged, not returned.
|
|
func (n *Notifier) Send(webhookURL string, event Event) {
|
|
if webhookURL == "" {
|
|
return
|
|
}
|
|
|
|
if event.Timestamp == "" {
|
|
event.Timestamp = time.Now().UTC().Format(time.RFC3339)
|
|
}
|
|
|
|
n.wg.Add(1)
|
|
go func() {
|
|
defer n.wg.Done()
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
if err := n.doSend(ctx, webhookURL, event); err != nil {
|
|
slog.Warn("notify: failed to send webhook", "url", webhookURL, "error", err)
|
|
}
|
|
}()
|
|
}
|
|
|
|
// doSend performs the actual HTTP POST to the webhook URL.
|
|
func (n *Notifier) doSend(ctx context.Context, webhookURL string, event Event) error {
|
|
body, err := json.Marshal(event)
|
|
if err != nil {
|
|
return fmt.Errorf("marshal notification: %w", err)
|
|
}
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, webhookURL, bytes.NewReader(body))
|
|
if err != nil {
|
|
return fmt.Errorf("create notification request: %w", err)
|
|
}
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
resp, err := n.httpClient.Do(req)
|
|
if err != nil {
|
|
return fmt.Errorf("send notification: %w", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
|
return fmt.Errorf("notification webhook returned status %d", resp.StatusCode)
|
|
}
|
|
|
|
return nil
|
|
}
|