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.
This commit is contained in:
stephan.kasdorf
2024-05-31 15:13:10 +02:00
parent 7e860b019a
commit 2572c28648
29 changed files with 568 additions and 72 deletions

16
.env
View File

@@ -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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

14
.env-local Normal file
View File

@@ -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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

50
docker-compose.local.yml Normal file
View File

@@ -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

View File

@@ -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

View File

@@ -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;
}
}

View File

@@ -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"]

View File

@@ -0,0 +1,5 @@
[global]
daemonize = no
[www]
listen = 0.0.0.0:$FPM_VIRTUAL_PORT

View File

@@ -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

View File

@@ -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;"]

View File

@@ -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;
}
}

View File

@@ -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;
}
}

View File

@@ -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 "$@"

View File

@@ -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"]

View File

@@ -0,0 +1,5 @@
[global]
daemonize = no
[www]
listen = 0.0.0.0:$FPM_VIRTUAL_PORT

View File

@@ -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

View File

@@ -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;"]

View File

@@ -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;
}
}

View File

@@ -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;
}
}

View File

@@ -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 "$@"

View File

@@ -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"]

23
src/AuthController.php Normal file
View File

@@ -0,0 +1,23 @@
<?php
class AuthController {
public function login() {
// Handle login logic, validate user credentials, generate token
$username = $_POST['username'];
$password = $_POST['password'];
// Validate credentials (this is just an example, use proper validation)
if ($username === 'user' && $password === 'password') {
$token = bin2hex(random_bytes(16)); // Generate a random token
echo json_encode(['token' => $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;
}
}

0
src/GitHubClient.php Normal file → Executable file
View File

9
src/ValidateToken.php Normal file
View File

@@ -0,0 +1,9 @@
<?php
require_once 'AuthController.php';
$authController = new AuthController();
$token = $_POST['token'] ?? '';
$isValid = $authController->validateToken($token);
echo json_encode(['valid' => $isValid]);

34
src/WebhookController.php Normal file
View File

@@ -0,0 +1,34 @@
<?php
class WebhookController {
public function __construct() {
$this->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
}
}

0
src/composer.json Normal file → Executable file
View File

2
src/index.php Executable file
View File

@@ -0,0 +1,2 @@
<?php
phpinfo();

0
src/webhookReceiver.php Normal file → Executable file
View File

26
start.sh Executable file
View File

@@ -0,0 +1,26 @@
#!/bin/bash
# Check for the environment parameter
if [ "$1" = "local" ]; then
# Use the .env-local file for local development
ENV_FILE=".env-local"
# Export the variables from the specified environment file
export $(grep -v '^#' $ENV_FILE | xargs)
# Use the specified environment file with docker-compose
COMPOSE_FILES="-f docker-compose.yml -f docker-compose.local.yml"
else
# Use the .env file for production
ENV_FILE=".env"
# Export the variables from the specified environment file
export $(grep -v '^#' $ENV_FILE | xargs)
# Use the specified environment file with docker-compose
COMPOSE_FILES="-f docker-compose.yml"
fi
# Start Docker Compose with the specified configuration files
docker-compose $COMPOSE_FILES build --no-cache
docker-compose $COMPOSE_FILES up -d --build

11
stop.sh Executable file
View File

@@ -0,0 +1,11 @@
#!/bin/bash
# Check for the environment parameter
if [ "$1" = "local" ]; then
COMPOSE_FILES="-f docker-compose.yml -f docker-compose.local.yml"
else
COMPOSE_FILES="-f docker-compose.yml"
fi
# Stop and remove the Docker Compose services
docker-compose $COMPOSE_FILES down