From c67d918b354c286a5c6582f70e3e066347242d9e Mon Sep 17 00:00:00 2001 From: Barden Date: Sun, 22 Feb 2026 19:46:06 +0300 Subject: [PATCH] Fixed many issues --- .env.example | 10 +++++++ .../{first_pipeline.yml => main.yml} | 9 +++--- Dockerfile | 5 ++++ README.md | 8 ++--- docker-compose.override.yml | 22 ++++++++++++++ docker-compose.yml | 8 +++-- entrypoint.sh | 9 ++++++ nginx/Dockerfile | 3 -- nginx/{default.conf => default.conf.template} | 8 ++--- nginx/local.conf | 30 +++++++++++++++++++ 10 files changed, 94 insertions(+), 18 deletions(-) create mode 100644 .env.example rename .github/workflows/{first_pipeline.yml => main.yml} (92%) create mode 100644 docker-compose.override.yml create mode 100644 entrypoint.sh delete mode 100644 nginx/Dockerfile rename nginx/{default.conf => default.conf.template} (76%) create mode 100644 nginx/local.conf diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..197fbd8 --- /dev/null +++ b/.env.example @@ -0,0 +1,10 @@ +POSTGRES_USER= +POSTGRES_PASSWORD= +POSTGRES_DB= +DATABASE_URL= + +REDIS_PASSWORD= +REDIS_DB= +REDIS_URL= + +DOMAIN=localhost \ No newline at end of file diff --git a/.github/workflows/first_pipeline.yml b/.github/workflows/main.yml similarity index 92% rename from .github/workflows/first_pipeline.yml rename to .github/workflows/main.yml index d6f9f6a..1bac9d0 100644 --- a/.github/workflows/first_pipeline.yml +++ b/.github/workflows/main.yml @@ -71,6 +71,7 @@ jobs: REDIS_PASSWORD: ${{ secrets.REDIS_PASSWORD }} REDIS_DB: ${{ secrets.REDIS_DB }} REDIS_URL: ${{ secrets.REDIS_URL }} + DOMAIN: ${{ secrets.DOMAIN }} run: | touch .env @@ -81,19 +82,17 @@ jobs: echo "REDIS_PASSWORD=$REDIS_PASSWORD" >> .env echo "REDIS_DB=$REDIS_DB" >> .env echo "REDIS_URL=$REDIS_URL" >> .env + echo "DOMAIN=$DOMAIN" >> .env - name: Stops all containers - run: docker compose down + run: docker compose -f docker-compose.yml --profile prod down - name: Build docker compose - run: docker compose up --build -d + run: docker compose -f docker-compose.yml --profile prod up --build -d - name: Wait for loading database run: sleep 10 - - name: Update alembic head - run: docker compose exec web alembic upgrade head - - name: Cleanup workspace if: always() run: rm -rf .env \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 954dda7..1f2aed6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -23,4 +23,9 @@ COPY . . ENV PATH="/opt/venv/bin:$PATH" +COPY entrypoint.sh /entrypoint.sh +RUN chmod +x /entrypoint.sh + +ENTRYPOINT ["/entrypoint.sh"] + CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"] \ No newline at end of file diff --git a/README.md b/README.md index 46ce47e..b63145f 100644 --- a/README.md +++ b/README.md @@ -18,19 +18,19 @@ Instead of just building a basic CRUD app, this project focuses on production-li 1. **Clone the repository:** ```bash - git clone + git clone https://github.com/Barden-dev/URLShortener.git cd URLShortener ``` 2. **Environment Variables:** - Rename `.env.example` to `.env` and fill in your local credentials for Postgres and Redis. + Rename `.env.example` to `.env` and fill in your local credentials for Postgres and Redis. By default the domain is `localhost`, you can change it for example to `example.com` 3. **Run with Docker:** ```bash - docker-compose up -d --build + docker compose up -d --build ``` -* The API will be available at `http://localhost`. The Grafana dashboard is routed through Nginx at `/grafana/`. +* The API will be available at `http://localhost:8080`. The Grafana dashboard is routed through Nginx at `/grafana/`. ## 🔌 API Usage diff --git a/docker-compose.override.yml b/docker-compose.override.yml new file mode 100644 index 0000000..b2b62d9 --- /dev/null +++ b/docker-compose.override.yml @@ -0,0 +1,22 @@ +services: + web: + ports: + - "8000:8000" + + nginx: + ports: + - "8080:80" + volumes: + - ./nginx/local.conf:/etc/nginx/templates/default.conf.template:ro + + db: + ports: + - "5432:5432" + + redis: + ports: + - "6379:6379" + + grafana: + environment: + GF_SERVER_ROOT_URL: "http://localhost:8080/grafana/" \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index e26655d..e620cd7 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -39,13 +39,16 @@ services: retries: 5 nginx: - build: ./nginx + image: nginx:alpine ports: - "80:80" - "443:443" + environment: + - DOMAIN=${DOMAIN} depends_on: - web volumes: + - ./nginx/default.conf.template:/etc/nginx/templates/default.conf.template:ro - certbot_conf:/etc/letsencrypt - certbot_www:/var/www/certbot @@ -58,13 +61,14 @@ services: grafana: image: grafana/grafana environment: - GF_SERVER_ROOT_URL: "https://bdnsrvepic.duckdns.org/grafana/" + GF_SERVER_ROOT_URL: "https://${DOMAIN}/grafana/" GF_SERVER_SERVE_FROM_SUB_PATH: true volumes: - grafana_data:/var/lib/grafana certbot: image: certbot/certbot:latest + profiles: ["prod"] volumes: - certbot_conf:/etc/letsencrypt - certbot_www:/var/www/certbot diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 0000000..a684a59 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,9 @@ +#!/bin/sh + +echo "Database migration started" + +alembic upgrade head + +echo "Database migration ended" + +exec "$@" \ No newline at end of file diff --git a/nginx/Dockerfile b/nginx/Dockerfile deleted file mode 100644 index 79de431..0000000 --- a/nginx/Dockerfile +++ /dev/null @@ -1,3 +0,0 @@ -FROM nginx:alpine - -COPY default.conf /etc/nginx/conf.d/default.conf \ No newline at end of file diff --git a/nginx/default.conf b/nginx/default.conf.template similarity index 76% rename from nginx/default.conf rename to nginx/default.conf.template index 4ffdadb..8fec568 100644 --- a/nginx/default.conf +++ b/nginx/default.conf.template @@ -7,7 +7,7 @@ upstream grafana { server { listen 80; - server_name bdnsrvepic.duckdns.org; + server_name ${DOMAIN}; location / { return 301 https://$host$request_uri; } @@ -19,10 +19,10 @@ server { server{ listen 443 ssl; - server_name bdnsrvepic.duckdns.org; + server_name ${DOMAIN}; - ssl_certificate /etc/letsencrypt/live/bdnsrvepic.duckdns.org/fullchain.pem; - ssl_certificate_key /etc/letsencrypt/live/bdnsrvepic.duckdns.org/privkey.pem; + ssl_certificate /etc/letsencrypt/live/${DOMAIN}/fullchain.pem; + ssl_certificate_key /etc/letsencrypt/live/${DOMAIN}/privkey.pem; location / { proxy_set_header Host $http_host; diff --git a/nginx/local.conf b/nginx/local.conf new file mode 100644 index 0000000..a08cd08 --- /dev/null +++ b/nginx/local.conf @@ -0,0 +1,30 @@ +upstream backend { + server web:8000; +} + +upstream grafana { + server grafana:3000; +} + +server { + listen 80; + server_name localhost; + + location / { + proxy_set_header Host $http_host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_pass http://backend; + } + + location /grafana/ { + proxy_set_header Host $http_host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_pass http://grafana; + } + + location = /metrics { + return 404; + } +} \ No newline at end of file