-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
53 lines (43 loc) · 1.36 KB
/
Copy pathmain.py
File metadata and controls
53 lines (43 loc) · 1.36 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
47
48
49
50
51
52
53
from fastapi import FastAPI
from fastapi.responses import JSONResponse
from fastapi import Depends
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String
from pydantic import BaseModel
# Define the database connection parameters
DATABASE_URL = "postgresql://user:password@localhost/dbname"
# Create the database engine
engine = create_engine(DATABASE_URL)
# Create a configured "Session" class
Session = sessionmaker(bind=engine)
# Create a base class for declarative class definitions
Base = declarative_base()
# Define a simple user model
class User(Base):
__tablename__ = "users"
id = Column(Integer, primary_key=True)
name = Column(String)
email = Column(String)
# Create the database tables
Base.metadata.create_all(engine)
# Define a Pydantic model for the user
class UserModel(BaseModel):
id: int
name: str
email: str
# Create the FastAPI app
app = FastAPI()
# Define a dependency to get a database session
def get_db():
db = Session()
try:
yield db
finally:
db.close()
# Define a route to get all users
@app.get("/users/")
def read_users(db: Session = Depends(get_db)):
users = db.query(User).all()
return JSONResponse(content=[user.name for user in users], media_type="application/json")