Giter Site home page Giter Site logo

wmramadan / fastapi-boilerplate Goto Github PK

View Code? Open in Web Editor NEW
35.0 3.0 5.0 124 KB

FastAPI REST API pre-configured with a database. This will get you up and running with CRUD operations quickly.

License: GNU General Public License v3.0

Python 97.68% Dockerfile 1.00% Shell 1.32%
boilerplate fastapi python rest-api pydantic sqlalchemy docker docker-compose pylint loguru

fastapi-boilerplate's Introduction

FastAPI image

Test Supported Platforms Supported Python versions

FastAPI Boilerplate

FastAPI REST API pre-configured with a database. This will get you up and running with CRUD operations quickly. Use this starter, boilerplate for all your new FastAPI projects.

Requirements

  • Python3
  • python3-virtualenv
  • python3-pip
  • Docker
  • Docker-compose

Features

  • SQLAlchemy
  • Pydantic
  • Docker
  • Logging
  • Celery
  • Tests
  • Config
  • SSE
  • PyMongo

ToDo

File Structure

.
├── api
│   ├── __init__.py
│   ├── collections.py
│   ├── config.py
│   ├── database.py
│   ├── main.py
|   ├── worker.py
│   └── models
│   │   ├── __init__.py
│   │   ├── item_model.py
│   │   └── task_model.py
│   │   └── user_model.py
│   └── schemas
│   │   ├── __init__.py
│   │   ├── items_schema.py
│   │   ├── questions_schema.py
│   │   └── tasks_schema.py
│   │   └── users_schema.py
│   └── routers
│   │   ├── __init__.py
│   │   ├── async_router.py
│   │   ├── items.py
│   │   ├── questions.py
│   │   ├── stream.py
│   │   ├── tasks.py
│   │   └── users.py
│   └── helpers
│   │   ├── __init__.py
│   │   ├── async_helper.py
│   │   └── crud.py
│   └── tests
│   │   ├── __init__.py
│   │   ├── db.py
│   │   └── test_main.py
├── docker
│   └── Dockerfile

Environment Variables

  • SQLALCHEMY_DATABASE_URL - Database URL used, can be either SQLite or PostgreSQL.
  • MONGODB_URL - Database URL for MongoDB server.
  • MONGODB_NAME - Name used for MongoDB Database.
  • CELERY_CONF_BROKER_URL - Celery redis broker URL.
  • CELERY_CONF_RESULT_BACKEND - Celery redis result backend.
  • ALLOWED_ORIGINS - A list of origins that should be permitted to make cross-origin requests.
  • ALLOW_CREDENTIALS - Allowed credentials bool for CORS middelware.
  • ALLOW_METHODS - Allowed methods ['GET','POST','PUT','PATCH','DELETE','OPTIONS'] for CORS middleware.
  • ALLOW_HEADERS - A list of HTTP request headers that should be supported for cross-origin requests.
  • APP_DEBUG - Set app debug mode bool value.

Updating Changelog

You can run git-changelog.sh bash script to update the CHANGELOG file using the following command:

bash git-changelog.sh > CHANGELOG.md

Linting

You can run pylint with the following command inside the fastapi-boilerplate directory:

pylint --recursive=y api

Running Tests

You can run pytest with the following command inside the fastapi-boilerplate directory:

pytest api/

Quick Start (Local)

  1. Clone the repo:

    git clone https://github.com/WMRamadan/fastapi-boilerplate
    cd fastapi-boilerplate
  2. Initialize and activate a virtual environment:

    virtualenv env
    source env/bin/activate
  3. Install dependencies:

    pip3 install -r requirements.txt
  4. Run redis service required for celery worker:

    docker-compose -f docker-compose-services.yml up -d
  5. Run celery worker:

    celery --app=api.worker.celery worker --loglevel=info --logfile=celery.log
  6. Run the development server:

    uvicorn api.main:app --reload
  7. View the API docs:

    http://localhost:8000/docs
    # OR
    http://localhost:8000/redoc

Quick Start (Docker)

  1. Clone the repo:

    git clone https://github.com/WMRamadan/fastapi-boilerplate
    cd fastapi-boilerplate
  2. Build:

    docker-compose build
  3. Run the app:

    docker-compose up
  4. View the API docs:

    http://localhost/docs
    # OR
    http://localhost/redoc

Contribution

Please check our Contributing Guide on how you can contribute.

fastapi-boilerplate's People

Contributors

wmramadan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

fastapi-boilerplate's Issues

Add SSE Support

Initial checklist

Problem

Provide a stream of messages that updates from server side events.

Solution

Create SSE end-point which will update messages without refreshing of the end-point.

Alternatives

None

Alembic Implementation

Initial checklist

Problem

Alembic has not been implemented so there is no way to run migrations.

Solution

Implement Alembic with documentation on how to apply migrations

Alternatives

None

Requirements: Lock package versions

Initial checklist

Problem

It is probably best to lock the packages in the requirements files to a specific version. No locking to specific version numbers can cause inconsistencies and incompatibility in the future.

Solution

Lock packages to specific versions that have been tested. With each boilerplate version iterations the version number may be upgraded after testing.

Alternatives

Testing can be done across different version but this will take too long in CI.

Test Structure

Initial checklist

Problem

Tests are currently not separated

Solution

Create separate tests for:

  • main
  • users
  • items
  • tasks

Alternatives

None

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.