[core] Add cors_allow_origin and production config option

This commit is contained in:
Cathy Hu 2020-07-25 13:34:18 +02:00
parent 915cc041e3
commit cb9e8e0136
2 changed files with 11 additions and 0 deletions

View file

@ -26,6 +26,8 @@ config = {
'database_connection': 'sqlite:////tmp/kibicara.sqlite', 'database_connection': 'sqlite:////tmp/kibicara.sqlite',
'frontend_path': None, 'frontend_path': None,
'root_url': 'http://localhost:8000', 'root_url': 'http://localhost:8000',
'production': True,
'cors_allow_origin': 'http://localhost:4200',
} }
""" Default configuration. """ Default configuration.

View file

@ -7,6 +7,7 @@
from asyncio import run as asyncio_run from asyncio import run as asyncio_run
from fastapi import FastAPI from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles from fastapi.staticfiles import StaticFiles
from hypercorn.config import Config from hypercorn.config import Config
from hypercorn.asyncio import serve from hypercorn.asyncio import serve
@ -57,6 +58,14 @@ class Main:
server_config = Config() server_config = Config()
server_config.accesslog = '-' server_config.accesslog = '-'
app.include_router(router, prefix='/api') app.include_router(router, prefix='/api')
if not config['production'] and config['cors_allow_origin']:
app.add_middleware(
CORSMiddleware,
allow_origins=config['cors_allow_origin'],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
if config['frontend_path'] is not None: if config['frontend_path'] is not None:
app.mount( app.mount(
'/', '/',