Skip to content

JAVI-CC/python-api-crud-template

Repository files navigation

FastAPI

FastAPI framework, high performance, easy to learn, fast to code, ready for production

Test Coverage Package version Supported Python versions


Application made with FastAPI that consists of the creation of a CRUD of a user and system authentication api that contains the following functionalities:

  • Relationships between different models.
  • Primary key format UUID.
  • Contains endpoints of type GET, POST, PATCH, PUT and DELETE
  • Generate token by JWT (Json web tokens)
  • SQLAlchemy.
  • Pydantic.
  • Postman collection.json file to import and create use endpoints.
  • Requests validations.
  • Exception handling.
  • Email sending notification to verify the user.
  • Enums.
  • Middleware.
  • Dependencies validations.
  • File Storage.
  • Exports in Excel and PDF format.
  • Jinja2 Email Templates.
  • Multi language support.
  • Migration file to create all the tables in the database.
  • Seeders are in JSON format.
  • Python 3.12
  • Users contain different roles and permissions.
  • The project contains the files to deploy it in Docker.

Headers

Key Value
Authorization {Token provided by JWT}
Accept application/json
Content-Type application/json
Accept-Language application/json

Setup

$ apt-get install wkhtmltopdf
$ cp .env.example .env.dev
$ pip install --no-cache-dir --upgrade -r requirements.txt
$ sed -i '/JWT_SECRET_KEY=""/d' .env.dev && jwt_secret_key="JWT_SECRET_KEY=$(openssl rand -hex 32)" && sed -i -e "22i$jwt_secret_key" .env.dev
$ fastapi run app/main.py --port 8000 --reload

User admin credentials

User: [email protected]
Password: Pass-1234

OpenAPI documentation: http://localhost:8000/docs

FastAPI

Endpoints Auth:

Method Path Description Auth Is Admin
POST /auth/login Login a user No No
GET /auth/me Check if user authenticated Yes No

Endpoints Users:

Method Path Description Auth Is Admin
GET /users Get all the users Yes No
GET /users/{id} Get a user Yes No
POST /users Add new user Yes Yes
PUT /users/{id} Update a user Yes Yes
PATCH /users/update_password Update password current user Yes No
DELETE /users/{id} Delete a user Yes Yes
GET /users/export/excel Export all users in Excel format Yes Yes
GET /users/export/pdf Export all users in Pdf format Yes Yes
GET /users/confirm_email/{hash} Confirm verification user No No
GET /users/resend/confirm_email Resend email verification user Yes No

Endpoints Roles:

Method Path Description Auth Is Admin
GET /roles Get all the roles Yes Yes
GET /roles/{id} Get a role Yes Yes


Configure values in the .env.dev file


APP_URL="http://localhost:8000"

DB_DRIVER=""
DB_HOST=""
DB_PORT=""
DB_DATABASE=""
DB_USERNAME=""
DB_PASSWORD=""

MAIL_SERVER=""
MAIL_PORT=""
MAIL_USERNAME=""
MAIL_PASSWORD=""
MAIL_FROM_ADDRESS=""
MAIL_FROM_NAME=""
MAIL_STARTTLS=""
MAIL_SSL_TLS=""
MAIL_USE_CREEDENTIALS=""

# command generate JWT_SECRET_KEY: openssl rand -hex 32
JWT_SECRET_KEY=""
JWT_ALGORITHM=""

Deploy to Docker 🐳

Docker repository: https://hub.docker.com/r/javi98/python-api-crud-template

Containers:

  • python:3.12 - :8000
  • mariadb:11.2.2 - :3306
  • phpmyadmin:5.2.1 - :8085->80/tcp
  • mailhog:v1.0.1 - :1025 # smtp server :8025 # web ui

Containers structure:

├── python-api-crud-template-app
├── python-api-crud-template-db
├── python-api-crud-template-smtp
└── python-api-crud-template-phpmyadmin

Setup:

$ git clone https://github.com/JAVI-CC/python-api-crud-template.git
$ cd python-api-crud-template
$ cp .env.example .env.dev
$ docker compose up -d
$ docker compose exec app chmod +x ./docker-config/run.sh
$ docker compose exec app ./docker-config/run.sh

Once you have the containers deployed, you can access the API at http://localhost:8000