Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/asacristani/fastapi-rocket-boilerplate
๐๐จ FastAPI Rocket Boilerplate to build an API based in Python with its most modern technologies!
https://github.com/asacristani/fastapi-rocket-boilerplate
boilerplate boilerplate-backend fastapi
Last synced: about 2 months ago
JSON representation
๐๐จ FastAPI Rocket Boilerplate to build an API based in Python with its most modern technologies!
- Host: GitHub
- URL: https://github.com/asacristani/fastapi-rocket-boilerplate
- Owner: asacristani
- Created: 2023-09-20T01:16:38.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-04-12T17:49:08.000Z (8 months ago)
- Last Synced: 2024-04-13T00:02:33.383Z (8 months ago)
- Topics: boilerplate, boilerplate-backend, fastapi
- Language: Python
- Homepage:
- Size: 313 KB
- Stars: 384
- Watchers: 6
- Forks: 57
- Open Issues: 19
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
- Security: SECURITY.md
Awesome Lists containing this project
README
๐๐จ FastAPI Rocket Boilerplate to build an API based in Python with its most modern technologies!---
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
- [Python 3.11](https://www.python.org/downloads/release/python-3114/)
- [Docker](https://docs.docker.com/engine/install/)
- [Node](https://nodejs.org/en) for SDK frontend generation## ๐๏ธ Use
### ๐ง Installation
1. Clone the repo
2. Create a virtual environment:
```shell
python3.11 -m venv venv
```3. Install the requirements with Poetry for developing, testing and debugging purposes. Also install Trunk for pre-commit and code quality.
```shell
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](https://sentry.io/welcome/) to create a project and obtain the Sentry DSN.
In the `.env` file, include the following variable:
```shell
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.
```shell
make run
```Congrats! the API is working at this point, you can check:
- Docs: http://localhost:8000/docs
- Admin: http://localhost:8000/admin
- RabbitMQ: http://localhost:15672/For admin, use:
```shell
ADMIN_USER=superuser
ADMIN_PASS=admin
```For generating the SDK frontend client (the app should be running):
```shell
make generate_sdk
```You will find the generated client in `generate_client/openapi.json`
### ๐งช Test
Run pytest with coverage for unit testing.
```shell
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`
```python
from app.core.base.models import ModelCoreclass NewModel(ModelCore, table=True):
unique_property: str
```2. Import the new class into the migration model file `app.core.db.migrations.models`
3. Create a new migration
4. Create an AdminModel in `app.services.admin.models`:```python
from app.core.admin.models import ModelViewCoreclass NewModelAdmin(ModelViewCore, model=NewModel):
# You can add config settings here for the Admin panel.
pass
```5. 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:
```python
from fastapi import APIRouter, Dependsfrom 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
```### ๐๏ธ 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_PASSAlso, it is possible you want to modify the expiry time of access/refresh tokens.
## ๐ฎ Future features
### Refactor
- [x] Organise better the root files
### Monitoring
- [ ] Add logging
- [x] Add Sentry
- [ ] Add Flower### Testing
- [ ] Integrity tests
- [x] pytest-alembic
- [x] Cover 100% with unit-testing### Quality code
- [x] 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