Skip to content

Commit

Permalink
the bot also runs in a container
Browse files Browse the repository at this point in the history
  • Loading branch information
2pipopolam committed Jan 12, 2025
1 parent a11d952 commit 773916c
Show file tree
Hide file tree
Showing 9 changed files with 364 additions and 413 deletions.
19 changes: 10 additions & 9 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
FROM python:3.11-slim

ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
ENV NODE_VERSION 20.x
ENV DEBIAN_FRONTEND=noninteractive
ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1 \
NODE_VERSION=20.x \
DEBIAN_FRONTEND=noninteractive \
TZ=Europe/Lisbon

WORKDIR /app

Expand All @@ -15,25 +16,25 @@ RUN apt-get update && apt-get install -y \
gnupg \
bash \
git \
netcat-traditional \
&& curl -fsSL https://deb.nodesource.com/setup_${NODE_VERSION} | bash - \
&& apt-get install -y nodejs \
&& npm install -g npm@latest \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*

RUN mkdir -p /app/media /app/static /app/frontend \
&& chmod -R 755 /app/media /app/static
RUN mkdir -p /app/media /app/static /app/frontend /app/bot_data \
&& chmod -R 755 /app/media /app/static /app/bot_data

COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

COPY . .

COPY frontend/package*.json ./frontend/
WORKDIR /app/frontend
COPY frontend/package*.json ./
RUN npm install

WORKDIR /app
COPY . .

COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh && \
Expand Down
8 changes: 8 additions & 0 deletions bot_entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

# create bot data directory
mkdir -p /app/bot_data
chmod -R 755 /app/bot_data

# start the bot
exec python /app/reminder_tg_bot/reminder_bot.py
1 change: 1 addition & 0 deletions chat_ids.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
95 changes: 80 additions & 15 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,36 +1,46 @@
version: '3.8'
version: "3.8"

services:
db:
image: postgres:15
image: postgres:15-alpine
volumes:
- postgres_data:/var/lib/postgresql/data
environment:
- POSTGRES_DB=${DB_NAME:-pampdb}
- POSTGRES_USER=${DB_USER:-postgres}
- POSTGRES_PASSWORD=${DB_PASSWORD:-123456}
healthcheck:
test: ["CMD-SHELL", "pg_isready -U ${DB_USER:-postgres} -d ${DB_NAME:-pampdb}"]
interval: 5s
test:
[
"CMD-SHELL",
"pg_isready -U ${DB_USER:-postgres} -d ${DB_NAME:-pampdb}",
]
interval: 10s
timeout: 5s
retries: 5
start_period: 10s
networks:
- app_network
ports:
- "5432:5432"

web:
build: .
build:
context: .
dockerfile: Dockerfile
volumes:
- .:/app
- static_volume:/app/static
- media_volume:/app/media
- frontend_build:/app/frontend/build
- /app/frontend/node_modules
ports:
- "8000:8000"
- "3000:3000"
environment:
- DEBUG=${DEBUG:-1}
- SECRET_KEY=${SECRET_KEY:-your-secret-key-123}
- DJANGO_ALLOWED_HOSTS=${DJANGO_ALLOWED_HOSTS:-localhost 127.0.0.1 [::1]}
- DJANGO_ALLOWED_HOSTS=localhost 127.0.0.1 [::1] web web:8000 web:3000
- DB_NAME=${DB_NAME:-pampdb}
- DB_USER=${DB_USER:-postgres}
- DB_PASSWORD=${DB_PASSWORD:-123456}
Expand All @@ -40,28 +50,83 @@ services:
- GOOGLE_OAUTH2_SECRET=${GOOGLE_OAUTH2_SECRET}
- REACT_APP_API_URL=http://localhost:8000/api
- NODE_ENV=development
- STATIC_ROOT=/app/static
- MEDIA_ROOT=/app/media
- SERVICE_NAME=web
- PORT=3000
- WDS_SOCKET_PORT=3000
- CHOKIDAR_USEPOLLING=true
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8000/api/health/"]
interval: 30s
timeout: 10s
retries: 3
start_period: 40s
depends_on:
db:
condition: service_healthy
networks:
- app_network
restart: unless-stopped

bot:
build:
context: .
dockerfile: Dockerfile
volumes:
- .:/app
- bot_data:/app/bot_data
entrypoint: /app/bot_entrypoint.sh
environment:
- TELEGRAM_BOT_TOKEN=${TELEGRAM_BOT_TOKEN}
- BOT_API_KEY=${BOT_API_KEY}
- API_BASE_URL=http://web:8000
- TIMEZONE=Europe/Lisbon
- CHAT_IDS_FILE=/app/bot_data/chat_ids.json
- PYTHONPATH=/app
- SERVICE_NAME=bot
- HEADERS_HOST=web
depends_on:
web:
condition: service_healthy
networks:
- app_network
restart: unless-stopped

