Files
CaddyProxyManager/backend/internal/caddy/provider.go
Pacerino 7ff0949067 [Backend] Add Caddy provider abstraction with Caddyfile and API modes
Introduce a Provider interface selectable via CPM_CADDY_MODE:
- CaddyfileProvider renders per-host snippets (template whitespace fixed)
- APIProvider manages Caddy's JSON config via the admin API
Add configurable reload strategy (systemd/exec/api/none) and a
configurable listen address for the bootstrapped server in API mode.
Includes unit tests for the JSON builder, provider factory and the
API provider (httptest).
2026-06-14 02:34:54 +02:00

32 lines
968 B
Go

package caddy
import (
"fmt"
"strings"
"github.com/Pacerino/CaddyProxyManager/internal/config"
"github.com/Pacerino/CaddyProxyManager/internal/database"
)
// Provider applies host configuration to Caddy. Implementations either write
// per-host Caddyfiles (CaddyfileProvider) or manage Caddy's JSON config through
// its admin API (APIProvider).
type Provider interface {
// WriteHost creates or updates the configuration for a host.
WriteHost(h database.Host) error
// RemoveHost removes the configuration for a host.
RemoveHost(hostID int) error
}
// GetProvider returns the configured Provider based on CPM_CADDY_MODE.
func GetProvider() (Provider, error) {
switch strings.ToLower(config.Configuration.Caddy.Mode) {
case "", "caddyfile":
return &CaddyfileProvider{}, nil
case "api":
return NewAPIProvider(), nil
default:
return nil, fmt.Errorf("unknown caddy mode %q (expected \"caddyfile\" or \"api\")", config.Configuration.Caddy.Mode)
}
}