https://github.com/neZorinEgor/FastAPI-StarterPack
⚡ Шаблон для создания backend приложений на фреймворке FastAPI в связке с Redis, Nginx и Mysql
https://github.com/neZorinEgor/FastAPI-StarterPack
alembic fastapi mysql nginx redis
Last synced: about 1 month ago
JSON representation
⚡ Шаблон для создания backend приложений на фреймворке FastAPI в связке с Redis, Nginx и Mysql
- Host: GitHub
- URL: https://github.com/neZorinEgor/FastAPI-StarterPack
- Owner: NeZorinEgor
- License: mit
- Created: 2024-06-23T06:40:42.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2024-08-29T13:47:00.000Z (9 months ago)
- Last Synced: 2024-08-29T15:04:15.499Z (9 months ago)
- Topics: alembic, fastapi, mysql, nginx, redis
- Language: Python
- Homepage:
- Size: 15.6 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# FastAPI-StarterPack
![]()
![]()
![]()
![]()
> Everything you need to create a `production ready` application on FastAPI.
Feature:
1. DB configuration: `Postgres + SQLAlchemy + Alembic`
2. Filestorage: `AWS-S3`
3. Broker/Cache: `Celery + Redis`The presence of `postgresql`, `redis` and `s3` is simulated in `docker-compose`.
### Usefulness's:
1. Creating and launching migrations:
```bash
alembic revision --autogenerate -m ""
alembic upgrade head
```
2. Setting up models for migration in [env.py](app/alembic/env.py)
```bash
# add your model's MetaData object here
from src.database import Base
# Your models here ↓
# from src..model import SomeModeltarget_metadata = Base.metadata
```
3. Generation of `RSA` keys for JWT:
```bash
# Generate an RSA private key of size 2048
openssl genrsa -out jwt-private.pem 2048
# Extract a public key from a key pair that can be used in a certificate
openssl rsa -in jwt-private.pem -outform PEM -pubout -out jwt-public.pem
```4. Cache connections to `get` routers:
```python
from collections.abc import AsyncIterator
from contextlib import asynccontextmanagerfrom fastapi import FastAPI
from fastapi_cache import FastAPICache
from fastapi_cache.backends.redis import RedisBackend
from fastapi_cache.decorator import cachefrom redis import asyncio as aioredis
@asynccontextmanager
async def lifespan(_: FastAPI) -> AsyncIterator[None]:
redis = aioredis.from_url("redis://localhost")
FastAPICache.init(RedisBackend(redis), prefix="fastapi-cache")
yieldapp = FastAPI(lifespan=lifespan)
@app.get("/")
@cache(expire=60) # <--- Obligatory after the router decorator
async def index():
return dict(hello="world")
```
5. Local dev start up:
```bash
docker compose up
alembic upgrade head; uvicorn src.main:app --reload --host 0.0.0.0 --port 8000
```