Advertisement
Guest User

Untitled

a guest
Feb 7th, 2024
130
0
Never
2
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.91 KB | Software | 0 0
  1. mre.py
  2. ```
  3. from fastapi import FastAPI
  4. from fastapi.middleware.cors import CORSMiddleware
  5. from db import db
  6.  
  7. app = FastAPI()
  8. app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"])
  9.  
  10. @app.middleware("http")
  11. async def add_process_time_header(request, call_next):
  12.     if db.is_closed():
  13.         db.connect()
  14.  
  15.     response = await call_next(request)
  16.  
  17.     return response
  18.  
  19. @app.get("/")
  20. async def root():
  21.     return {"message": f"Hello World!"}
  22. ```
  23.  
  24. db.py
  25. ```
  26. import os
  27. from dotenv import load_dotenv
  28. from peewee import MySQLDatabase
  29.  
  30. load_dotenv()
  31.  
  32. username = os.getenv('USERNAME')
  33. password = os.getenv('PASSWORD')
  34. hostname = os.getenv('HOSTNAME')
  35. database = os.getenv('DATABASE')
  36.  
  37. DATABASE_URL = "mysql://{username}:{password}@{host}/{db_name}"
  38.  
  39. db = MySQLDatabase(
  40.     database, user=username,
  41.     password=password, host=hostname, port=3306
  42. )
  43. ```
Advertisement
Comments
  • NFeruch
    1 year
    # text 0.29 KB | 0 0
    1. Here is the Dockerfile I used to test just this minimum reproducible example
    2.  
    3. ```
    4. FROM python:3.9
    5.  
    6. WORKDIR /app
    7.  
    8. COPY requirements.txt .
    9.  
    10. RUN pip install --no-cache-dir --upgrade -r requirements.txt
    11.  
    12. COPY . .
    13.  
    14. CMD ["uvicorn", "mre:app", "--host", "0.0.0.0", "--port", "8000"]
    15. ```
  • NFeruch
    1 year
    # Python 0.09 KB | 0 0
    1. if I comment out the below, then it just works (???)
    2. ```
    3. if db.is_closed():
    4.     db.connect()
    5. ```
Add Comment
Please, Sign In to add comment
Advertisement