Add smart Caddy auto-configuration

This commit is contained in:
Azat
2026-02-02 22:20:36 +01:00
parent 373fef6f1b
commit d88b2104a2
2 changed files with 60 additions and 1 deletions

View File

@@ -29,6 +29,7 @@ Installs [ttyd](https://github.com/tsl0922/ttyd) - a simple command-line tool fo
| `TTYD_COMMAND` | Command to run | `/bin/bash` | | `TTYD_COMMAND` | Command to run | `/bin/bash` |
| `TTYD_CREDENTIAL` | Basic auth (user:pass) | (none) | | `TTYD_CREDENTIAL` | Basic auth (user:pass) | (none) |
| `TTYD_OPTIONS` | Additional ttyd options | (none) | | `TTYD_OPTIONS` | Additional ttyd options | (none) |
| `TTYD_DOMAIN` | Domain for Caddy auto-config | (none) |
## Usage ## Usage
@@ -54,7 +55,20 @@ TTYD_CREDENTIAL="admin:secret" ./scripts/run.sh
TTYD_COMMAND="htop" ./scripts/run.sh TTYD_COMMAND="htop" ./scripts/run.sh
``` ```
### With Caddy (HTTPS) ### Auto-configuration with Caddy
If the `caddy` skill is present, ttyd will automatically register itself:
```bash
# Set domain for auto-config
TTYD_DOMAIN="terminal.example.com"
```
This creates a snippet in `caddy/snippets.d/ttyd.caddy` that Caddy imports on startup.
### Manual Caddy Config
If not using auto-config:
```caddyfile ```caddyfile
terminal.example.com { terminal.example.com {

View File

@@ -2,6 +2,9 @@
set -e set -e
TTYD_VERSION="${TTYD_VERSION:-1.7.7}" TTYD_VERSION="${TTYD_VERSION:-1.7.7}"
SKILLS_DIR="${SKILLS_DIR:-/skills}"
TTYD_PORT="${TTYD_PORT:-7681}"
TTYD_DOMAIN="${TTYD_DOMAIN:-}"
# Idempotent ttyd installation # Idempotent ttyd installation
install_ttyd() { install_ttyd() {
@@ -32,4 +35,46 @@ install_ttyd() {
echo "ttyd installed: $(ttyd --version)" echo "ttyd installed: $(ttyd --version)"
} }
# Configure Caddy if present
configure_caddy() {
local caddy_dir="$SKILLS_DIR/caddy"
if [ ! -d "$caddy_dir" ]; then
echo "Caddy not found - ttyd will run standalone on port $TTYD_PORT"
return 0
fi
echo "Caddy detected - configuring reverse proxy..."
# Create snippets directory for modular config
local snippets_dir="$caddy_dir/snippets.d"
mkdir -p "$snippets_dir"
# Generate ttyd snippet
local snippet="$snippets_dir/ttyd.caddy"
if [ -n "$TTYD_DOMAIN" ]; then
# Domain-based routing
cat > "$snippet" << EOF
# Auto-generated by ttyd skill
$TTYD_DOMAIN {
reverse_proxy localhost:$TTYD_PORT
}
EOF
echo "Caddy config: $TTYD_DOMAIN -> localhost:$TTYD_PORT"
else
# Path-based routing (fallback)
cat > "$snippet" << EOF
# Auto-generated by ttyd skill
# Add to your site block:
# handle /terminal/* {
# uri strip_prefix /terminal
# reverse_proxy localhost:$TTYD_PORT
# }
EOF
echo "Caddy snippet created: $snippet (manual path-based config)"
fi
}
install_ttyd install_ttyd
configure_caddy