Giter VIP home page Giter VIP logo

fastapi-boilerplate's Introduction

fastAPI Biolerplate

A FastAPI project!

  • Every endpoint should be accessed by an API key
  • API key has roles, such a way that, access to appropriate endpoints is allowed or denied
  • Multiple endpoint project
  • User management to create new users, disable users, verify email accounts and then allow api access, etc...
  • Connects to MongoDB


A Rust version can be found here.


Requirements

  • fastAPI
  • Python 3.7+

Lib Requirements

  • UVLoop
  • re2

Database

  • MongoDB

Setting up

mkvirtualenv --python=/usr/bin/python3 fastapi-boilerplate
cd /path/to/project
setvirtualenvproject

Installing the requirements

pip install pipenv
pipenv install

OR

pip install -r requirements.txt

Creating the first user

$ ./first_user.py [email protected]
Your API Key is: _hplIyYGpeVKc... (Do not loose this key as its not stored in the system)

Running the Project in Dev env

uvicorn server.main:app --reload

Project Structure

api/endpoints - handlers for all endpoint routes
core          - general components like settings, security, key validation, etc...
db            - db connection specific
db/crud       - CRUD for types from models
models        - pydantic models that used in crud or handlers

Docs

Accessing the "user" endpoint with non superuser account

$ curl -H'x-api-key: -OWN3pNZ6FsaPppPqsyeuF6sxe' -H'x-email-id: [email protected]' http://127.0.0.1:8000/api/user/ 

{
    "created_at": "2019-11-01T03:21:02.868000",
    "updated_at": "2019-11-06T03:22:01.045000",
    "email": "[email protected]"
}

Accessing the "user" endpoint with superuser account

$ curl -H'x-api-key: -OWN3pNZ6FsaPppPqsyeuF6sxe' -H'x-email-id: [email protected]' http://127.0.0.1:8000/api/user/ 
{
    "total_users": 1,
    "users": [
        {
            "created_at": "2019-11-01T03:21:02.868000",
            "updated_at": "2019-11-06T03:22:01.045000",
            "email": "[email protected]",
            "endpoint_access": [
                "user"
            ],
            "is_superuser": true,
            "is_active": true,
            "disabled": false,
            "hashed_api_key": "$2b$12$DJ2D249xR4MY.aeTODeq5OasBR8TUoxknjxX6WqLf1QoG1k7VWQim",
            "salt": "$2b$12$lJ937UATRjcCU4.kk0HYZ."
        }
    ]
}

Accessing the "hello" endpoint

$ curl -H'x-api-key: -OWN3pNZ6FsaPppPqsyeuF6sxe' -H'x-email-id: [email protected]' http://127.0.0.1:8000/api/hello/ 

"Hello World"

Verify an email account of an added API user account

$ curl http://127.0.0.1:8000/api/user/[email protected]

{
    "detail": "[email protected] is now verified! You can start using your API key."
}

Disabling an email account

$ curl  -H'x-api-key: -OWN3pNZ6FsaPppPqsyeuF6sxe' -H'x-email-id: [email protected]' http://localhost:8000/api/user/[email protected]

{
    "detail": "[email protected] is disabled!"
}

Initial Author: Sriram

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.