Skip to content

maksutovnurda/fastapi_init

Repository files navigation

FastAPI Application

A modern, production-ready FastAPI application with PostgreSQL, Redis, Elasticsearch, and Docker support.

🚀 Features

  • FastAPI - Modern, fast web framework for building APIs
  • PostgreSQL - Primary database with async support via SQLAlchemy
  • Redis - Caching and session storage
  • Elasticsearch - Search and analytics
  • Alembic - Database migrations
  • Docker - Containerized deployment
  • Poetry - Dependency management
  • Sentry - Error tracking and monitoring
  • CORS - Cross-origin resource sharing
  • Session Management - Secure session handling

📋 Prerequisites

  • Docker & Docker Compose
  • Python 3.11+ (for local development)
  • Poetry (for dependency management)

🛠️ Quick Start

Using Docker (Recommended)

  1. Clone the repository

    git clone <repository-url>
    cd fastapi-init
  2. Set up environment variables

    cp .env.example .env
    # Edit .env with your configuration
  3. Start the application

    make start
  4. Access the application

Local Development

  1. Install dependencies

    poetry install
  2. Set up environment variables

    cp .env.example .env
    # Configure your local database and Redis settings
  3. Run the application

    poetry run uvicorn app.main:app --reload --host 0.0.0.0 --port 8081

🗃️ Database

Migrations

Create a new migration:

make migrate "Your migration message"

Apply migrations:

make migration

🐳 Docker Commands

Command Description
make start Start all services
make stop Stop all services
make restart Restart all services
make build Build Docker images
make deploy Deploy to Docker Swarm

🧪 Development

Code Quality

Format code:

make format

Lint code:

make lint

Project Structure

app/
├── alembic/          # Database migrations
├── core/             # Core functionality
│   ├── cache.py      # Redis cache management
│   ├── config.py     # Application settings
│   ├── db.py         # Database configuration
│   ├── elastic.py    # Elasticsearch setup
│   ├── logger.py     # Logging configuration
│   └── middleware.py # Custom middleware
├── models/           # SQLAlchemy models
├── routers/          # API routes
├── services/         # Business logic
├── utils/            # Utility functions
└── main.py           # Application entry point

🔧 Configuration

The application uses environment variables for configuration. Key settings include:

  • PROJECT_NAME - Application name
  • DOMAIN - Domain for the application
  • SESSION_SECRET_KEY - Secret key for sessions
  • ENVIRONMENT - Environment (local/staging/production)
  • Database credentials (PostgreSQL)
  • Redis configuration
  • Sentry DSN (optional)

📊 Monitoring

  • Health Check: /ping endpoint for service health
  • Process Time: Middleware adds X-Process-Time header
  • Sentry: Automatic error tracking (in non-local environments)
  • Logging: Structured logging with configurable levels

🚀 Deployment

Docker Swarm

  1. Initialize swarm:

    make swarm-init
  2. Deploy:

    make deploy

Production Considerations

  • Set ENVIRONMENT=production
  • Configure proper SENTRY_DSN
  • Use secure SESSION_SECRET_KEY
  • Set up proper database backups
  • Configure reverse proxy (nginx/traefik)

📝 API Documentation

Once running, access the interactive API documentation at:

🤝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Run tests and linting
  5. Submit a pull request

📄 License

This project is licensed under the MIT License.

👨‍💻 Author

Nurdaulet Maxutov


For more information, check the API documentation at /docs when the application is running.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published