Skip to content

Commit

Permalink
feat: update README for Docker setup, add Dockerfile, docker-compose,…
Browse files Browse the repository at this point in the history
… and Nginx configuration
  • Loading branch information
hackerman70000 committed Jan 11, 2025
1 parent a913413 commit 5badbdf
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 70 deletions.
84 changes: 19 additions & 65 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,48 +8,25 @@ This project is a pharmacy application that includes user account management, pr

Follow these steps to set up the project locally:

#### 1. **Clone the Repository**
#### Access the Docker Environment

Clone the project repository and navigate to the backend directory:
In VSCode, use the "Reopen in Container" option to seamlessly open the project inside the container.

```bash
git clone https://github.com/hackerman70000/pharmacy-app.git
```

#### 2. Access the Docker Environment

For an optimal development experience, it is recommended to use Visual Studio Code with the Dev Containers extension.

You can access the container in two ways:

1. **Reopen in Container**: In VSCode, use the "Reopen in Container" option to seamlessly open the project inside the container.
- During container creation, you'll be prompted to enter e-mail credentials for the email notification system

2. **Access via Terminal**: Alternatively, you can connect to the container directly through the terminal using the appropriate Docker commands.

#### 3. **Create the Database**

Use the `createdb` command to create the PostgreSQL database:

```bash
createdb pharmacy_db
```

#### 4. **Navigate to `/backend`**
#### Navigate to `/backend`

```bash
cd backend
```

#### 5. **Apply Migrations**
#### Apply Migrations

Run the following command to apply existing database migrations:

```bash
uv run flask db upgrade
```

#### 6. **Run the Development Server**
#### Run the Development Server

Start the Flask development server:

Expand All @@ -66,65 +43,42 @@ uv run flask run
To completely reset the database and its data:

```bash
uv run flask db downgrade base # Go back to empty state
uv run flask db upgrade # Recreate all tables
uv run flask seed products # Reseed with example data
uv run flask db downgrade base
uv run flask db upgrade
```

#### CLI Commands

Available custom CLI commands:

```bash
uv run flask seed products # Add example pharmacy products to the database
uv run flask seed products
```

#### PostgreSQL Database

To inspect the database directly using PostgreSQL CLI:
### Tests

1. Connect to the database:
Run all tests:

```bash
psql pharmacy_db
uv run pytest
```

2. Useful PostgreSQL commands:
To run tests in the Docker container:

```sql
\l # List all databases
\dt # List all tables in the current database
\d TABLE # Describe table structure
\du # List all users and their roles
\q # Quit psql
```

Example queries:

```sql
SELECT * FROM users; # View all users
SELECT * FROM products; # View all products
SELECT * FROM cart; # View all cart items
```bash
docker compose exec backend uv run pytest
```

---

### Common Issues and Solutions
## Production deployment

1. Migration issues:
Configure Environment Variables in `backend/.env` file with all necessary configuration.

To reset migrations:
Build and start services:

```bash
rm -rf migrations/
uv run flask db init
uv run flask db migrate -m "Reset migrations"
uv run flask db upgrade
docker compose up --build -d
```

2. **Order Email Notifications**:
If order confirmation emails are not being received:
- Check your spam folder
- Verify email credentials in .env file
- Ensure the Gmail App Password is correct
- Check the application logs for email-related errors
Access the application at http://localhost (Port 80). All requests go through Nginx reverse proxy.
18 changes: 18 additions & 0 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FROM python:3.12-slim

WORKDIR /app

RUN apt-get update && apt-get install -y \
gcc \
&& rm -rf /var/lib/apt/lists/*

RUN pip install uv

COPY . .
RUN uv sync

ENV FLASK_APP=app.py
ENV FLASK_ENV=production

CMD uv run flask db upgrade && \
uv run gunicorn --bind 0.0.0.0:5000 'app:create_app()'
7 changes: 4 additions & 3 deletions backend/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
from app.cli.commands import seed_cli
from flask.cli import FlaskGroup

app = create_app()
application = create_app()
app = application
cli = FlaskGroup(create_app=create_app)

app.cli.add_command(seed_cli)
application.cli.add_command(seed_cli)

if __name__ == "__main__":
app.run(host='0.0.0.0', debug=True)
application.run(host="0.0.0.0", debug=True)
3 changes: 2 additions & 1 deletion backend/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ dependencies = [
"pytest>=8.3.4",
"pytest-cov>=6.0.0",
"pytest-mock>=3.14.0",
"gunicorn>=23.0.0",
]

[dependency-groups]
Expand Down Expand Up @@ -51,4 +52,4 @@ exclude_lines = [
"raise ImportError",
]
show_missing = true
fail_under = 60
fail_under = 60
17 changes: 16 additions & 1 deletion backend/uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
services:
backend:
build:
context: ./backend
dockerfile: Dockerfile
restart: always
env_file:
- ./backend/.env
depends_on:
- db
networks:
- internal

db:
image: postgres:latest
restart: always
volumes:
- postgres_data:/var/lib/postgresql/data
env_file:
- ./backend/.env
networks:
- internal

nginx:
image: nginx:latest
restart: always
ports:
- "80:80"
volumes:
- ./nginx.conf:/etc/nginx/conf.d/default.conf
depends_on:
- backend
networks:
- internal

volumes:
postgres_data:

networks:
internal:
driver: bridge
12 changes: 12 additions & 0 deletions nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
server {
listen 80;
server_name _;

location / {
proxy_pass http://backend:5000;
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;
}
}

0 comments on commit 5badbdf

Please sign in to comment.