39 lines
877 B
Python
39 lines
877 B
Python
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()
|
|
|
|
|
|
def get_db():
|
|
db = SessionLocal()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close()
|
|
|
|
|
|
@app.get("/hello")
|
|
async def hello():
|
|
return {"message": "Hello, World!"}
|
|
|
|
|
|
@app.get("/")
|
|
async def redirect_to_docs():
|
|
return RedirectResponse(url="/docs")
|
|
|
|
@app.get("/users")
|
|
def read_users(db: Session = Depends(get_db)):
|
|
result = db.execute(select(User))
|
|
users = result.scalars().all() # Получаем всех пользователей
|
|
return users
|
|
|
|
if __name__ == "__main__":
|
|
import uvicorn
|
|
uvicorn.run(app, host="localhost", port=8080) # Изменено на localhost
|