Pull this repo to quickly start a new project with FastAPI and JSON Web Token (JWT) authorisation
- Create
.env
file:ENVIRONMENT=local DEBUG=true SENTRY_DSN= JWT_SECRET=...replace.secret... POSTGRES_HOST=127.0.0.1 POSTGRES_DATABASE=database_name POSTGRES_USER=username POSTGRES_PASSWORD=...replace.password...
- Run database server
- In console: run
make dev
- In console: run
- Create environment:
- In console: run
poetry shell
- In console: run
poetry install
- In console: run
- Add interpreter:
- Add new interpreter
- Local interpreter
- Poetry interpreter
- Choose created environment
- Add configuration for API:
- Open "Edit configurations"
- Add new configuration → FastAPI
- Fill "Application file": full path to
api/app.py
file - Fill "Application name":
app
- Fill "Uvicorn options":
--host=127.0.0.1 --reload --no-server-header
- Fill "Working directory": full path to project folder
- Enter database server
- In console: run
make postgres
- In console: run
- Create database for tests
In PostgreSQL console run:Close consoleCREATE DATABASE database_tests OWNER username;
- Open "Edit configurations"
- Open "Edit configuration templates"
- Choose "Python tests" → "pytest"
- Fill "Working directory": full path to project folder
- Open console
- Enter poetry shell (if not in already):
poetry shell
- Create migration:
For example:
alembic revision --autogenerate -m "<migration_name>" --rev-id "<revision_id>"
alembic revision --autogenerate -m "create_event_table" --rev-id "02"
- Apply migration(s):
Or upgrade to next revision:
alembic upgrade head
Or look at SQL code:alembic upgrade +1
alembic upgrade --sql
- Downgrade (if needed):
Or downgrade to previous revision:
alembic downgrade base
alembic downgrade -1
In order to tun a management command you need to define PYTHONPATH
variable first
So here are some examples:
Example for fish shell:
PYTHONPATH=(pwd) python management_commands/create_user.py --email[email protected] --first_name=me --last_name=myself
PYTHONPATH=(pwd) python management_commands/generate_token.py --user_id=1
Example for bash shell:
PYTHONPATH=$(pwd) python management_commands/create_user.py [email protected] --first_name=me --last_name=myself
PYTHONPATH=$(pwd) python management_commands/generate_token.py --user_id=1