Last active
December 31, 2020 19:46
-
-
Save avilior/58bebc110d50da5a6c070b5338b022f8 to your computer and use it in GitHub Desktop.
FastAPI main file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
from fastapi import FastAPI, Depends, HTTPException, Request | |
from fastapi.middleware.cors import CORSMiddleware # https://fastapi.tiangolo.com/tutorial/cors/ | |
from fastapi.staticfiles import StaticFiles # needs aiofiles to be installed | |
import logging | |
LOG = logging.getLogger(__name__) | |
app = FastAPI() | |
app.mount("/static", StaticFiles(directory="../static"), name="static") | |
origins = ["*"] | |
app.add_middleware( | |
CORSMiddleware, | |
allow_origins=origins, | |
allow_credentials=True, | |
allow_methods=["*"], | |
allow_headers=["*"], | |
) | |
@app.on_event("startup") | |
async def on_start_up(): | |
LOG.info("Start Up procedures...") | |
LOG.info("Start up procedures DONE") | |
@app.on_event("shutdown") | |
async def on_shutdown(): | |
LOG.info("Shutdown procedures...") | |
LOG.info("Shutdown procedures DONE") | |
if __name__ == '__main__': | |
import uvicorn | |
logging.basicConfig( | |
filename=None, | |
filemode="a", | |
format='%(asctime)-15s|%(levelname)-8s|%(filename)-25s|%(funcName)-15s|%(lineno)-4s|%(message)s', | |
level=logging.INFO | |
) | |
uvicorn.run(app, host="0.0.0.0", port=9000, log_level="info") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment