-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathmain.py
More file actions
46 lines (36 loc) · 1.42 KB
/
main.py
File metadata and controls
46 lines (36 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import os
from pathlib import Path
import uvicorn
from fastapi import FastAPI, Depends, HTTPException, Request, status
from fastapi.responses import JSONResponse, HTMLResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from sqlalchemy import text
from sqlalchemy.orm import Session
from conf.db import get_db
app = FastAPI()
BASE_DIR = Path(__file__).parent
directory = BASE_DIR.joinpath("static")
app.mount("/static", StaticFiles(directory=directory), name="static")
templates = Jinja2Templates(directory=BASE_DIR / "templates")
@app.get("/", response_class=HTMLResponse)
def index(request: Request):
return templates.TemplateResponse(
"index.html", {"request": request, "our": "Build group WebPython #16"}
)
@app.get("/healthchecker")
def healthchecker(db: Session = Depends(get_db)):
try:
# Make request
result = db.execute(text("SELECT 1"))
result = result.fetchone()
if result is None:
raise HTTPException(
status_code=500, detail="Database is not configured correctly"
)
return {"message": "Welcome to FastAPI!"}
except Exception as e:
print(e)
raise HTTPException(status_code=500, detail="Error connecting to the database")
if __name__ == "__main__":
uvicorn.run("main:app", host="0.0.0.0", port=int(os.environ.get("PORT", 8000)), log_level="info", reload=True)