b17e7d3d5f
- Add Caddy client package (client.go) with Load/GetConfig/Ping methods - Implement config generator (config.go) transforming ProxyHost → Caddy JSON - Add pre-flight validator (validator.go) catching config errors before reload - Create manager (manager.go) with rollback capability using config snapshots - Add CaddyConfig model for audit trail of configuration changes - Update Config to include Caddy admin API and config dir settings - Create comprehensive unit tests with 100% coverage for caddy package Docker Infrastructure: - Add docker-compose.yml with Caddy sidecar container - Add docker-compose.dev.yml for development overrides - Create .github/workflows/docker-publish.yml for GHCR publishing - Update CI to build Docker images and run integration tests - Add DOCKER.md with comprehensive deployment guide - Update Makefile with docker-compose commands - Update README with Docker-first deployment instructions Configuration: - Add CPM_CADDY_ADMIN_API and CPM_CADDY_CONFIG_DIR env vars - Update .env.example with new Caddy settings - Update AutoMigrate to include CaddyConfig model All acceptance criteria met: ✅ Can programmatically generate valid Caddy JSON configs ✅ Can reload Caddy configuration via admin API ✅ Invalid configs caught by validator before reload ✅ Automatic rollback on failure via snapshot system
102 lines
2.5 KiB
Go
102 lines
2.5 KiB
Go
package caddy
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
// Client wraps the Caddy admin API.
|
|
type Client struct {
|
|
baseURL string
|
|
httpClient *http.Client
|
|
}
|
|
|
|
// NewClient creates a Caddy API client.
|
|
func NewClient(adminAPIURL string) *Client {
|
|
return &Client{
|
|
baseURL: adminAPIURL,
|
|
httpClient: &http.Client{
|
|
Timeout: 30 * time.Second,
|
|
},
|
|
}
|
|
}
|
|
|
|
// Load atomically replaces Caddy's entire configuration.
|
|
// This is the primary method for applying configuration changes.
|
|
func (c *Client) Load(ctx context.Context, config *Config) error {
|
|
body, err := json.Marshal(config)
|
|
if err != nil {
|
|
return fmt.Errorf("marshal config: %w", err)
|
|
}
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.baseURL+"/load", bytes.NewReader(body))
|
|
if err != nil {
|
|
return fmt.Errorf("create request: %w", err)
|
|
}
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
resp, err := c.httpClient.Do(req)
|
|
if err != nil {
|
|
return fmt.Errorf("execute request: %w", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
bodyBytes, _ := io.ReadAll(resp.Body)
|
|
return fmt.Errorf("caddy returned status %d: %s", resp.StatusCode, string(bodyBytes))
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// GetConfig retrieves the current running configuration from Caddy.
|
|
func (c *Client) GetConfig(ctx context.Context) (*Config, error) {
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.baseURL+"/config/", nil)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("create request: %w", err)
|
|
}
|
|
|
|
resp, err := c.httpClient.Do(req)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("execute request: %w", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
bodyBytes, _ := io.ReadAll(resp.Body)
|
|
return nil, fmt.Errorf("caddy returned status %d: %s", resp.StatusCode, string(bodyBytes))
|
|
}
|
|
|
|
var config Config
|
|
if err := json.NewDecoder(resp.Body).Decode(&config); err != nil {
|
|
return nil, fmt.Errorf("decode response: %w", err)
|
|
}
|
|
|
|
return &config, nil
|
|
}
|
|
|
|
// Ping checks if Caddy admin API is reachable.
|
|
func (c *Client) Ping(ctx context.Context) error {
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.baseURL+"/config/", nil)
|
|
if err != nil {
|
|
return fmt.Errorf("create request: %w", err)
|
|
}
|
|
|
|
resp, err := c.httpClient.Do(req)
|
|
if err != nil {
|
|
return fmt.Errorf("caddy unreachable: %w", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
return fmt.Errorf("caddy returned status %d", resp.StatusCode)
|
|
}
|
|
|
|
return nil
|
|
}
|