Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
POSTGRES_USER=
POSTGRES_PASSWORD=
POSTGRES_DB=
DATABASE_URL=

REDIS_PASSWORD=
REDIS_DB=
REDIS_URL=

DOMAIN=localhost
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
5 changes: 5 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <repository-url>
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

Expand Down
22 changes: 22 additions & 0 deletions docker-compose.override.yml
Original file line number Diff line number Diff line change
@@ -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/"
8 changes: 6 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
9 changes: 9 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/sh

echo "Database migration started"

alembic upgrade head

echo "Database migration ended"

exec "$@"
3 changes: 0 additions & 3 deletions nginx/Dockerfile

This file was deleted.

8 changes: 4 additions & 4 deletions nginx/default.conf → nginx/default.conf.template
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ upstream grafana {

server {
listen 80;
server_name bdnsrvepic.duckdns.org;
server_name ${DOMAIN};
location / {
return 301 https://$host$request_uri;
}
Expand All @@ -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;
Expand Down
30 changes: 30 additions & 0 deletions nginx/local.conf
Original file line number Diff line number Diff line change
@@ -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;
}
}