From d4bad260d1bbeec9c2a0adee85c3afbb7f8cc324 Mon Sep 17 00:00:00 2001 From: Haletran Date: Wed, 25 Feb 2026 14:30:47 +0100 Subject: [PATCH] feat: rest of caddy path and domains --- core/caddy/Dockerfile | 8 +++-- core/caddy/compose.yml | 5 +++ core/caddy/config/sites/outils.caddy | 17 +++++++++ core/caddy/init.sh | 22 ++++++++++++ core/tools/test-caddy | 53 ++++++++++++++++++++++++++++ justfile | 6 ++-- 6 files changed, 106 insertions(+), 5 deletions(-) create mode 100644 core/caddy/init.sh mode change 100644 => 100755 core/tools/test-caddy diff --git a/core/caddy/Dockerfile b/core/caddy/Dockerfile index 795a14e..b1d88ed 100644 --- a/core/caddy/Dockerfile +++ b/core/caddy/Dockerfile @@ -1,5 +1,9 @@ FROM alpine:3.21 -COPY config/ /caddyfiles/ +RUN apk add --no-cache wget unzip -CMD ["sh", "-c", "cp -r /caddyfiles/. /etc/caddy/ && echo 'Caddyfiles copied.'"] +COPY config/ /caddyfiles/ +COPY init.sh /init.sh +RUN chmod +x /init.sh + +CMD ["/init.sh"] \ No newline at end of file diff --git a/core/caddy/compose.yml b/core/caddy/compose.yml index a6b4c3b..0d0eb78 100644 --- a/core/caddy/compose.yml +++ b/core/caddy/compose.yml @@ -3,8 +3,11 @@ services: container_name: caddy-init build: context: . + environment: + - DRAWIO_VERSION=29.5.2 volumes: - caddy-caddyfiles:/etc/caddy + - caddy-diagrams:/srv/diagrams restart: "no" caddy: @@ -20,6 +23,7 @@ services: - caddy-caddyfiles:/etc/caddy - caddy-data:/data - caddy-config:/config + - caddy-diagrams:/srv/diagrams networks: - proxy @@ -28,6 +32,7 @@ volumes: name: caddy-caddyfiles caddy-data: caddy-config: + caddy-diagrams: networks: proxy: diff --git a/core/caddy/config/sites/outils.caddy b/core/caddy/config/sites/outils.caddy index 7dd2982..b36818e 100644 --- a/core/caddy/config/sites/outils.caddy +++ b/core/caddy/config/sites/outils.caddy @@ -27,4 +27,21 @@ http://outils.ft-chatons.local { templates respond `{{ .Req.URL.Path | trimPrefix "/" | b64dec }}` } + + basic_auth /secretpage { + # username "towel" password "poisson" + towel $2a$14$/cfdN3/kWYwiw/OeAtLcjOxj/NFZa3gUxqePMSoNh1VmoDzS1BP5S + } + handle_path /secretpage { + respond "Welcome, {http.auth.user.id}" 200 + } + + handle_path /teapot { + respond "HTML Tea! Tea! Teapot!" 418 + } +} + +http://diagrams.ft-chatons.local { + root * /srv/diagrams + file_server } diff --git a/core/caddy/init.sh b/core/caddy/init.sh new file mode 100644 index 0000000..4c13704 --- /dev/null +++ b/core/caddy/init.sh @@ -0,0 +1,22 @@ +#!/bin/sh +set -e + +cp -r /caddyfiles/. /etc/caddy/ +echo "Caddyfiles copied." + +if [ -z "$DRAWIO_VERSION" ]; then + echo "ERROR: DRAWIO_VERSION is not set" + exit 1 +fi + +echo "Downloading diagrams.net v${DRAWIO_VERSION}..." +mkdir -p /srv/diagrams + +wget -v \ + "https://github.com/jgraph/drawio/releases/download/v${DRAWIO_VERSION}/draw.war" \ + -O /tmp/drawio.war + +echo "Extracting..." +unzip -q /tmp/drawio.war -d /srv/diagrams/ +rm /tmp/drawio.war +echo "Done." \ No newline at end of file diff --git a/core/tools/test-caddy b/core/tools/test-caddy old mode 100644 new mode 100755 index 78b033a..63fe3a6 --- a/core/tools/test-caddy +++ b/core/tools/test-caddy @@ -60,10 +60,63 @@ test_endpoint() { return 0 } +test_endpoint_auth() { + local name="$1" + local url="$2" + local username="$3" + local password="$4" + local expected_code="$5" + local expected_body="$6" + local match_mode="${7:-exact}" + + echo -n "Testing $name... " + + local tmp + tmp=$(mktemp) + + local curl_args=(-s -o "$tmp" -w "%{http_code}" -u "$username:$password" "$url") + + local code + code=$(curl "${curl_args[@]}" 2>/dev/null || echo "000") + + local body + body=$(cat "$tmp") + rm -f "$tmp" + + if [ "$code" != "$expected_code" ]; then + echo -e "${RED}✗ FAIL${NC} (HTTP $code, expected $expected_code)" + echo "Response: $body" + return 1 + fi + + if [ -n "$expected_body" ]; then + if [ "$match_mode" = "regex" ]; then + if [[ ! "$body" =~ $expected_body ]]; then + echo -e "${RED}✗ FAIL${NC} (body mismatch)" + echo "Response: $body" + return 1 + fi + else + if [ "$body" != "$expected_body" ]; then + echo -e "${RED}✗ FAIL${NC} (body mismatch)" + echo "Response: $body" + return 1 + fi + fi + fi + + echo -e "${GREEN}✓ OK${NC} (HTTP $code)" + return 0 +} + test_endpoint "Health Check" "$DOMAIN_NAME/healthcheck" "200" "OK" test_endpoint "Date Check" "$DOMAIN_NAME/date" "200" "^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$" "regex" test_endpoint "IP check" "$DOMAIN_NAME/ip" "200" "^[0-9]{1,3}(\.[0-9]{1,3}){3}$" "regex" test_endpoint "Encode base64" "$DOMAIN_NAME/b64/encode/test" "200" "dGVzdA==" test_endpoint "Decode base64" "$DOMAIN_NAME/b64/decode/dGVzdA==" "200" "test" test_endpoint "Hello Check" "$DOMAIN_NAME/hello/Chatons" "200" "Hello Chatons!" +test_endpoint "Teapot Check" "$DOMAIN_NAME/teapot" "418" "HTML Tea! Tea! Teapot!" +test_endpoint "Auth Check Unauthorized" "$DOMAIN_NAME/secretpage" "401" "" +test_endpoint_auth "Auth Check Authorized" "$DOMAIN_NAME/secretpage" "towel" "poisson" "200" "Welcome, towel" "exact" +test_endpoint_auth "Auth Check wrong password" "$DOMAIN_NAME/secretpage" "towel" "fakepassword" "401" test_endpoint "Root redirect" "$DOMAIN_NAME/" "200" "Hello world!" "exact" "true" diff --git a/justfile b/justfile index 67c745e..2e55874 100644 --- a/justfile +++ b/justfile @@ -6,15 +6,15 @@ start container="": docker compose -f core/{{container}}/compose.yml up -d ## Stop the compose of the param -stop container="caddy": +down container="caddy": docker compose -f core/{{container}}/compose.yml down ## Restart all the compose -re: stop +re: down docker volume rm $(docker volume ls -q) || true docker system prune -af docker volume prune -f just start caddy tests: - bash core/tools/test-caddy \ No newline at end of file + bash core/tools/test-caddy