EvaluationEfficiencyOptimiz.../server/main.py

39 lines
877 B
Python
Raw Normal View History

2024-10-14 17:06:49 +04:00
from fastapi import FastAPI, Depends
from fastapi.responses import RedirectResponse
from sqlalchemy.orm import Session
from sqlalchemy.future import select
from database import SessionLocal, User, Base, engine
Base.metadata.create_all(bind=engine)
app = FastAPI()
2024-10-14 17:06:49 +04:00
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
@app.get("/hello")
async def hello():
return {"message": "Hello, World!"}
@app.get("/")
2024-10-14 17:06:49 +04:00
async def redirect_to_docs():
return RedirectResponse(url="/docs")
2024-10-14 17:06:49 +04:00
@app.get("/users")
def read_users(db: Session = Depends(get_db)):
result = db.execute(select(User))
users = result.scalars().all() # Получаем всех пользователей
return users
2024-10-14 17:06:49 +04:00
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="localhost", port=8080) # Изменено на localhost