nginx:
image: nginx:alpine
ports:
- "8080:80"
image: nginx:1.25-alpine
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf:ro
- static_volume:/app/static
- media_volume:/app/media
- frontend_build:/app/frontend/build
- static_volume:/app/static:ro
- media_volume:/app/media:ro
- frontend_build:/app/frontend/build:ro
ports:
- "8080:8080"
environment:
- NGINX_PORT=8080
depends_on:
- web
web:
condition: service_healthy
networks:
- app_network
restart: unless-stopped
healthcheck:
test: ["CMD", "nginx", "-t"]
interval: 30s
timeout: 10s
retries: 3

networks:
app_network:
name: pamp_network
driver: bridge

volumes:
postgres_data:
name: pamp_postgres_data
static_volume:
name: pamp_static_volume
media_volume:
name: pamp_media_volume
frontend_build:
name: pamp_frontend_build
bot_data:
name: pamp_bot_data
46 changes: 25 additions & 21 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,31 +1,35 @@
#!/bin/bash
set -e

echo "Checking installed Python packages..."
pip freeze

echo "Waiting for PostgreSQL..."
while ! pg_isready -h $DB_HOST -p $DB_PORT -U $DB_USER; do
echo "PostgreSQL is unavailable - sleeping"
sleep 1
# wait for database
while ! nc -z $DB_HOST $DB_PORT; do
echo "Waiting for database..."
sleep 1
done
echo "PostgreSQL is up and running!"

echo "Building React application..."
cd frontend
npm install
npm run build
cd ..
echo "Database is available"

mkdir -p /app/media
mkdir -p /app/static
mkdir -p /app/frontend/build/static

chown -R www-data:www-data /app/media /app/static
chmod -R 755 /app/media /app/static

echo "Running migrations..."
python manage.py migrate

echo "Collecting static files..."
python manage.py collectstatic --noinput
# frontend dependencies and start development server
if [ "$SERVICE_NAME" = "web" ]; then
echo "Setting up frontend..."
cd /app/frontend
npm install
# start react development server in the background
npm start &
cd /app
fi

echo "Starting Django server..."
python manage.py runserver 0.0.0.0:8000 &
# collect static files
python manage.py collectstatic --noinput

echo "Starting React development server..."
cd frontend
npm start
# start django development server
python manage.py runserver 0.0.0.0:8000
53 changes: 20 additions & 33 deletions nginx.conf
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
worker_processes auto;

events {
worker_connections 1024;
}
Expand All @@ -6,37 +8,25 @@ http {
include /etc/nginx/mime.types;
default_type application/octet-stream;

sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;

# Gzip configuration
gzip on;
gzip_disable "msie6";
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

upstream django {
server web:8000;
}

upstream react {
server web:3000;
}

server {
listen 80;
listen 8080;
server_name localhost;

# Logging configuration
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;

location /api/ {
proxy_pass http://django;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
}

location /admin/ {
proxy_pass http://django;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
client_max_body_size 100M;

location /static/ {
alias /app/static/;
Expand All @@ -51,18 +41,15 @@ http {
}

location / {
proxy_pass http://react;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_pass http://django;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;

# Additional WebSocket settings
proxy_read_timeout 86400;
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;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_redirect off;
proxy_buffering off;
}
}
}
10 changes: 7 additions & 3 deletions pampApp/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

DEBUG = True

ALLOWED_HOSTS = ['pumpapp.com', 'localhost', '0.0.0.0', '127.0.0.1']
ALLOWED_HOSTS = ['pumpapp.com', 'localhost', '0.0.0.0', '127.0.0.1', '[::1]', 'web', 'web:8000', 'web:3000']

# CORS settings
CORS_ALLOWED_ORIGINS = [
Expand All @@ -56,6 +56,8 @@
"http://127.0.0.1:8000",
"http://127.0.0.1:8080",
"http://pumpapp.com:3000",
"http://web:8000",
"http://web:3000"
]

CORS_ALLOW_CREDENTIALS = True
Expand All @@ -65,6 +67,8 @@
'http://127.0.0.1:3000',
'http://localhost:8000',
'http://localhost:8080',
'http://web:8000',
"http://web:3000"
]

CORS_ALLOW_HEADERS = list(default_headers) + [
Expand Down Expand Up @@ -146,7 +150,7 @@
'django.contrib.staticfiles',
'django.contrib.postgres',
'django.contrib.sites',

# Third party apps
'rest_framework',
'corsheaders',
Expand All @@ -159,7 +163,7 @@
'dj_rest_auth.registration',
'allauth.socialaccount',
'allauth.socialaccount.providers.google',

# Local apps
'pamp_app',
]
Expand Down
Loading

0 comments on commit 773916c

Please sign in to comment.