Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- mre.py
- ```
- from fastapi import FastAPI
- from fastapi.middleware.cors import CORSMiddleware
- from db import db
- app = FastAPI()
- app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"])
- @app.middleware("http")
- async def add_process_time_header(request, call_next):
- if db.is_closed():
- db.connect()
- response = await call_next(request)
- return response
- @app.get("/")
- async def root():
- return {"message": f"Hello World!"}
- ```
- db.py
- ```
- import os
- from dotenv import load_dotenv
- from peewee import MySQLDatabase
- load_dotenv()
- username = os.getenv('USERNAME')
- password = os.getenv('PASSWORD')
- hostname = os.getenv('HOSTNAME')
- database = os.getenv('DATABASE')
- DATABASE_URL = "mysql://{username}:{password}@{host}/{db_name}"
- db = MySQLDatabase(
- database, user=username,
- password=password, host=hostname, port=3306
- )
- ```
Advertisement
Comments
-
- Here is the Dockerfile I used to test just this minimum reproducible example
- ```
- FROM python:3.9
- WORKDIR /app
- COPY requirements.txt .
- RUN pip install --no-cache-dir --upgrade -r requirements.txt
- COPY . .
- CMD ["uvicorn", "mre:app", "--host", "0.0.0.0", "--port", "8000"]
- ```
-
- if I comment out the below, then it just works (???)
- ```
- if db.is_closed():
- db.connect()
- ```
Add Comment
Please, Sign In to add comment
Advertisement