charlesa101 / fastapi-boilerplate

Fastapi complete boilerplate

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

FastAPI REST API boilerplate

Description

FastAPI REST boilerplate for typical project

Table of Contents

Features

  • Precommit (Pre-commit)
  • Config Service (Pydantic).
  • Database (Sqlalchemy).
  • Database migration (Alembic)
  • Swagger.
  • Redoc.
  • Sign in and sign up via email.
  • Social sign in (apple, facebook, google, linkedin, microsoft)
  • Seeding (sqlalchemyseed).
  • Mailing
  • Realtime notification using messaging queue and python-socketio
  • File uploads (aws s3, cloudinary, google cloud storage)
  • Redis
  • Admin and User roles using RBAC (Casbin).
  • Admin dashboard using (sqladmin)
  • I18N
  • Elasticseach
  • E2E and units tests.
  • Monitoring using grafana and prometheus (Grafana)
  • Docker.
  • CI (Gitlab).

Project Structure

seeder           # Configurtion for database seeding
migrations       # Alembic migration files
app              # Rest api files
app.core         # General components like config, security, types, role, etc...
app.db           # Database connection specific
app.crud         # CRUD for types from models
app.models       # Sqlalchemy models
app.schemas      # Pydantic models that used in crud or handlers
app.templates    # Html files for mails
app.endpoints    # Restapi endpoints files
├── app
|   |
│   ├── core
│   ├── crud
│   ├── db
│   ├── endpoints
│   │   ├── api.py
│   │   └── v1
│   │   └── v2
│   │   └── ...
│   ├── main.py
│   ├── models
│   ├── schemas
|   |
│   └── templates
|
├── migrations
├── seeder
|── .vscode

Environmnent variables

To correctly run the project, you will need some environment variables. Expose & import them in core/config.py

befor starting you need to generate RSA key pairs, both public and private keys. You can use this website or any other method you prefer to generate these keys. Keep a note of the file paths where you save these keys on your project folder (like private_key.pem and public_key.pem) or local machine.

  • ENV : Running Environment can be dev, staging, preprod
  • API_BASE_URL: The pathname of the api version url
  • API_URL: api base url
  • DB_HOST: Postgres database host
  • DB_PORT: Postgres database port
  • DB_NAME: Postgres database name
  • DB_USER_NAME: Postgres user
  • DB_PASSWORD: Postgres password
  • SECRET_KEY: Postgres password
  • ACCESS_TOKEN_EXPIRE_MINUTES: Access token duration
  • PRIVATE_KEY_PATH: Specify the absolute file paths to the RSA private key you generated earlier (private_key.pem if you saved it in your project root folder)
  • PUBLIC_KEY_PATH: Specify the absolute file paths to the RSA public key you generated earlier (public_key.pem if you saved it in your project root folder)
  • SMTP_TLS: Indicates whether to use TLS (true or false) for SMTP email communication.
  • SMTP_PORT: The port number for SMTP email communication.
  • SMTP_HOST: The hostname or IP address of the SMTP server for sending emails.
  • SMTP_USER: The username for authenticating with the SMTP server.
  • SMTP_PASSWORD: The password for authenticating with the SMTP server.
  • RABBIT_MQ_HOST: RabbitMQ host (localhost in this case)
  • RABBIT_MQ_PORT: RabbitMQ port (5672 in this case)
  • RABBIT_MQ_PASSWORD: RabbitMQ password (password123 in this case)
  • RABBIT_MQ_USER: RabbitMQ user (root in this case)
  • APPLE_CLIENT_ID: Apple OAuth client ID
  • APPLE_SECRET_KEY: Apple OAuth secret key
  • APPLE_WEBHOOK_OAUTH_REDIRECT_URI: Redirect URI for Apple OAuth callbacks
  • FACEBOOK_CLIENT_ID: Facebook OAuth client ID
  • FACEBOOK_SECRET_KEY: Facebook OAuth secret key
  • FACEBOOK_WEBHOOK_OAUTH_REDIRECT_URI: Redirect URI for Facebook OAuth callbacks
  • GOOGLE_CLIENT_ID: Google OAuth client ID
  • GOOGLE_SECRET_KEY: Google OAuth secret key
  • GOOGLE_WEBHOOK_OAUTH_REDIRECT_URI: Redirect URI for Google OAuth callbacks
  • LINKEDIN_CLIENT_ID: LinkedIn OAuth client ID
  • LINKEDIN_SECRET_KEY: LinkedIn OAuth secret key
  • LINKEDIN_WEBHOOK_OAUTH_REDIRECT_URI: Redirect URI for LinkedIn OAuth callbacks
  • MSAL_CLIENT_ID: Microsoft MSAL client ID
  • MSAL_CLIENT_SECRET: Microsoft MSAL client secret
  • MSAL_WEBHOOK_OAUTH_REDIRECT_URI: Redirect URI for Microsoft MSAL OAuth callbacks
  • CLOUDINARY_CLOUD_NAME: The name of your Cloudinary cloud.
  • CLOUDINARY_API_KEY: Your Cloudinary API key.
  • CLOUDINARY_API_SECRET: Your Cloudinary API secret.
  • AWS_BUCKET_NAME: The name of the AWS S3 bucket you want to access.
  • AWS_KEY_ID: AWS access key ID for accessing the S3 bucket.
  • AWS_SECRET_KEY: AWS secret key for accessing the S3 bucket.
  • AWS_REGION: The AWS region where the S3 bucket is located.
  • GCS_BUCKET_NAME: The name of the Google Cloud Storage (GCS) bucket you want to access.
  • REDIS_HOST:
  • REDIS_PORT:
  • REDIS_DB:

Quick run

git clone --depth 1 https://github.com/kaanari-tech/fastapi-boilerplate.git my-app
cd my-app/
cp .env.example .env
docker-compose up -d --build

For check status run

docker-compose logs

Comfortable development

git clone --depth 1 https://github.com/kaanari-tech/fastapi-boilerplate.git my-app
cd my-app/
cp .env.example .env

Change DB_HOST=postgres to DB_HOST=localhost, DB_PORT=5432 to DB_PORT=6001 RABBIT_MQ_HOST=rabbitmq to RABBIT_MQ_HOST=localhost REDIS_HOST=redis-cache to REDIS_HOST=localhost make sure you have poetry install Run additional container:

docker-compose up -d postgres-db redis-cache rabbitmq adminer
poetry install
poe migrate
poe run

Links

Database utils

Generate migration

poe makemigrations

Run migration

poe migrate

Revert migration

poe dwngrade

Drop all tables in database

poe drop-tables

About

Fastapi complete boilerplate

License:MIT License


Languages

Language:Python 98.5%Language:Dockerfile 0.7%Language:Mako 0.7%Language:Makefile 0.1%