@@ -410,101 +410,109 @@ poetry run alembic upgrade head
410410### 5.1 Project Structure
411411First, you may want to take a look at the project structure and understand what each file is doing.
412412``` sh
413- . # FastAPI-boilerplate folder. Rename it to suit your project name
414- ├── Dockerfile # Dockerfile for building the application container.
415- ├── LICENSE.md # License file for the project.
416- ├── README.md # Project README providing information and instructions.
417- ├── docker-compose.yml # Docker Compose file for defining multi-container applications.
413+ . # FastAPI-boilerplate folder. Rename it to suit your project name
414+ ├── Dockerfile # Dockerfile for building the application container.
415+ ├── LICENSE.md # License file for the project.
416+ ├── README.md # Project README providing information and instructions.
417+ ├── docker-compose.yml # Docker Compose file for defining multi-container applications.
418418│
419- └── src # Source code directory.
420- ├── __init__.py # Initialization file for the src package.
421- ├── alembic.ini # Configuration file for Alembic (database migration tool).
422- ├── poetry.lock # Poetry lock file specifying exact versions of dependencies.
423- ├── pyproject.toml # Configuration file for Poetry, lists project dependencies.
419+ └── src # Source code directory.
420+ ├── __init__.py # Initialization file for the src package.
421+ ├── alembic.ini # Configuration file for Alembic (database migration tool).
422+ ├── poetry.lock # Poetry lock file specifying exact versions of dependencies.
423+ ├── pyproject.toml # Configuration file for Poetry, lists project dependencies.
424424 │
425- ├── app # Main application directory.
426- │ ├── __init__.py # Initialization file for the app package.
427- │ ├── main.py # Entry point that imports and creates the FastAPI application instance.
428- │ ├── worker.py # Worker script for handling background tasks.
425+ ├── app # Main application directory.
426+ │ ├── __init__.py # Initialization file for the app package.
427+ │ ├── main.py # Entry point that imports and creates the FastAPI application instance.
428+ │ ├── worker.py # Worker script for handling background tasks.
429429 │ │
430- │ ├── api # Folder containing API-related logic.
430+ │ ├── api # Folder containing API-related logic.
431431 │ │ ├── __init__.py
432- │ │ ├── dependencies.py # Defines dependencies that can be reused across the API endpoints.
433- │ │ ├── exceptions.py # Contains custom exceptions for the API.
434- │ │ ├── paginated.py # Utilities for paginated responses in APIs.
432+ │ │ ├── dependencies.py # Defines dependencies that can be reused across the API endpoints.
433+ │ │ ├── exceptions.py # Contains custom exceptions for the API.
434+ │ │ ├── paginated.py # Utilities for paginated responses in APIs.
435435 │ │ │
436- │ │ └── v1 # Version 1 of the API.
436+ │ │ └── v1 # Version 1 of the API.
437437 │ │ ├── __init__.py
438- │ │ ├── login.py # API routes related to user login.
439- │ │ ├── posts.py # API routes related to posts.
440- │ │ ├── rate_limits.py # API routes for rate limiting features.
441- │ │ ├── tasks.py # API routes related to background tasks.
442- │ │ ├── tiers.py # API routes for handling different user tiers.
443- │ │ └── users.py # API routes related to user management.
438+ │ │ ├── login.py # API routes related to user login.
439+ │ │ ├── logout.py # API routes related to user logout (token blacklist).
440+ │ │ ├── posts.py # API routes related to posts.
441+ │ │ ├── rate_limits.py # API routes for rate limiting features.
442+ │ │ ├── tasks.py # API routes related to background tasks.
443+ │ │ ├── tiers.py # API routes for handling different user tiers.
444+ │ │ └── users.py # API routes related to user management.
444445 │ │
445- │ ├── core # Core utilities and configurations for the application.
446+ │ ├── core # Core utilities and configurations for the application.
446447 │ │ ├── __init__.py
447- │ │ ├── cache.py # Utilities related to caching.
448- │ │ ├── config.py # Application configuration settings.
449- │ │ ├── database.py # Database connectivity and session management.
450- │ │ ├── exceptions.py # Contains core custom exceptions for the application.
451- │ │ ├── logger.py # Logging utilities.
452- │ │ ├── models.py # Base models for the application.
453- │ │ ├── queue.py # Utilities related to task queues.
454- │ │ ├── rate_limit.py # Rate limiting utilities and configurations.
455- │ │ ├── security.py # Security utilities like password hashing and token generation.
456- │ │ └── setup.py # File defining settings and FastAPI application instance definition.
448+ │ │ ├── cache.py # Utilities related to caching.
449+ │ │ ├── config.py # Application configuration settings.
450+ │ │ ├── database.py # Database connectivity and session management.
451+ │ │ ├── exceptions.py # Contains core custom exceptions for the application.
452+ │ │ ├── logger.py # Logging utilities.
453+ │ │ ├── models.py # Base models for the application.
454+ │ │ ├── queue.py # Utilities related to task queues.
455+ │ │ ├── rate_limit.py # Rate limiting utilities and configurations.
456+ │ │ ├── security.py # Security utilities like password hashing and token generation.
457+ │ │ └── setup.py # File defining settings and FastAPI application instance definition.
457458 │ │
458- │ ├── crud # CRUD operations for the application.
459+ │ ├── crud # CRUD operations for the application.
459460 │ │ ├── __init__.py
460- │ │ ├── crud_base.py # Base CRUD operations class that can be extended by other CRUD modules.
461- │ │ ├── crud_posts.py # CRUD operations for posts.
462- │ │ ├── crud_rate_limit.py # CRUD operations for rate limiting configurations.
463- │ │ ├── crud_tier.py # CRUD operations for user tiers.
464- │ │ ├── crud_users.py # CRUD operations for users.
465- │ │ └── helper.py # Helper functions for CRUD operations.
461+ │ │ ├── crud_base.py # Base CRUD operations class that can be extended by other CRUD modules.
462+ │ │ ├── crud_posts.py # CRUD operations for posts.
463+ │ │ ├── crud_rate_limit.py # CRUD operations for rate limiting configurations.
464+ │ │ ├── crud_tier.py # CRUD operations for user tiers.
465+ │ │ ├── crud_token_blaclist.py # CRUD operations for token blacklist.
466+ │ │ ├── crud_users.py # CRUD operations for users.
467+ │ │ └── helper.py # Helper functions for CRUD operations.
466468 │ │
467- │ ├── models # ORM models for the application.
469+ │ ├── models # ORM models for the application.
468470 │ │ ├── __init__.py
469- │ │ ├── post.py # ORM model for posts.
470- │ │ ├── rate_limit.py # ORM model for rate limiting configurations.
471- │ │ ├── tier.py # ORM model for user tiers.
472- │ │ └── user.py # ORM model for users.
471+ │ │ ├── post.py # ORM model for posts.
472+ │ │ ├── rate_limit.py # ORM model for rate limiting configurations.
473+ │ │ ├── tier.py # ORM model for user tiers.
474+ │ │ ├── token_blacklist.py # ORM model for token blacklist.
475+ │ │ └── user.py # ORM model for users.
473476 │ │
474- │ ├── schemas # Pydantic schemas for data validation.
477+ │ ├── schemas # Pydantic schemas for data validation.
475478 │ │ ├── __init__.py
476- │ │ ├── job.py # Schemas related to background jobs.
477- │ │ ├── post.py # Schemas related to posts.
478- │ │ ├── rate_limit.py # Schemas for rate limiting configurations.
479- │ │ ├── tier.py # Schemas for user tiers.
480- │ │ └── user.py # Schemas related to users.
479+ │ │ ├── job.py # Schemas related to background jobs.
480+ │ │ ├── post.py # Schemas related to posts.
481+ │ │ ├── rate_limit.py # Schemas for rate limiting configurations.
482+ │ │ ├── tier.py # Schemas for user tiers.
483+ │ │ ├── token_blacklist.py # Schemas for token blacklist.
484+ │ │ └── user.py # Schemas related to users.
481485 │ │
482- │ └── logs # Directory for log files.
483- │ └── app.log # Application log file.
486+ │ └── logs # Directory for log files.
487+ │ └── app.log # Application log file.
484488 │
485- ├── migrations # Directory for Alembic migrations.
486- │ ├── README # General info and guidelines for migrations.
487- │ ├── env.py # Environment configurations for Alembic.
488- │ ├── script.py.mako # Template script for migration generation.
489+ ├── migrations # Directory for Alembic migrations.
490+ │ ├── README # General info and guidelines for migrations.
491+ │ ├── env.py # Environment configurations for Alembic.
492+ │ ├── script.py.mako # Template script for migration generation.
489493 │ │
490- │ └── versions # Folder containing individual migration scripts.
494+ │ └── versions # Folder containing individual migration scripts.
491495 │ └── README.MD
492496 │
493- ├── scripts # Utility scripts for the project.
497+ ├── scripts # Utility scripts for the project.
494498 │ ├── __init__.py
495- │ ├── create_first_superuser.py # Script to create the first superuser in the application.
496- │ └── create_first_tier.py # Script to create the first user tier in the application.
499+ │ ├── create_first_superuser.py # Script to create the first superuser in the application.
500+ │ └── create_first_tier.py # Script to create the first user tier in the application.
497501 │
498- └── tests # Directory containing all the tests.
499- ├── __init__.py # Initialization file for the tests package.
500- ├── conftest.py # Configuration and fixtures for pytest.
501- ├── helper.py # Helper functions for writing tests.
502- └── test_user.py # Tests related to the user model and endpoints.
502+ └── tests # Directory containing all the tests.
503+ ├── __init__.py # Initialization file for the tests package.
504+ ├── conftest.py # Configuration and fixtures for pytest.
505+ ├── helper.py # Helper functions for writing tests.
506+ └── test_user.py # Tests related to the user model and endpoints.
503507```
504508
505509### 5.2 Database Model
506- Create the new entities and relationships and add them to the model
507- ![ diagram] ( https://user-images.githubusercontent.com/43156212/282272311-c7a36e26-dcd0-42cf-939d-6434b5579f29.png )
510+ Create the new entities and relationships and add them to the model <br >
511+ ![ diagram] ( https://user-images.githubusercontent.com/43156212/284426387-bdafc637-0473-4b71-890d-29e79da288cf.png )
512+
513+ #### 5.2.1 Token Blacklist
514+ Note that this table is used to blacklist the ` JWT ` tokens (it's how you log a user out) <br >
515+ ![ diagram] ( https://user-images.githubusercontent.com/43156212/284426382-b2f3c0ca-b8ea-4f20-b47e-de1bad2ca283.png )
508516
509517### 5.3 SQLAlchemy Models
510518Inside ` app/models ` , create a new ` entity.py ` for each new entity (replacing entity with the name) and define the attributes according to [ SQLAlchemy 2.0 standards] ( https://docs.sqlalchemy.org/en/20/orm/mapping_styles.html#orm-mapping-styles ) :
0 commit comments