Code Monkey home page Code Monkey logo

fastapi-cors's Introduction

fastapi-cors

A simply scoped abstraction to provide CORS settings via environment variables to a Fastapi application.

Usage

from fastapi import FastAPI

# during this next line `fastapi_cors.env` will read environment variables from .env
from fastapi_cors import CORS 

app = FastAPI()

CORS(app)

Opinions

A health check route is optionally added that displays these (but not other) environment variables.

If you want to disable it, use the code below

from fastapi import FastAPI

# during this next line `fastapi_cors.env` will read environment variables from .env
from fastapi_cors import CORS 

app = FastAPI()

CORS(app, include_health_check=False)

Config

Configure FastAPI as usual. Extra arguments (that can be accessed from app.extra):

Name Default Description
HOST 0.0.0.0 Displayed in the Swagger title, with app.title.
PORT 8000 Where to mount the static directory. Disabled if value is falsy.
LOG_LEVEL info log level.
ALLOW_ORIGINS ["http://localhost","http://localhost:3000"] A list of origins that should be permitted to make cross-origin requests. E.g. ['https://example.org', 'https://www.example.org']. You can use ['*'] to allow any origin. These are the URLs clients can make requests from
ALLOWED_CREDENTIALS True Indicate that cookies should be supported for cross-origin requests. Also, allow_origins cannot be set to ['*'] for credentials to be allowed, origins must be specified.
ALLOWED_METHODS ["*"] A list of HTTP methods that should be allowed for cross-origin requests. Defaults to ['*'] to allow all standard methods. You can use ['GET'] to reduce the list.
ALLOWED_HEADERS ["Access-Control-Allow-Origin"] A list of HTTP request headers that should be supported for cross-origin requests. You can use ['*'] to allow all headers. The Accept, Accept-Language, Content-Language and Content-Type headers are always allowed for simple CORS requests

⚠️ allow_origins default is ["http://localhost","http://localhost:3000"], not [] (80 -> docs, 3000 -> frontend)

⚠️ allow_methods default is ["*"], not ["GET"]

⚠️ allowed_credentials default is True, not False

See the FastAPI documentation on CORS for more information

Example Env

Values will be cast into a list of str, as appropriate.

HOST=0.0.0.0
PORT=8000
LOG_LEVEL=info
ALLOW_ORIGINS=http://localhost,http://localhost:3000
ALLOWED_CREDENTIALS=True
ALLOWED_METHODS=*
ALLOWED_HEADERS=Access-Control-Allow-Origin

Note, this is not required unless you are changing a default or want to declare them all

fastapi-cors's People

Contributors

iancleary avatar

Stargazers

 avatar

Watchers

 avatar  avatar

fastapi-cors's Issues

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.