Giter Site home page Giter Site logo

asacristani / fastapi-rocket-boilerplate Goto Github PK

View Code? Open in Web Editor NEW
439.0 439.0 65.0 475 KB

๐Ÿ๐Ÿ’จ FastAPI Rocket Boilerplate to build an API based in Python with its most modern technologies!

License: MIT License

Dockerfile 0.83% Makefile 1.48% Python 95.82% Mako 1.09% HTML 0.51% Shell 0.27%
alembic api async backend boilerplate boilerplate-backend celery docker fastapi poetry postgres pydantic pytest python rabbitmq redis sqlmodel trunk

fastapi-rocket-boilerplate's Introduction

Test Coverage Python 3.11

๐Ÿ๐Ÿ’จ FastAPI Rocket Boilerplate to build an API based in Python with its most modern technologies!


Python Fastapi Postgresql Celery Rabbitmq Redis Docker

Also sqlmodel, pydantic, alembic, poetry, ...


๐Ÿงฉ Features

  • Infrastructure: the common services that every backend needs, served in local by Docker Compose.
  • Easy: all the commands ready by Makefile.
  • Fast: thanks to Fastapi and async programming.
  • Async: Celery using RabbitMQ as broker and Redis as backend.
  • ORM: custom sqlmodel orm as django orm and mongoengine.
  • Authentication: OAuth2 with access/refresh tokens.
  • Admin dashboard: custom admin dashboard as django by sqladmin.
  • Rock-Solid Reliability: CI and pre-commit by Trunk. Integrity testing and covered by unit test at +95%.
  • Frontend friendly: auto generation of SDK Typescript client.

โš™๏ธ Requirements

๐ŸŽ›๏ธ Use

๐Ÿ”ง Installation

  1. Clone the repo

  2. Create a virtual environment:

python3.11 -m venv venv
  1. Install the requirements with Poetry for developing, testing and debugging purposes. Also install Trunk for pre-commit and code quality.
make install

VS Code

If you are using VS Code, I recommend you to install some plugins:

  • Trunk: for improving the quality code.

โ„น๏ธ You can test the pre-commit without committing running trunk check

Sentry Integration

To integrate Sentry for error monitoring, add the Sentry DSN (Data Source Name) to the .env file. Set up your environment by signing in to Sentry to create a project and obtain the Sentry DSN.

In the .env file, include the following variable:

SENTRY_DSN=your_sentry_dsn_here

With this configuration, errors will be captured and reported to your Sentry project for effective monitoring.

๐Ÿ”Œ Build and run

Build and run the Docker services for using in Locaql.

make run

Congrats! the API is working at this point, you can check:

For admin, use:

ADMIN_USER=superuser
ADMIN_PASS=admin

For generating the SDK frontend client (the app should be running):

make generate_sdk

You will find the generated client in generate_client/openapi.json

๐Ÿงช Test

Run pytest with coverage for unit testing.

make test

You do not need to run inside Docker container.

The DB is replaced by a SQLite db in memory ๐Ÿ˜Ž

๐Ÿšš Migrations

Use Alembic for DB migrations.

If you create a new model, import it in: app/core/db/migrations/models.py

After this, or modified a previous model, create the migration document:

docker-compose run app alembic revision --autogenerate -m "your commit"

If you are trying to do something complicated, maybe you need to fix the file manually.

Migration file should be created inside the Docker container because the DB url is referencing the Docker network domain.

Migrations will run when docker compose up, but you can run them manually:

docker-compose run app alembic upgread head

๐Ÿ›  Extend

Basically, you will want to create new services that contain endpoints and models. And of course, it is almost completely sure you need to add new extra dependencies.

You can use the service user as reference.

๐Ÿ“ฆ Models

If you want to create a new model to be stored in the DB, you should follow these steps:

  1. Create a new Class based in ModelCore with table=True
from app.core.base.models import ModelCore

class NewModel(ModelCore, table=True):
    unique_property: str
  1. Import the new class into the migration model file app.core.db.migrations.models
  2. Create a new migration
  3. Create an AdminModel in app.services.admin.models:
