From 91dfc166660f88086b7fdde08a05831043be341b Mon Sep 17 00:00:00 2001 From: Thomas Schaffter Date: Mon, 2 Dec 2024 23:50:28 +0000 Subject: [PATCH 1/3] Replace Nginx by Caddy project --- apps/openchallenges/apex/Caddyfile | 31 +++++ apps/openchallenges/apex/Dockerfile | 14 +-- apps/openchallenges/apex/README.md | 8 -- apps/openchallenges/apex/nginx.conf | 8 -- apps/openchallenges/apex/project.json | 8 +- .../apex/templates/events.conf.template | 3 - .../apex/templates/http.conf.template | 118 ------------------ docker/openchallenges/services/apex.yml | 8 +- 8 files changed, 39 insertions(+), 159 deletions(-) create mode 100644 apps/openchallenges/apex/Caddyfile delete mode 100644 apps/openchallenges/apex/README.md delete mode 100644 apps/openchallenges/apex/nginx.conf delete mode 100644 apps/openchallenges/apex/templates/events.conf.template delete mode 100644 apps/openchallenges/apex/templates/http.conf.template diff --git a/apps/openchallenges/apex/Caddyfile b/apps/openchallenges/apex/Caddyfile new file mode 100644 index 0000000000..56e172700d --- /dev/null +++ b/apps/openchallenges/apex/Caddyfile @@ -0,0 +1,31 @@ +{ + debug +} + +:80 { + handle_path /api-docs* { + reverse_proxy {env.API_DOCS_HOST}:{env.API_DOCS_PORT} + } + + handle_path /api* { + reverse_proxy {env.API_GATEWAY_HOST}:{env.API_GATEWAY_PORT} + } + + handle_path /health { + respond `{"status":"healthy","message":"OK"}` 200 + header Content-Type application/json + } + + handle_path /img* { + reverse_proxy {env.THUMBOR_HOST}:{env.THUMBOR_PORT} + } + + handle_path /zipkin* { + rewrite * /zipkin{uri} + reverse_proxy {env.ZIPKIN_HOST}:{env.ZIPKIN_PORT} + } + + handle { + reverse_proxy {env.APP_HOST}:{env.APP_PORT} + } +} \ No newline at end of file diff --git a/apps/openchallenges/apex/Dockerfile b/apps/openchallenges/apex/Dockerfile index a64f490dff..fa4dba30fa 100644 --- a/apps/openchallenges/apex/Dockerfile +++ b/apps/openchallenges/apex/Dockerfile @@ -1,13 +1,3 @@ -FROM nginx:1.25.1-alpine +FROM caddy:2.8.4 -RUN apk add --no-cache jq - -HEALTHCHECK --interval=2s --timeout=3s --retries=20 --start-period=5s \ - CMD curl --fail --silent "localhost:8000/health" | jq '.status' | grep UP || exit 1 - -COPY templates /etc/nginx/templates/ -COPY nginx.conf /etc/nginx/ - -EXPOSE 8000 - -CMD ["nginx", "-g", "daemon off;"] \ No newline at end of file +# COPY Caddyfile /etc/caddy/ \ No newline at end of file diff --git a/apps/openchallenges/apex/README.md b/apps/openchallenges/apex/README.md deleted file mode 100644 index 2f3d70250e..0000000000 --- a/apps/openchallenges/apex/README.md +++ /dev/null @@ -1,8 +0,0 @@ -# OpenChallenges Apex - -## Follow access log - -```console -docker exec -it openchallenges-apex \ - tail -f /var/log/nginx/openchallenges-apex.access.log -``` diff --git a/apps/openchallenges/apex/nginx.conf b/apps/openchallenges/apex/nginx.conf deleted file mode 100644 index 9a7c682c76..0000000000 --- a/apps/openchallenges/apex/nginx.conf +++ /dev/null @@ -1,8 +0,0 @@ -user nginx; -worker_processes auto; - -error_log /var/log/nginx/error.log notice; -pid /var/run/nginx.pid; - -include conf.d/events.conf; -include conf.d/http.conf; \ No newline at end of file diff --git a/apps/openchallenges/apex/project.json b/apps/openchallenges/apex/project.json index 82141a791c..6653cd011e 100644 --- a/apps/openchallenges/apex/project.json +++ b/apps/openchallenges/apex/project.json @@ -1,7 +1,6 @@ { "name": "openchallenges-apex", "$schema": "../../../node_modules/nx/schemas/project-schema.json", - "sourceRoot": "apps/openchallenges-apex/src", "projectType": "application", "targets": { "create-config": { @@ -14,17 +13,16 @@ "serve-detach": { "executor": "nx:run-commands", "options": { - "command": "docker/openchallenges/serve-detach.sh openchallenges-apex" + "command": "docker/openchallenges/serve-detach.sh {projectName}" } }, "scan-image": { "executor": "nx:run-commands", "options": { - "command": "trivy image ghcr.io/sage-bionetworks/openchallenges-apex:local --quiet", + "command": "trivy image ghcr.io/sage-bionetworks/{projectName}:local --quiet", "color": true } } }, - "tags": ["type:service", "scope:backend"], - "implicitDependencies": [] + "tags": ["type:service", "scope:backend"] } diff --git a/apps/openchallenges/apex/templates/events.conf.template b/apps/openchallenges/apex/templates/events.conf.template deleted file mode 100644 index 6eece52865..0000000000 --- a/apps/openchallenges/apex/templates/events.conf.template +++ /dev/null @@ -1,3 +0,0 @@ -events { - worker_connections 1024; -} \ No newline at end of file diff --git a/apps/openchallenges/apex/templates/http.conf.template b/apps/openchallenges/apex/templates/http.conf.template deleted file mode 100644 index d801388df6..0000000000 --- a/apps/openchallenges/apex/templates/http.conf.template +++ /dev/null @@ -1,118 +0,0 @@ -http { - include /etc/nginx/mime.types; - default_type application/octet-stream; - - upstream api-docs { - server ${API_DOCS_HOST}:${API_DOCS_PORT}; - keepalive 15; - } - - upstream api-gateway { - server ${API_GATEWAY_HOST}:${API_GATEWAY_PORT}; - keepalive 15; - } - - upstream app { - server ${APP_HOST}:${APP_PORT}; - keepalive 15; - } - - upstream thumbor { - server ${THUMBOR_HOST}:${THUMBOR_PORT}; - keepalive 15; - } - - upstream zipkin { - server ${ZIPKIN_HOST}:${ZIPKIN_PORT}; - keepalive 15; - } - - server { - listen 8000; - server_name openchallenges-apex; - - error_log /var/log/nginx/openchallenges-apex.error.log; - access_log /var/log/nginx/openchallenges-apex.access.log; - - location / { - proxy_set_header X-Real-IP $remote_addr; - proxy_set_header X-Forwarded-For $remote_addr; - proxy_set_header Host $http_host; - - proxy_http_version 1.1; - proxy_set_header Upgrade $http_upgrade; - proxy_set_header Connection "Upgrade"; - - proxy_set_header Connection "Keep-Alive"; - proxy_set_header Proxy-Connection "Keep-Alive"; - - proxy_pass http://app; - } - - location /api-docs { - proxy_set_header X-Real-IP $remote_addr; - proxy_set_header X-Forwarded-For $remote_addr; - proxy_set_header Host $http_host; - - proxy_http_version 1.1; - proxy_set_header Upgrade $http_upgrade; - proxy_set_header Connection "Upgrade"; - - proxy_set_header Connection "Keep-Alive"; - proxy_set_header Proxy-Connection "Keep-Alive"; - - proxy_pass http://api-docs/; - } - - location /api { - proxy_set_header X-Real-IP $remote_addr; - proxy_set_header X-Forwarded-For $remote_addr; - proxy_set_header Host $http_host; - - proxy_http_version 1.1; - proxy_set_header Upgrade $http_upgrade; - proxy_set_header Connection "Upgrade"; - - proxy_set_header Connection "Keep-Alive"; - proxy_set_header Proxy-Connection "Keep-Alive"; - - proxy_pass http://api-gateway; - } - - location = /health { - access_log off; - add_header 'Content-Type' 'application/json'; - return 200 '{"status": "UP"}'; - } - - location /img/ { - proxy_set_header X-Real-IP $remote_addr; - proxy_set_header X-Forwarded-For $remote_addr; - proxy_set_header Host $http_host; - - proxy_http_version 1.1; - proxy_set_header Upgrade $http_upgrade; - proxy_set_header Connection "Upgrade"; - - proxy_set_header Connection "Keep-Alive"; - proxy_set_header Proxy-Connection "Keep-Alive"; - - proxy_pass http://thumbor/; - } - - location /zipkin { - proxy_set_header X-Real-IP $remote_addr; - proxy_set_header X-Forwarded-For $remote_addr; - proxy_set_header Host $http_host; - - proxy_http_version 1.1; - proxy_set_header Upgrade $http_upgrade; - proxy_set_header Connection "Upgrade"; - - proxy_set_header Connection "Keep-Alive"; - proxy_set_header Proxy-Connection "Keep-Alive"; - - proxy_pass http://zipkin; - } - } -} \ No newline at end of file diff --git a/docker/openchallenges/services/apex.yml b/docker/openchallenges/services/apex.yml index 5d60861fcd..9cb9d06c0c 100644 --- a/docker/openchallenges/services/apex.yml +++ b/docker/openchallenges/services/apex.yml @@ -5,13 +5,12 @@ services: restart: always env_file: - ../../../apps/openchallenges/apex/.env - # volumes: - # - ../../../apps/openchallenges/apex/nginx.conf:/etc/nginx/nginx.conf - # - ../../../apps/openchallenges/apex/templates:/etc/nginx/templates + volumes: + - ../../../apps/openchallenges/apex/Caddyfile:/etc/caddy/Caddyfile networks: - openchallenges ports: - - '8000:8000' + - '8000:80' depends_on: openchallenges-api-docs: condition: service_healthy @@ -21,7 +20,6 @@ services: condition: service_started openchallenges-zipkin: condition: service_healthy - deploy: resources: limits: From 9355b4a43fed05723f4784b9bb2cd847d059f5b9 Mon Sep 17 00:00:00 2001 From: Thomas Schaffter Date: Mon, 2 Dec 2024 23:55:40 +0000 Subject: [PATCH 2/3] Add the Caddyfile to the Docker image --- apps/openchallenges/apex/Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/openchallenges/apex/Dockerfile b/apps/openchallenges/apex/Dockerfile index fa4dba30fa..4f39245e5e 100644 --- a/apps/openchallenges/apex/Dockerfile +++ b/apps/openchallenges/apex/Dockerfile @@ -1,3 +1,3 @@ FROM caddy:2.8.4 -# COPY Caddyfile /etc/caddy/ \ No newline at end of file +COPY Caddyfile /etc/caddy/ \ No newline at end of file From dd43bf29b8423c08ad9a2e88550b955bce8772c1 Mon Sep 17 00:00:00 2001 From: Thomas Schaffter Date: Tue, 3 Dec 2024 02:31:25 +0000 Subject: [PATCH 3/3] Add HEALTHCHECK --- apps/openchallenges/apex/Caddyfile | 2 +- apps/openchallenges/apex/Dockerfile | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/apps/openchallenges/apex/Caddyfile b/apps/openchallenges/apex/Caddyfile index 56e172700d..c4275a058a 100644 --- a/apps/openchallenges/apex/Caddyfile +++ b/apps/openchallenges/apex/Caddyfile @@ -12,7 +12,7 @@ } handle_path /health { - respond `{"status":"healthy","message":"OK"}` 200 + respond `{"status":"healthy"}` 200 header Content-Type application/json } diff --git a/apps/openchallenges/apex/Dockerfile b/apps/openchallenges/apex/Dockerfile index 4f39245e5e..95819eea07 100644 --- a/apps/openchallenges/apex/Dockerfile +++ b/apps/openchallenges/apex/Dockerfile @@ -1,3 +1,8 @@ FROM caddy:2.8.4 +RUN apk add --no-cache curl jq + +HEALTHCHECK --interval=2s --timeout=3s --retries=20 --start-period=5s \ + CMD curl --fail --silent "localhost:80/health" | jq '.status' | grep healthy || exit 1 + COPY Caddyfile /etc/caddy/ \ No newline at end of file