commit a971e97b104fad62156733f766a7cf52766de837 Author: Daniel Date: Mon Apr 6 20:28:46 2026 +0200 Initial commit: KonCad — web UI for managing dockerized Caddy Co-Authored-By: Claude Sonnet 4.6 diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..8849cba --- /dev/null +++ b/.env.example @@ -0,0 +1,7 @@ +# URL of Caddy's admin API — must be accessible from the KonCad container +# Use the service name as hostname when both run in the same compose stack +CADDY_ADMIN_URL=http://caddy:2019 + +# Path to the Caddyfile inside the KonCad container +# Should match the volume mount path (default Caddy path) +CADDYFILE_PATH=/etc/caddy/Caddyfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..09ad920 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,11 @@ +FROM php:8.2-apache + +# curl extension is bundled in the official image; enable it +RUN docker-php-ext-enable curl 2>/dev/null || \ + (apt-get update && apt-get install -y --no-install-recommends libcurl4-openssl-dev \ + && docker-php-ext-install curl \ + && rm -rf /var/lib/apt/lists/*) + +COPY app/ /var/www/html/ + +EXPOSE 80 diff --git a/app/index.php b/app/index.php new file mode 100644 index 0000000..2ed2516 --- /dev/null +++ b/app/index.php @@ -0,0 +1,742 @@ + '', 'sites' => []]; + $lines = explode("\n", $content); + $n = count($lines); + $i = 0; + + while ($i < $n) { + $trimmed = trim($lines[$i]); + + if ($trimmed === '' || str_starts_with($trimmed, '#')) { $i++; continue; } + + // Global options block: lone opening brace + if ($trimmed === '{') { + $block = []; $depth = 1; $i++; + while ($i < $n && $depth > 0) { + $depth += substr_count($lines[$i], '{') - substr_count($lines[$i], '}'); + if ($depth > 0) $block[] = $lines[$i]; + $i++; + } + $result['global'] = implode("\n", $block); + continue; + } + + // Site block: address(es) { + if (preg_match('/^(.+?)\s*\{(.*)$/', $trimmed, $m)) { + $address = trim($m[1]); + $block = trim($m[2]) !== '' ? [trim($m[2])] : []; + $depth = 1; $i++; + while ($i < $n && $depth > 0) { + $depth += substr_count($lines[$i], '{') - substr_count($lines[$i], '}'); + if ($depth > 0) $block[] = $lines[$i]; + $i++; + } + $result['sites'][] = ['address' => $address, 'body' => implode("\n", $block)]; + continue; + } + + $i++; + } + return $result; +} + +function buildCaddyfile(string $global, array $sites): string { + $out = ''; + if (trim($global) !== '') $out .= "{\n" . $global . "\n}\n\n"; + foreach ($sites as $site) { + $out .= trim($site['address']) . " {\n" . $site['body'] . "\n}\n\n"; + } + return rtrim($out) . "\n"; +} + +function caddyLoad(string $caddyfile): array { + $url = rtrim(CADDY_ADMIN_URL, '/') . '/load'; + $ch = curl_init($url); + curl_setopt_array($ch, [ + CURLOPT_POST => true, + CURLOPT_POSTFIELDS => $caddyfile, + CURLOPT_HTTPHEADER => ['Content-Type: text/caddyfile'], + CURLOPT_RETURNTRANSFER => true, + CURLOPT_TIMEOUT => 10, + ]); + $body = curl_exec($ch); + $status = curl_getinfo($ch, CURLINFO_HTTP_CODE); + $err = curl_error($ch); + curl_close($ch); + + if ($err) return ['ok' => false, 'error' => 'Cannot reach Caddy admin API: ' . $err]; + if ($status === 200) return ['ok' => true]; + // Try to extract a clean message from Caddy's JSON error response + $decoded = json_decode($body, true); + $msg = $decoded['error'] ?? trim($body) ?: "HTTP $status"; + return ['ok' => false, 'error' => $msg]; +} + +function checkUpstream(string $host, int $port): bool { + $fp = @fsockopen($host, $port, $errno, $errstr, 3); + if ($fp) { fclose($fp); return true; } + return false; +} + +// ─── API handlers ───────────────────────────────────────────────────────────── +if (isset($_GET['action'])) { + header('Content-Type: application/json'); + + switch ($_GET['action']) { + + case 'load': + $raw = file_exists(CADDYFILE_PATH) ? file_get_contents(CADDYFILE_PATH) : ''; + echo json_encode(['ok' => true, 'data' => parseCaddyfile($raw)]); + exit; + + case 'save': + $input = json_decode(file_get_contents('php://input'), true); + if (!$input) { echo json_encode(['ok' => false, 'error' => 'Invalid JSON']); exit; } + + $cf = buildCaddyfile($input['global'] ?? '', $input['sites'] ?? []); + $result = caddyLoad($cf); + if (!$result['ok']) { echo json_encode($result); exit; } + + if (file_put_contents(CADDYFILE_PATH, $cf) === false) { + echo json_encode(['ok' => false, 'error' => 'Config applied but failed to write Caddyfile to disk']); + exit; + } + echo json_encode(['ok' => true]); + exit; + + case 'check_upstreams': + $input = json_decode(file_get_contents('php://input'), true); + $targets = $input['targets'] ?? []; + $results = []; + foreach ($targets as $t) { + $host = $t['host'] ?? ''; + $port = (int)($t['port'] ?? 80); + if ($host === '') continue; + $results[] = [ + 'host' => $host, + 'port' => $port, + 'reachable' => checkUpstream($host, $port), + ]; + } + echo json_encode(['ok' => true, 'results' => $results]); + exit; + } + + echo json_encode(['ok' => false, 'error' => 'Unknown action']); + exit; +} +?> + + + + + +KonCad + + + + +
+

KonCad

+ Loading… + +
+ +
+ +
+
+ ▶ + Global Options + +
+
+
+ + — required for KonCad to reach Caddy +
+ +
+
+ + +
+
+ Sites + +
+
+
No sites configured. Click "+ Add Site" to create one.
+
+
+ + + + +
+ + + + diff --git a/caddy/Caddyfile.example b/caddy/Caddyfile.example new file mode 100644 index 0000000..a4a41a9 --- /dev/null +++ b/caddy/Caddyfile.example @@ -0,0 +1,10 @@ +{ + # Required: expose admin API on the Docker network so KonCad can reach it + admin :2019 + + # email admin@example.com +} + +# example.com { +# reverse_proxy localhost:3000 +# } diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..7ce944f --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,30 @@ +services: + + caddy: + image: caddy:latest + restart: unless-stopped + ports: + - "80:80" + - "443:443" + volumes: + # Shared Caddyfile directory — both services mount the same path + - ./caddy:/etc/caddy + - caddy_data:/data + - caddy_config:/config + # NOTE: Your Caddyfile must contain `admin :2019` in the global block + # for KonCad to communicate with Caddy. See caddy/Caddyfile.example + + koncad: + build: . + restart: unless-stopped + ports: + - "8080:80" + volumes: + - ./caddy:/etc/caddy + env_file: .env + depends_on: + - caddy + +volumes: + caddy_data: + caddy_config: