From 2572c28648b00ce251f72a35d8a1881d3e84f6ad Mon Sep 17 00:00:00 2001 From: "stephan.kasdorf" Date: Fri, 31 May 2024 15:13:10 +0200 Subject: [PATCH] Refactored the application's dockerization and added new services This commit involves significant changes to the application's infrastructure. It refactors the application into two separate services: 'nibiru-auth-api' and 'nibiru-webhook-receiver'. Each service has its corresponding Dockerfile, Nginx & PHP-FPM configuration. It also provides a separate Nginx configuration for handling requests for each service. New environment files, start and stop scripts are introduced to facilitate local and production deployments. --- .env | 16 +++- .env-local | 14 +++ docker-compose.local.yml | 50 +++++++++++ docker-compose.yml | 86 ++++++++++++++++--- nginx/nginx.conf | 36 -------- nibiru-auth-api/fpm/Dockerfile | 38 ++++++++ .../fpm/conf/zz-docker.conf.template | 5 ++ nibiru-auth-api/fpm/entrypoint.sh | 10 +++ nibiru-auth-api/nginx/Dockerfile | 16 ++++ nibiru-auth-api/nginx/conf.d/default.conf | 40 +++++++++ .../nginx/conf.d/default.conf.template | 40 +++++++++ nibiru-auth-api/nginx/entrypoint.sh | 7 ++ nibiru-webhook-receiver/fpm/Dockerfile | 38 ++++++++ .../fpm/conf/zz-docker.conf.template | 5 ++ nibiru-webhook-receiver/fpm/entrypoint.sh | 10 +++ nibiru-webhook-receiver/nginx/Dockerfile | 16 ++++ .../nginx/conf.d/default.conf | 40 +++++++++ .../nginx/conf.d/default.conf.template | 40 +++++++++ nibiru-webhook-receiver/nginx/entrypoint.sh | 7 ++ php-fpm/Dockerfile | 21 ----- src/AuthController.php | 23 +++++ src/GitHubClient.php | 0 src/ValidateToken.php | 9 ++ src/WebhookController.php | 34 ++++++++ src/composer.json | 0 src/index.php | 2 + src/webhookReceiver.php | 0 start.sh | 26 ++++++ stop.sh | 11 +++ 29 files changed, 568 insertions(+), 72 deletions(-) create mode 100644 .env-local create mode 100644 docker-compose.local.yml delete mode 100644 nginx/nginx.conf create mode 100644 nibiru-auth-api/fpm/Dockerfile create mode 100644 nibiru-auth-api/fpm/conf/zz-docker.conf.template create mode 100755 nibiru-auth-api/fpm/entrypoint.sh create mode 100644 nibiru-auth-api/nginx/Dockerfile create mode 100644 nibiru-auth-api/nginx/conf.d/default.conf create mode 100644 nibiru-auth-api/nginx/conf.d/default.conf.template create mode 100755 nibiru-auth-api/nginx/entrypoint.sh create mode 100644 nibiru-webhook-receiver/fpm/Dockerfile create mode 100644 nibiru-webhook-receiver/fpm/conf/zz-docker.conf.template create mode 100755 nibiru-webhook-receiver/fpm/entrypoint.sh create mode 100644 nibiru-webhook-receiver/nginx/Dockerfile create mode 100644 nibiru-webhook-receiver/nginx/conf.d/default.conf create mode 100644 nibiru-webhook-receiver/nginx/conf.d/default.conf.template create mode 100755 nibiru-webhook-receiver/nginx/entrypoint.sh delete mode 100644 php-fpm/Dockerfile create mode 100644 src/AuthController.php mode change 100644 => 100755 src/GitHubClient.php create mode 100644 src/ValidateToken.php create mode 100644 src/WebhookController.php mode change 100644 => 100755 src/composer.json create mode 100755 src/index.php mode change 100644 => 100755 src/webhookReceiver.php create mode 100755 start.sh create mode 100755 stop.sh diff --git a/.env b/.env index 88469ca..2f7af5f 100644 --- a/.env +++ b/.env @@ -1,6 +1,16 @@ COMPOSE_PROJECT_NAME=nibiru-framework-api -VIRTUAL_HOST=agent.api.nibiru-framework.com -LETSENCRYPT_HOST=agent.api.nibiru-framework.com -VIRTUAL_PORT=80 +APPLICATION_ENV=production +TZ=Europe/Berlin +NGINX_PROXY_VIRTUAL_PORT=80 +FPM_VIRTUAL_PORT=9000 + +NIBIRU_WEBHOOK_HOST=nibiru-webhook-receiver +NIBIRU_WEHOOK_RECEIVER_VIRTUAL_HOST=api.agent.receiver.nibiru-framework.com +NIBIRU_WEHOOK_RECEIVER_LETSENCRYPT_HOST=api.agent.receiver.nibiru-framework.com + +NIBIRU_AUTH_HOST=nibiru-auth-api +NIBIRU_AUTH_VIRTUAL_HOST=api.agent.auth.nibiru-framework.com +NIBIRU_AUTH_LETSENCRYPT_HOST=api.agent.auth.nibiru-framework.com + PROXY_NETWORK=nginx-proxy GITHUB_TOKEN=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 \ No newline at end of file diff --git a/.env-local b/.env-local new file mode 100644 index 0000000..fd4facf --- /dev/null +++ b/.env-local @@ -0,0 +1,14 @@ +COMPOSE_PROJECT_NAME=nibiru-framework-api +APPLICATION_ENV=development +TZ=Europe/Berlin +NGINX_PROXY_VIRTUAL_PORT=80 +FPM_VIRTUAL_PORT=9000 + +NIBIRU_WEBHOOK_HOST=nibiru-webhook-receiver +NIBIRU_WEHOOK_RECEIVER_VIRTUAL_HOST=local.receiver.nibiru-framework.com + +NIBIRU_AUTH_HOST=nibiru-auth-api +NIBIRU_AUTH_VIRTUAL_HOST=local.auth.nibiru-framework.com + +PROXY_NETWORK=nginx-proxy +GITHUB_TOKEN=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 \ No newline at end of file diff --git a/docker-compose.local.yml b/docker-compose.local.yml new file mode 100644 index 0000000..26a85c0 --- /dev/null +++ b/docker-compose.local.yml @@ -0,0 +1,50 @@ +version: '3.3' + +services: + nibiru-auth-api: + env_file: + - .env-local + environment: + - APPLICATION_ENV=${APPLICATION_ENV} + - VIRTUAL_HOST=${NIBIRU_AUTH_VIRTUAL_HOST} + - VIRTUAL_PORT=${FPM_VIRTUAL_PORT} + - NIBIRU_AUTH_VIRTUAL_HOST=${NIBIRU_AUTH_VIRTUAL_HOST} + - NIBIRU_AUTH_HOST=${NIBIRU_AUTH_HOST} + - TZ=${TZ} + + nibiru-auth-api-nginx: + env_file: + - .env-local + environment: + - APPLICATION_ENV=${APPLICATION_ENV} + - VIRTUAL_HOST=${NIBIRU_AUTH_VIRTUAL_HOST} + - VIRTUAL_PORT=${NGINX_PROXY_VIRTUAL_PORT} + - NIBIRU_AUTH_VIRTUAL_HOST=${NIBIRU_AUTH_VIRTUAL_HOST} + - NIBIRU_AUTH_HOST=${NIBIRU_AUTH_HOST} + - TZ=${TZ} + + nibiru-webhook-receiver: + env_file: + - .env-local + environment: + - APPLICATION_ENV=${APPLICATION_ENV} + - VIRTUAL_HOST=${NIBIRU_WEHOOK_RECEIVER_VIRTUAL_HOST} + - VIRTUAL_PORT=${FPM_VIRTUAL_PORT} + - NIBIRU_WEHOOK_RECEIVER_VIRTUAL_HOST=${NIBIRU_WEHOOK_RECEIVER_VIRTUAL_HOST} + - NIBIRU_WEBHOOK_HOST=${NIBIRU_WEBHOOK_HOST} + - TZ=${TZ} + + nibiru-webhook-receiver-nginx: + env_file: + - .env-local + environment: + - APPLICATION_ENV=${APPLICATION_ENV} + - VIRTUAL_HOST=${NIBIRU_WEHOOK_RECEIVER_VIRTUAL_HOST} + - VIRTUAL_PORT=${NGINX_PROXY_VIRTUAL_PORT} + - TZ=${TZ} + +networks: + nginx-proxy: + external: true + api_internal: + driver: bridge \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 50df32d..b2bce65 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,30 +1,92 @@ version: '3.3' services: - php-fpm: + nibiru-auth-api: build: - context: ./php-fpm + context: ./nibiru-auth-api/fpm + env_file: + - .env + environment: + - APPLICATION_ENV=${APPLICATION_ENV} + - VIRTUAL_HOST=${NIBIRU_AUTH_VIRTUAL_HOST} + - VIRTUAL_PORT=${FPM_VIRTUAL_PORT} + - NIBIRU_AUTH_VIRTUAL_HOST=${NIBIRU_AUTH_VIRTUAL_HOST} + - NIBIRU_AUTH_HOST=${NIBIRU_AUTH_HOST} + - TZ=${TZ} volumes: - ./src:/var/www/html networks: - - nginx-proxy + api_internal: restart: always - nginx: - image: nginx:latest + nibiru-auth-api-nginx: + build: + context: ./nibiru-auth-api/nginx + links: + - nibiru-auth-api + env_file: + - .env environment: - - VIRTUAL_HOST=${VIRTUAL_HOST} - - LETSENCRYPT_HOST=${LETSENCRYPT_HOST} - - VIRTUAL_PORT=${VIRTUAL_PORT} + - APPLICATION_ENV=${APPLICATION_ENV} + - VIRTUAL_HOST=${NIBIRU_AUTH_VIRTUAL_HOST} + - VIRTUAL_PORT=${NGINX_PROXY_VIRTUAL_PORT} + - FPM_VIRTUAL_PORT=${FPM_VIRTUAL_PORT} + - NIBIRU_AUTH_VIRTUAL_HOST=${NIBIRU_AUTH_VIRTUAL_HOST} + - NIBIRU_AUTH_HOST=${NIBIRU_AUTH_HOST} + - LETSENCRYPT_HOST=${NIBIRU_AUTH_LETSENCRYPT_HOST} + - TZ=${TZ} + volumes: + - ./src:/usr/share/nginx/html + networks: + nginx-proxy: + api_internal: + depends_on: + - nibiru-auth-api + restart: always + + nibiru-webhook-receiver: + build: + context: ./nibiru-webhook-receiver/fpm + env_file: + - .env + environment: + - APPLICATION_ENV=${APPLICATION_ENV} + - VIRTUAL_HOST=${NIBIRU_WEHOOK_RECEIVER_VIRTUAL_HOST} + - VIRTUAL_PORT=${FPM_VIRTUAL_PORT} + - NIBIRU_WEHOOK_RECEIVER_VIRTUAL_HOST=${NIBIRU_WEHOOK_RECEIVER_VIRTUAL_HOST} + - NIBIRU_WEBHOOK_HOST=${NIBIRU_WEBHOOK_HOST} + - TZ=${TZ} volumes: - ./src:/var/www/html - - ./nginx/nginx.conf:/etc/nginx/conf.d/default.conf - depends_on: - - php-fpm networks: - - nginx-proxy + api_internal: + restart: always + + nibiru-webhook-receiver-nginx: + build: + context: ./nibiru-webhook-receiver/nginx + links: + - nibiru-webhook-receiver + env_file: + - .env + environment: + - APPLICATION_ENV=${APPLICATION_ENV} + - VIRTUAL_HOST=${NIBIRU_WEHOOK_RECEIVER_VIRTUAL_HOST} + - VIRTUAL_PORT=${NGINX_PROXY_VIRTUAL_PORT} + - FPM_VIRTUAL_PORT=${FPM_VIRTUAL_PORT} + - LETSENCRYPT_HOST=${NIBIRU_WEHOOK_RECEIVER_LETSENCRYPT_HOST} + - TZ=${TZ} + volumes: + - ./src:/usr/share/nginx/html + networks: + nginx-proxy: + api_internal: + depends_on: + - nibiru-auth-api restart: always networks: nginx-proxy: + external: true + api_internal: external: true \ No newline at end of file diff --git a/nginx/nginx.conf b/nginx/nginx.conf deleted file mode 100644 index 2c005d7..0000000 --- a/nginx/nginx.conf +++ /dev/null @@ -1,36 +0,0 @@ -server { - listen 80; - server_name ${VIRTUAL_HOST}; - - gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss; - - location / { - proxy_read_timeout 7200; - proxy_connect_timeout 7200; - if (!-e $request_filename){ - rewrite ^(.*)$ / break; - } - root /usr/share/nginx/html; - index index.php; - } - - location ~ \.php$ { - root /var/www/html; - fastcgi_pass php-fpm:9000; - fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name; - include fastcgi_params; - fastcgi_buffers 16 32k; - fastcgi_buffer_size 64k; - fastcgi_busy_buffers_size 64k; - fastcgi_read_timeout 900; - } - - error_page 500 502 503 504 /50x.html; - location = /50x.html { - root /usr/share/nginx/html; - } - - location ~ /\.ht { - deny all; - } -} \ No newline at end of file diff --git a/nibiru-auth-api/fpm/Dockerfile b/nibiru-auth-api/fpm/Dockerfile new file mode 100644 index 0000000..d99d26a --- /dev/null +++ b/nibiru-auth-api/fpm/Dockerfile @@ -0,0 +1,38 @@ +# Use the official PHP-FPM image for PHP 8.3 +FROM php:8.3-fpm + +# Import the timezone and virtual port arguments +ARG TZ +ARG FPM_VIRTUAL_PORT + +# Set the working directory +WORKDIR /var/www/html + +# Install Composer +COPY --from=composer:latest /usr/bin/composer /usr/bin/composer + +# Install system dependencies +RUN apt-get update && apt-get install -y \ + zip \ + unzip \ + git \ + gettext-base \ + && apt-get clean + +# Set the timezone +ENV TZ=$TZ +RUN echo "date.timezone=${TZ}" > /usr/local/etc/php/conf.d/timezone.ini + + +# Copy entrypoint script and PHP-FPM configuration template +COPY entrypoint.sh /usr/local/bin/entrypoint.sh +COPY conf/zz-docker.conf.template /usr/local/etc/php-fpm.d/zz-docker.conf.template + +# Make entrypoint script executable +RUN chmod +x /usr/local/bin/entrypoint.sh + +# Expose port 9000 for PHP-FPM +EXPOSE $FPM_VIRTUAL_PORT + +# Set entrypoint +ENTRYPOINT ["/usr/local/bin/entrypoint.sh"] \ No newline at end of file diff --git a/nibiru-auth-api/fpm/conf/zz-docker.conf.template b/nibiru-auth-api/fpm/conf/zz-docker.conf.template new file mode 100644 index 0000000..d637737 --- /dev/null +++ b/nibiru-auth-api/fpm/conf/zz-docker.conf.template @@ -0,0 +1,5 @@ +[global] +daemonize = no + +[www] +listen = 0.0.0.0:$FPM_VIRTUAL_PORT \ No newline at end of file diff --git a/nibiru-auth-api/fpm/entrypoint.sh b/nibiru-auth-api/fpm/entrypoint.sh new file mode 100755 index 0000000..566aeab --- /dev/null +++ b/nibiru-auth-api/fpm/entrypoint.sh @@ -0,0 +1,10 @@ +#!/bin/sh + +# Set the PHP-FPM listen port from the environment variable or default to 9000 +export FPM_VIRTUAL_PORT=${FPM_VIRTUAL_PORT:-9000} + +# Substitute environment variables in the PHP-FPM configuration template +envsubst '$FPM_VIRTUAL_PORT' < /usr/local/etc/php-fpm.d/zz-docker.conf.template > /usr/local/etc/php-fpm.d/zz-docker.conf + +# Start PHP-FPM +php-fpm \ No newline at end of file diff --git a/nibiru-auth-api/nginx/Dockerfile b/nibiru-auth-api/nginx/Dockerfile new file mode 100644 index 0000000..0c8bae6 --- /dev/null +++ b/nibiru-auth-api/nginx/Dockerfile @@ -0,0 +1,16 @@ +FROM nginx:latest + +# Set the timezone +ENV TZ=Europe/Berlin + +# Install tzdata for timezone data and gettext for envsubst +RUN apt-get update && apt-get install -y tzdata gettext && \ + ln -fs /usr/share/zoneinfo/$TZ /etc/localtime && \ + dpkg-reconfigure -f noninteractive tzdata + +COPY conf.d /etc/nginx/conf.d +COPY entrypoint.sh /entrypoint.sh +RUN chmod +x /entrypoint.sh + +ENTRYPOINT ["/entrypoint.sh"] +CMD ["nginx", "-g", "daemon off;"] \ No newline at end of file diff --git a/nibiru-auth-api/nginx/conf.d/default.conf b/nibiru-auth-api/nginx/conf.d/default.conf new file mode 100644 index 0000000..007b89c --- /dev/null +++ b/nibiru-auth-api/nginx/conf.d/default.conf @@ -0,0 +1,40 @@ +server { + listen 80; + server_name ${NIBIRU_AUTH_VIRTUAL_HOST}; + + gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss; + + location / { + proxy_read_timeout 7200; + proxy_connect_timeout 7200; + if (!-e $request_filename){ + rewrite ^(.*)$ / break; + } + root /usr/share/nginx/html; + index index.php; + } + + location ~ \.php$ { + root /var/www/html; + fastcgi_pass ${NIBIRU_AUTH_HOST}:${FPM_VIRTUAL_PORT}; + fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + include fastcgi_params; + fastcgi_buffers 16 32k; + fastcgi_buffer_size 64k; + fastcgi_busy_buffers_size 64k; + fastcgi_read_timeout 900; + } + + error_page 500 502 503 504 /50x.html; + location = /50x.html { + root /usr/share/nginx/html; + } + + location ~ /\.ht { + deny all; + } +} \ No newline at end of file diff --git a/nibiru-auth-api/nginx/conf.d/default.conf.template b/nibiru-auth-api/nginx/conf.d/default.conf.template new file mode 100644 index 0000000..007b89c --- /dev/null +++ b/nibiru-auth-api/nginx/conf.d/default.conf.template @@ -0,0 +1,40 @@ +server { + listen 80; + server_name ${NIBIRU_AUTH_VIRTUAL_HOST}; + + gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss; + + location / { + proxy_read_timeout 7200; + proxy_connect_timeout 7200; + if (!-e $request_filename){ + rewrite ^(.*)$ / break; + } + root /usr/share/nginx/html; + index index.php; + } + + location ~ \.php$ { + root /var/www/html; + fastcgi_pass ${NIBIRU_AUTH_HOST}:${FPM_VIRTUAL_PORT}; + fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + include fastcgi_params; + fastcgi_buffers 16 32k; + fastcgi_buffer_size 64k; + fastcgi_busy_buffers_size 64k; + fastcgi_read_timeout 900; + } + + error_page 500 502 503 504 /50x.html; + location = /50x.html { + root /usr/share/nginx/html; + } + + location ~ /\.ht { + deny all; + } +} \ No newline at end of file diff --git a/nibiru-auth-api/nginx/entrypoint.sh b/nibiru-auth-api/nginx/entrypoint.sh new file mode 100755 index 0000000..096e8dd --- /dev/null +++ b/nibiru-auth-api/nginx/entrypoint.sh @@ -0,0 +1,7 @@ +#!/bin/sh + +# Substitute environment variables in the configuration templates +envsubst '$NIBIRU_AUTH_HOST $NIBIRU_AUTH_VIRTUAL_HOST $FPM_VIRTUAL_PORT' < /etc/nginx/conf.d/default.conf.template > /etc/nginx/conf.d/default.conf + +# Start Nginx +exec "$@" \ No newline at end of file diff --git a/nibiru-webhook-receiver/fpm/Dockerfile b/nibiru-webhook-receiver/fpm/Dockerfile new file mode 100644 index 0000000..d99d26a --- /dev/null +++ b/nibiru-webhook-receiver/fpm/Dockerfile @@ -0,0 +1,38 @@ +# Use the official PHP-FPM image for PHP 8.3 +FROM php:8.3-fpm + +# Import the timezone and virtual port arguments +ARG TZ +ARG FPM_VIRTUAL_PORT + +# Set the working directory +WORKDIR /var/www/html + +# Install Composer +COPY --from=composer:latest /usr/bin/composer /usr/bin/composer + +# Install system dependencies +RUN apt-get update && apt-get install -y \ + zip \ + unzip \ + git \ + gettext-base \ + && apt-get clean + +# Set the timezone +ENV TZ=$TZ +RUN echo "date.timezone=${TZ}" > /usr/local/etc/php/conf.d/timezone.ini + + +# Copy entrypoint script and PHP-FPM configuration template +COPY entrypoint.sh /usr/local/bin/entrypoint.sh +COPY conf/zz-docker.conf.template /usr/local/etc/php-fpm.d/zz-docker.conf.template + +# Make entrypoint script executable +RUN chmod +x /usr/local/bin/entrypoint.sh + +# Expose port 9000 for PHP-FPM +EXPOSE $FPM_VIRTUAL_PORT + +# Set entrypoint +ENTRYPOINT ["/usr/local/bin/entrypoint.sh"] \ No newline at end of file diff --git a/nibiru-webhook-receiver/fpm/conf/zz-docker.conf.template b/nibiru-webhook-receiver/fpm/conf/zz-docker.conf.template new file mode 100644 index 0000000..d637737 --- /dev/null +++ b/nibiru-webhook-receiver/fpm/conf/zz-docker.conf.template @@ -0,0 +1,5 @@ +[global] +daemonize = no + +[www] +listen = 0.0.0.0:$FPM_VIRTUAL_PORT \ No newline at end of file diff --git a/nibiru-webhook-receiver/fpm/entrypoint.sh b/nibiru-webhook-receiver/fpm/entrypoint.sh new file mode 100755 index 0000000..566aeab --- /dev/null +++ b/nibiru-webhook-receiver/fpm/entrypoint.sh @@ -0,0 +1,10 @@ +#!/bin/sh + +# Set the PHP-FPM listen port from the environment variable or default to 9000 +export FPM_VIRTUAL_PORT=${FPM_VIRTUAL_PORT:-9000} + +# Substitute environment variables in the PHP-FPM configuration template +envsubst '$FPM_VIRTUAL_PORT' < /usr/local/etc/php-fpm.d/zz-docker.conf.template > /usr/local/etc/php-fpm.d/zz-docker.conf + +# Start PHP-FPM +php-fpm \ No newline at end of file diff --git a/nibiru-webhook-receiver/nginx/Dockerfile b/nibiru-webhook-receiver/nginx/Dockerfile new file mode 100644 index 0000000..0c8bae6 --- /dev/null +++ b/nibiru-webhook-receiver/nginx/Dockerfile @@ -0,0 +1,16 @@ +FROM nginx:latest + +# Set the timezone +ENV TZ=Europe/Berlin + +# Install tzdata for timezone data and gettext for envsubst +RUN apt-get update && apt-get install -y tzdata gettext && \ + ln -fs /usr/share/zoneinfo/$TZ /etc/localtime && \ + dpkg-reconfigure -f noninteractive tzdata + +COPY conf.d /etc/nginx/conf.d +COPY entrypoint.sh /entrypoint.sh +RUN chmod +x /entrypoint.sh + +ENTRYPOINT ["/entrypoint.sh"] +CMD ["nginx", "-g", "daemon off;"] \ No newline at end of file diff --git a/nibiru-webhook-receiver/nginx/conf.d/default.conf b/nibiru-webhook-receiver/nginx/conf.d/default.conf new file mode 100644 index 0000000..42e9ea7 --- /dev/null +++ b/nibiru-webhook-receiver/nginx/conf.d/default.conf @@ -0,0 +1,40 @@ +server { + listen 80; + server_name ${NIBIRU_WEHOOK_RECEIVER_VIRTUAL_HOST}; + + gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss; + + location / { + proxy_read_timeout 7200; + proxy_connect_timeout 7200; + if (!-e $request_filename){ + rewrite ^(.*)$ / break; + } + root /usr/share/nginx/html; + index index.php; + } + + location ~ \.php$ { + root /var/www/html; + fastcgi_pass ${NIBIRU_WEBHOOK_HOST}:${FPM_VIRTUAL_PORT}; + fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + include fastcgi_params; + fastcgi_buffers 16 32k; + fastcgi_buffer_size 64k; + fastcgi_busy_buffers_size 64k; + fastcgi_read_timeout 900; + } + + error_page 500 502 503 504 /50x.html; + location = /50x.html { + root /usr/share/nginx/html; + } + + location ~ /\.ht { + deny all; + } +} \ No newline at end of file diff --git a/nibiru-webhook-receiver/nginx/conf.d/default.conf.template b/nibiru-webhook-receiver/nginx/conf.d/default.conf.template new file mode 100644 index 0000000..42e9ea7 --- /dev/null +++ b/nibiru-webhook-receiver/nginx/conf.d/default.conf.template @@ -0,0 +1,40 @@ +server { + listen 80; + server_name ${NIBIRU_WEHOOK_RECEIVER_VIRTUAL_HOST}; + + gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss; + + location / { + proxy_read_timeout 7200; + proxy_connect_timeout 7200; + if (!-e $request_filename){ + rewrite ^(.*)$ / break; + } + root /usr/share/nginx/html; + index index.php; + } + + location ~ \.php$ { + root /var/www/html; + fastcgi_pass ${NIBIRU_WEBHOOK_HOST}:${FPM_VIRTUAL_PORT}; + fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + include fastcgi_params; + fastcgi_buffers 16 32k; + fastcgi_buffer_size 64k; + fastcgi_busy_buffers_size 64k; + fastcgi_read_timeout 900; + } + + error_page 500 502 503 504 /50x.html; + location = /50x.html { + root /usr/share/nginx/html; + } + + location ~ /\.ht { + deny all; + } +} \ No newline at end of file diff --git a/nibiru-webhook-receiver/nginx/entrypoint.sh b/nibiru-webhook-receiver/nginx/entrypoint.sh new file mode 100755 index 0000000..aa9893e --- /dev/null +++ b/nibiru-webhook-receiver/nginx/entrypoint.sh @@ -0,0 +1,7 @@ +#!/bin/sh + +# Substitute environment variables in the configuration templates +envsubst '$NIBIRU_WEBHOOK_HOST $NIBIRU_WEHOOK_RECEIVER_VIRTUAL_HOST $FPM_VIRTUAL_PORT' < /etc/nginx/conf.d/default.conf.template > /etc/nginx/conf.d/default.conf + +# Start Nginx +exec "$@" \ No newline at end of file diff --git a/php-fpm/Dockerfile b/php-fpm/Dockerfile deleted file mode 100644 index ffdb600..0000000 --- a/php-fpm/Dockerfile +++ /dev/null @@ -1,21 +0,0 @@ -# Use the official PHP-FPM image for PHP 8.3 -FROM php:8.3-fpm - -# Set the working directory -WORKDIR /var/www/html - -# Install Composer -COPY --from=composer:latest /usr/bin/composer /usr/bin/composer - -# Install system dependencies -RUN apt-get update && apt-get install -y \ - zip \ - unzip \ - git \ - && apt-get clean - -# Expose port 9000 for PHP-FPM -EXPOSE 9000 - -# Start PHP-FPM -CMD ["php-fpm"] \ No newline at end of file diff --git a/src/AuthController.php b/src/AuthController.php new file mode 100644 index 0000000..9a20320 --- /dev/null +++ b/src/AuthController.php @@ -0,0 +1,23 @@ + $token]); + } else { + http_response_code(401); + echo json_encode(['error' => 'Invalid credentials']); + } + } + + public function validateToken($token) { + // Validate the token (this is a simple example, implement proper token validation) + return strlen($token) === 32; + } +} \ No newline at end of file diff --git a/src/GitHubClient.php b/src/GitHubClient.php old mode 100644 new mode 100755 diff --git a/src/ValidateToken.php b/src/ValidateToken.php new file mode 100644 index 0000000..c4d09d2 --- /dev/null +++ b/src/ValidateToken.php @@ -0,0 +1,9 @@ +validateToken($token); +echo json_encode(['valid' => $isValid]); \ No newline at end of file diff --git a/src/WebhookController.php b/src/WebhookController.php new file mode 100644 index 0000000..4e7ac82 --- /dev/null +++ b/src/WebhookController.php @@ -0,0 +1,34 @@ +authServiceUrl = 'http://nibiru-auth-api:9000/validateToken.php'; + } + + private function validateToken($token) { + // Make a request to the auth service to validate the token + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $this->authServiceUrl); + curl_setopt($ch, CURLOPT_POST, 1); + curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(['token' => $token])); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + $response = curl_exec($ch); + curl_close($ch); + + $result = json_decode($response, true); + return $result && isset($result['valid']) && $result['valid']; + } + + public function handleWebhook() { + $headers = getallheaders(); + $token = $headers['Authorization'] ?? ''; + + if (!$this->validateToken($token)) { + http_response_code(401); + echo json_encode(['error' => 'Unauthorized']); + return; + } + + // Handle the webhook logic here + } +} \ No newline at end of file diff --git a/src/composer.json b/src/composer.json old mode 100644 new mode 100755 diff --git a/src/index.php b/src/index.php new file mode 100755 index 0000000..bfd863b --- /dev/null +++ b/src/index.php @@ -0,0 +1,2 @@ +