from app.core.admin.models import ModelViewCore

class NewModelAdmin(ModelViewCore, model=NewModel):
    # You can add config settings here for the Admin panel.
    pass
  1. Append it in admin_models into app.services.admin.config

๐Ÿš Routes

If you want to create a new view protected by auth, you should include the get_current_user dependency.

Here you have an example of a new service with a protected route:

from fastapi import APIRouter, Depends

from app.core.auth.functions import get_current_user

router = APIRouter(
    prefix="/security",
    tags=["security"]
)

@router.get("/protected")
def protected_route(current_user: str = Depends(get_current_user)):
    """ Endpoint for auth test"""
    return {"message": f"ยกHola, {current_user}! This is a protected url and you are inside!"}

And then append the router in routers into app.main

For creating new users, they can register by themselves or be added by Admin panel.

๐Ÿ—๏ธ Dependencies

Use Poetry like:

poetry add <new_dependency>

๐Ÿ—œ๏ธ Environment variables

You should change the next env vars in .env:

  • Password hash:
    • SECRET_KEY: run in the terminal openssl rand -base64 32 to generate a new one
  • Admin superuser:
    • ADMIN_USER
    • ADMIN_PASS

Also, it is possible you want to modify the expiry time of access/refresh tokens.

๐Ÿ”ฎ Future features

Refactor

  • Organise better the root files

Monitoring

  • Add logging
  • Add Sentry
  • Add Flower

Testing

  • Integrity tests
  • pytest-alembic
  • Cover 100% with unit-testing

Quality code

  • Use a complete quality check for the code and pre-commit

Async

  • Use 100% async/await for routes and database connections

Auth

  • Authentication client with Google

Admin

  • Search events by model AND id
  • Fix popup for reverse_delete
  • Relationship of records into model details (performance)
  • Fix export CSV

fastapi-rocket-boilerplate's People

Contributors

asacristani avatar doziestar avatar esanchezm avatar mbxrateeq avatar onuralpszr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

fastapi-rocket-boilerplate's Issues

Monitoring: add logging system

Include logs using different levels for the following events:

  • db interactions
  • celery task interactions
  • endpoint interactions

Admin: Fix popup for reverse_delete

Now the message for delete and reverse_delete is the same (delete message).
It could be good to modify for reverse_delete being more accurate.

Issue Title: Implement Rate Limiting Middleware

Issue Description: Currently, the FastAPI Rocket Boilerplate lacks rate limiting middleware, which is essential for controlling the number of requests a client can make to the API within a specific time frame. This issue aims to implement rate limiting middleware, allowing developers to configure and enforce rate limits on API endpoints.

Proposed Solution: Implement a rate limiting middleware that:

Supports rate limits based on IP address or user tokens.
Allows developers to configure rate limits for specific routes or groups of routes.
Provides clear documentation on how to configure and use rate limiting in the boilerplate.
Expected Impact: The addition of rate limiting middleware would enhance the security and reliability of the API by preventing abuse and ensuring fair usage of resources

Create a CLI tool?

Imitating Django, it could be amazing a CLI manager program for different things:

  • Explore and manipulate the data though the ORM
  • Handle migrations with Alembic
  • Maybe remove the makefile and do everything in python?
  • ...

Support Python 3.10

I recommend you add python 3.10 support if possible.
Most enterprises will be stuck to 3.10 due to their use of Ubuntu 22.04.

They don't upgrade things as often as they should. So its in the best interest of the maintainers to do so.

Python3 and Makefile

When running through the installation guide, I needed to change pip to pip3 / python to python3 in order to get the setup running, as my local system is Mac Apple Silicon. Because Mac laptops have python 2.7 installed as base python version, I use shell aliases to map to the latest python/pip versions. Shell aliases don't appear to be expanded in Make, as /bin/sh is a non-interactive shell. How do you want your users to proceed here? Open to suggestions, thanks.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.