From 11ca8f626e29519e03f9522544cec5dc79bf3e24 Mon Sep 17 00:00:00 2001 From: Haletran Date: Tue, 24 Feb 2026 16:14:13 +0100 Subject: [PATCH] feat: all routes working for caddy and add tests --- core/caddy/Dockerfile | 5 ++ core/caddy/compose.yml | 15 ++++- core/caddy/config/{ => sites}/outils.caddy | 9 ++- core/tools/test-caddy | 69 ++++++++++++++++++++++ justfile | 20 +++++++ 5 files changed, 114 insertions(+), 4 deletions(-) create mode 100644 core/caddy/Dockerfile rename core/caddy/config/{ => sites}/outils.caddy (59%) create mode 100644 core/tools/test-caddy create mode 100644 justfile diff --git a/core/caddy/Dockerfile b/core/caddy/Dockerfile new file mode 100644 index 0000000..795a14e --- /dev/null +++ b/core/caddy/Dockerfile @@ -0,0 +1,5 @@ +FROM alpine:3.21 + +COPY config/ /caddyfiles/ + +CMD ["sh", "-c", "cp -r /caddyfiles/. /etc/caddy/ && echo 'Caddyfiles copied.'"] diff --git a/core/caddy/compose.yml b/core/caddy/compose.yml index 2cdebb7..a6b4c3b 100644 --- a/core/caddy/compose.yml +++ b/core/caddy/compose.yml @@ -1,10 +1,21 @@ services: + caddy-init: + container_name: caddy-init + build: + context: . + volumes: + - caddy-caddyfiles:/etc/caddy + restart: "no" + caddy: container_name: caddy image: caddy:2.10.2-alpine restart: unless-stopped + depends_on: + caddy-init: + condition: service_completed_successfully ports: - - "80:80" + - "8080:80" volumes: - caddy-caddyfiles:/etc/caddy - caddy-data:/data @@ -21,3 +32,5 @@ volumes: networks: proxy: name: proxy + driver: bridge + diff --git a/core/caddy/config/outils.caddy b/core/caddy/config/sites/outils.caddy similarity index 59% rename from core/caddy/config/outils.caddy rename to core/caddy/config/sites/outils.caddy index 1d18973..7dd2982 100644 --- a/core/caddy/config/outils.caddy +++ b/core/caddy/config/sites/outils.caddy @@ -1,4 +1,4 @@ -http://outils.localhost { +http://outils.ft-chatons.local { header X-Chatons "miaou" respond /healthcheck "OK" @@ -16,12 +16,15 @@ http://outils.localhost { } handle /ip { - respond "{{.ClientIP}" + respond "{http.request.remote.host}" } handle_path /b64/encode/* { + templates + respond `{{ .Req.URL.Path | trimPrefix "/" | b64enc }}` } - handle_path /b64/decode/* { + templates + respond `{{ .Req.URL.Path | trimPrefix "/" | b64dec }}` } } diff --git a/core/tools/test-caddy b/core/tools/test-caddy new file mode 100644 index 0000000..78b033a --- /dev/null +++ b/core/tools/test-caddy @@ -0,0 +1,69 @@ +#!/usr/bin/env bash + +DOMAIN_NAME="http://outils.ft-chatons.local:8080" + +GREEN='\033[0;32m' +RED='\033[0;31m' +YELLOW='\033[1;33m' +NC='\033[0m' + +now=$(date +"%Y-%m-%d %H:%M:%S") + +test_endpoint() { + local name="$1" + local url="$2" + local expected_code="$3" + local expected_body="$4" + local match_mode="${5:-exact}" + local follow_redirects="${6:-false}" + + echo -n "Testing $name... " + + local tmp + tmp=$(mktemp) + + local curl_args=(-s -o "$tmp" -w "%{http_code}" "$url") + if [ "$follow_redirects" = "true" ]; then + curl_args=(-s -L -o "$tmp" -w "%{http_code}" "$url") + fi + + 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 "Root redirect" "$DOMAIN_NAME/" "200" "Hello world!" "exact" "true" diff --git a/justfile b/justfile new file mode 100644 index 0000000..67c745e --- /dev/null +++ b/justfile @@ -0,0 +1,20 @@ +default: + just --list + +## Start the compose of the param +start container="": + docker compose -f core/{{container}}/compose.yml up -d + +## Stop the compose of the param +stop container="caddy": + docker compose -f core/{{container}}/compose.yml down + +## Restart all the compose +re: stop + 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