Modular features discovery system for FastAPI projects.
Project description
Fast-Features: A Modular Approach to FastAPI Development
fast-features is a powerful toolkit for FastAPI projects designed to accelerate development by providing scaffolding, feature generation, and automatic discovery of project components. It promotes a modular and organized project structure, allowing developers to focus on business logic rather than boilerplate code.
Why a Modular Structure?
As applications grow, maintaining a single, monolithic codebase becomes increasingly challenging. A modular, feature-based architecture offers several advantages:
- Scalability: By organizing your code into self-contained features, you can easily scale your application by adding, removing, or modifying features without affecting other parts of the system.
- Maintainability: A modular structure makes it easier to understand, debug, and test your code. Each feature has a clear responsibility, reducing cognitive load and simplifying maintenance.
- Separation of Concerns: By separating your application into distinct features, you enforce a clean separation of concerns, leading to more robust and reliable code.
- Team Collaboration: A modular architecture allows multiple developers to work on different features simultaneously with minimal conflicts, improving team productivity.
fast-features is designed to help you achieve these benefits by providing a solid foundation for building modular FastAPI applications.
Key Features
- Project Scaffolding: Kickstart your FastAPI project with a production-ready, modular structure in seconds. The
fastfeatures-scaffoldcommand generates a new project with a logical directory structure, including a core application setup and an emptyfeaturesdirectory, ready for you to start building. - Feature Generation: Accelerate your development workflow by generating new features with a single command. The
fastfeatures-featurecommand creates a new feature with a predefined structure, including models, services, and routes, so you can focus on implementing the business logic. - Automatic Settings Generation: Simplify your application's configuration with automatic settings generation. The
fastfeatures-settingscommand generates a Pydanticsettings.pyfile from your.envfile, with support for nested settings, providing a type-safe and organized way to manage your application's configuration. - Automatic Route Discovery:
fast-featuresautomatically discovers and includesAPIRouterinstances from your features, so you don't have to manually wire up your routes. This reduces boilerplate code and ensures that your routes are always up-to-date. - Automatic Model Discovery:
fast-featuresautomatically discovers yourSQLModelandSQLAlchemymodels, making it easy to work with your database and ensuring that your models are always available when you need them.
Installation
poetry add fast-features
Getting Started
-
Create a new project:
fastfeatures-scaffold
-
Generate a new feature:
fastfeatures-feature
-
Enable route discovery in
app/main.py:from fastfeatures.core.routes_discoverer import add_features_routes from app import features # ... (existing app setup) add_features_routes(app, features)
-
Run your application:
uvicorn main:app --reload
Usage
Project Scaffolding
To create a new FastAPI project, use the fastfeatures-scaffold command. This command will prompt you for the project name and description.
fastfeatures-scaffold
This will create a new project scaffold in the current directory with the following structure:
<PROJECT_NAME>/
├── .env
├── main.py
└── app/
├── __init__.py
├── core/
│ ├── __init__.py
│ ├── settings.py
│ └── lib/
│ ├── __init__.py
│ └── database.py
├── features/
│ └── __init__.py
└── main.py
Feature Generation
To generate a new feature, use the fastfeatures-feature command. This command will prompt you for the feature name.
fastfeatures-feature
This will create a new feature directory inside app/features with the following structure:
app/features/<feature_name>/
├── __init__.py
├── models/
│ ├── __init__.py
│ └── <feature_name>.py
├── routes.py
└── services/
├── __init__.py
└── <feature_name>_services.py
Settings Generation
To generate a settings.py file from your .env file, use the fastfeatures-settings command.
fastfeatures-settings --env-file=.env --output-path=app/core/settings.py
This will generate a settings.py file with nested Pydantic models. For example:
# THIS FILE IS AUTO-GENERATED...
from pydantic_settings import BaseSettings, SettingsConfigDict
class Settings(BaseSettings):
DATABASE_URL: str
DEV_MODE: bool = False
model_config = SettingsConfigDict(
env_file=".env",
env_file_encoding="utf-8",
extra="ignore",
)
settings = Settings()
Route Discovery
To automatically discover and include all the APIRouter instances from your features, add the following to your app/main.py:
from fastfeatures.core.routes_discoverer import add_features_routes
from app import features
# ... (existing app setup)
add_features_routes(app, features)
Model Discovery
To automatically discover all your SQLModel and SQLAlchemy models, you can use the get_sql_models function. This is particularly useful for database migrations with Alembic.
from fastfeatures.core.models_discoverer import get_sql_models
from app import features
# Discover all SQL models from the features module
sql_models = get_sql_models(features)
The "Features" Concept
A "feature" is a self-contained unit of functionality that encapsulates a specific part of your application's domain. Each feature has its own models, services, and routes, promoting a clean separation of concerns and making your code easier to understand and maintain.
models: This directory contains the data models for your feature, defined usingSQLModelorSQLAlchemy.routes: This directory contains the API routes for your feature, defined usingFastAPI'sAPIRouter.services: This directory contains the business logic for your feature, which is used by the routes to interact with the models and perform actions.
By organizing your code into features, you can build complex applications in a more structured and maintainable way.
Alembic Integration
To use Alembic with fast-features, you need to configure your migrations/env.py file to automatically discover your models. Add the following code to your migrations/env.py file, just after the original config = context.config line:
#### BEGIN OF CUSTOM CODE ####
from sqlmodel import SQLModel
from app.core.settings import Settings
from fastfeatures import get_sql_models
from app import features
get_sql_models(features)
config.set_main_option("sqlalchemy.url", Settings.DATABASE_URL)
#### END OF CUSTOM CODE ####
License
This project is licensed under the MIT License. See the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastfeatures-0.1.2.tar.gz.
File metadata
- Download URL: fastfeatures-0.1.2.tar.gz
- Upload date:
- Size: 14.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.2.1 CPython/3.10.12 Linux/6.8.0-87-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4bf229dae93bb5755dc7cce5753451006892708d27b8d3f6a13d2b09ec6b105c
|
|
| MD5 |
3873983961240592747690f8fe1d787e
|
|
| BLAKE2b-256 |
0a4f95b5ff85d4f54634fa95cbf39e291915e0c434210a96cd7e792788d1f433
|
File details
Details for the file fastfeatures-0.1.2-py3-none-any.whl.
File metadata
- Download URL: fastfeatures-0.1.2-py3-none-any.whl
- Upload date:
- Size: 17.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.2.1 CPython/3.10.12 Linux/6.8.0-87-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ccd4bea54aff86cef14ae5cf4569dad2cae259ec5d91dda22abe75d95dfb8acc
|
|
| MD5 |
d13cfa91f473f3a52e10959246bbb535
|
|
| BLAKE2b-256 |
0e09a5b3ee67a2fa8fa54f7bd906599d3d96afa27977586da1701284d13d0a68
|