This project is a robust Blog API built with FastAPI, providing a full-featured backend for managing blog posts. It includes features such as creating, reading, updating, and deleting blog posts, along with search functionality and sorting options.
- CRUD operations for blog posts
- Asynchronous database operations with SQLAlchemy
- Database migrations using Alembic
- Search functionality for posts
- Sorting and filtering options
- Automatic slug generation for posts
- Seeding functionality for development and testing
- FastAPI
- SQLAlchemy (Async)
- Alembic
- Pydantic
- PostgreSQL
- Python 3.9+
The project follows a modular structure:
app/
: Main application directorycontrollers/
: Business logiclib/
: Utility functions and configurationsmodels/
: Database models and Pydantic schemasrouters/
: API route definitions
alembic/
: Database migration scripts
- Python 3.9+
- PostgreSQL
-
Clone the repository:
git clone <repository-url> cd <project-directory>
-
Create and activate a virtual environment:
python -m venv venv source venv/bin/activate # On Windows, use `venv\Scripts\activate`
-
Install dependencies:
pip install -r requirements.txt
-
Set up environment variables: Create a
.env
file in the root directory and add the following:APP_NAME=YourAppName DB_URL=postgresql+asyncpg://user:password@localhost/dbname
-
Run database migrations:
alembic upgrade head
To start the FastAPI server:
uvicorn app.main:app --reload
The API will be available at http://localhost:8000
.
GET /blog/
: Get all blog postsGET /blog/{id}
: Get a specific blog postPOST /blog/
: Create a new blog postPUT /blog/{id}
: Update a blog postDELETE /blog/{id}
: Delete a blog postGET /blog/seed
: Seed the database with sample posts
For detailed API documentation, visit http://localhost:8000/docs
or http://localhost:8000/redoc
after starting the server.
To create a new migration:
alembic revision --autogenerate -m "Description of the change"
To apply migrations:
alembic upgrade head
- Models are defined in
app/models/
- Business logic is in
app/controllers/
- API routes are defined in
app/routers/
- Database configurations and sessions are managed in
app/lib/db.py
- Create or update models in
app/models/
- Implement business logic in
app/controllers/
- Define new routes in
app/routers/
- Update database models and run migrations if necessary
Contributions are welcome! Please feel free to submit a Pull Request.