Foxhole Backend API is a comprehensive starter kit for building robust Python-based API systems with real-time communication capabilities. It provides a solid foundation with integrated logging, storage, caching, database management, WebSocket support, and a fully-featured CLI. This project aims to accelerate the development of scalable and maintainable backend services.
- FastAPI web framework for high-performance API development
- WebSocket support for real-time communication
- Asynchronous SQLite database with SQLAlchemy ORM
- Redis caching for improved performance
- MinIO integration for object storage
- JWT-based authentication with unique user secrets
- Structured logging
- Global error handling
- Docker and docker-compose setup for easy deployment
- Comprehensive testing framework
- Command-line interface (CLI) for easy interaction with the API
foxhole/
├── app/
│ ├── core/
│ │ └── config.py
│ ├── routers/
│ │ ├── auth.py
│ │ ├── files.py
│ │ ├── health.py
│ │ └── websocket.py
│ ├── schemas/
│ │ └── user_schema.py
│ ├── services/
│ │ ├── storage_service.py
│ │ └── websocket_manager.py
│ ├── app.py
│ ├── dependencies.py
│ ├── exceptions.py
│ └── models.py
├── cli/
│ ├── commands.py
│ ├── config.py
│ ├── main.py
│ ├── session_manager.py
│ ├── ui.py
│ └── websocket_client.py
├── tests/
│ ├── conftest.py
│ └── test_*.py
├── utils/
│ ├── cache.py
│ ├── database.py
│ ├── error_handlers.py
│ ├── logging.py
│ ├── security.py
│ └── token.py
├── main.py
├── Dockerfile
├── docker-compose.yml
└── requirements.txt
Our code organization follows a modular and separation-of-concerns approach:
- app/: Contains the core application logic
- core/: Central configurations and settings
- routers/: API route definitions, separated by functionality
- schemas/: Pydantic models for request/response validation
- services/: Business logic and external service integrations
- cli/: Command-line interface for interacting with the API
- tests/: Holds all test files, mirroring the structure of the
app/
directory - utils/: Utility functions and helpers used across the application
This structure promotes:
- Modularity: Each component has a clear responsibility
- Scalability: Easy to add new features or modify existing ones
- Maintainability: Clear separation of concerns makes the codebase easier to understand and maintain
- Testability: Organized structure facilitates comprehensive testing
-
Main Application (
main.py
):- Entry point of the application
- Sets up FastAPI app, routers, and lifecycle management
-
Core Application (
app/app.py
):- Defines the main FastAPI application
- Includes routers and dependency injections
-
Configuration (
app/core/config.py
):- Manages application settings using Pydantic
-
Routers:
auth.py
: Handles authentication and user-related endpointshealth.py
: Provides a health check endpointwebsocket.py
: Manages WebSocket connectionsfiles.py
: Handles file upload and retrieval
-
Database Management (
utils/database.py
):- Sets up SQLAlchemy for async database operations
-
Caching (
utils/cache.py
):- Implements Redis caching functionality
-
Models (
app/models.py
):- Defines SQLAlchemy ORM models for the application
-
Services:
websocket_manager.py
: Manages WebSocket connectionsstorage_service.py
: Handles file storage operations
-
Dependencies (
app/dependencies.py
):- Defines dependency injection for services
-
Error Handling (
utils/error_handlers.py
):- Sets up global error handlers for the application
-
Logging (
utils/logging.py
):- Configures structured logging for the application
-
Testing (
tests/
):- Contains unit tests and test configurations
-
CLI (
cli/
):- Provides a command-line interface for interacting with the API
- Includes session management, WebSocket client, and user interface components
We've implemented a unique approach to user identification:
- Users are identified by a randomly generated userID
- The userID serves as both the identifier and the access key for authentication
- No traditional username/password combination is used
- Users can optionally set a screen name (handle) for display purposes
- The user model is easily extensible for additional fields
This approach simplifies user management and enhances security by eliminating the need for password storage and management.
The project leverages FastAPI's asynchronous capabilities, coupled with SQLAlchemy's async support, to provide high-performance, non-blocking operations throughout the application.
The application is designed with a modular service architecture, allowing easy extension and modification of core functionalities like storage (currently using MinIO) and caching (using Redis).
The project includes a robust testing framework using pytest, with fixtures for database and application setup, ensuring high code quality and reliability.
The CLI provides a user-friendly interface to interact with the API, supporting all major functionalities including user management, messaging, and file operations.
- Clone the repository
- Install dependencies:
pip install -r requirements.txt
- Set up environment variables or use default values in
app/core/config.py
- Run the application:
python main.py
- Access the API documentation at
http://localhost:8000/docs
- For CLI usage, run:
python -m cli.main
To run the application using Docker:
-
Build the Docker image:
docker build -t foxhole-backend .
-
Run the container:
docker run -p 8000:8000 foxhole-backend
The application will be available at http://localhost:8000
- For local development, use
uvicorn main:app --reload
- For deployment, utilize the provided Dockerfile and docker-compose.yml
- Customize the deployment process using Kamal (configuration to be added)
We welcome contributions to the Foxhole Backend API project! Here are some guidelines to help you get started:
- Fork the repository and create your branch from
main
. - If you've added code that should be tested, add tests.
- Ensure the test suite passes.
- Make sure your code lints.
- Issue that pull request!
Please refer to the GitHub Flow for more details on the contribution process.
This project is licensed under the MIT License. See the LICENSE file for details.