Spaces:
Runtime error
Runtime error
Upload 25 files
Browse files- .dockerignore +1 -0
- Dockerfile +20 -0
- app/__init__.py +0 -0
- app/__pycache__/__init__.cpython-311.pyc +0 -0
- app/__pycache__/__init__.cpython-312.pyc +0 -0
- app/__pycache__/database.cpython-311.pyc +0 -0
- app/__pycache__/database.cpython-312.pyc +0 -0
- app/__pycache__/main.cpython-312.pyc +0 -0
- app/__pycache__/models.cpython-311.pyc +0 -0
- app/__pycache__/models.cpython-312.pyc +0 -0
- app/__pycache__/schemas.cpython-311.pyc +0 -0
- app/__pycache__/schemas.cpython-312.pyc +0 -0
- app/check_conn.py +22 -0
- app/database.py +30 -0
- app/main.py +17 -0
- app/models.py +27 -0
- app/routes/__init__.py +1 -0
- app/routes/__pycache__/__init__.cpython-311.pyc +0 -0
- app/routes/__pycache__/__init__.cpython-312.pyc +0 -0
- app/routes/__pycache__/users.cpython-311.pyc +0 -0
- app/routes/__pycache__/users.cpython-312.pyc +0 -0
- app/routes/users.py +55 -0
- app/schemas.py +21 -0
- app/test.db +0 -0
- requirements.txt +0 -0
.dockerignore
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
venv/
|
Dockerfile
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Use the official Python image as the base image
|
2 |
+
FROM python:3.9
|
3 |
+
|
4 |
+
# Set the working directory in the container
|
5 |
+
WORKDIR /app
|
6 |
+
|
7 |
+
# Copy the project files to the container
|
8 |
+
COPY . /app
|
9 |
+
|
10 |
+
# Install the project dependencies
|
11 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
12 |
+
|
13 |
+
# Expose the port that your FastAPI app will run on
|
14 |
+
EXPOSE 8000
|
15 |
+
|
16 |
+
# Copy the .env file into the container
|
17 |
+
# COPY .env .env
|
18 |
+
|
19 |
+
# Start the FastAPI app
|
20 |
+
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
|
app/__init__.py
ADDED
File without changes
|
app/__pycache__/__init__.cpython-311.pyc
ADDED
Binary file (172 Bytes). View file
|
|
app/__pycache__/__init__.cpython-312.pyc
ADDED
Binary file (127 Bytes). View file
|
|
app/__pycache__/database.cpython-311.pyc
ADDED
Binary file (1.05 kB). View file
|
|
app/__pycache__/database.cpython-312.pyc
ADDED
Binary file (839 Bytes). View file
|
|
app/__pycache__/main.cpython-312.pyc
ADDED
Binary file (701 Bytes). View file
|
|
app/__pycache__/models.cpython-311.pyc
ADDED
Binary file (1.61 kB). View file
|
|
app/__pycache__/models.cpython-312.pyc
ADDED
Binary file (1.32 kB). View file
|
|
app/__pycache__/schemas.cpython-311.pyc
ADDED
Binary file (1.42 kB). View file
|
|
app/__pycache__/schemas.cpython-312.pyc
ADDED
Binary file (1.1 kB). View file
|
|
app/check_conn.py
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pymysql
|
2 |
+
from dotenv import load_dotenv
|
3 |
+
import os
|
4 |
+
|
5 |
+
load_dotenv()
|
6 |
+
|
7 |
+
MYSQL_USER = os.getenv('MYSQL_USER')
|
8 |
+
MYSQL_PASSWORD = os.getenv('MYSQL_PASSWORD')
|
9 |
+
MYSQL_HOST = os.getenv('MYSQL_HOST')
|
10 |
+
MYSQL_PORT = int(os.getenv('MYSQL_PORT'))
|
11 |
+
MYSQL_DB = os.getenv('MYSQL_DB')
|
12 |
+
|
13 |
+
connection = pymysql.connect(
|
14 |
+
host=MYSQL_HOST,
|
15 |
+
user=MYSQL_USER,
|
16 |
+
password=MYSQL_PASSWORD,
|
17 |
+
database=MYSQL_DB,
|
18 |
+
port=MYSQL_PORT
|
19 |
+
)
|
20 |
+
|
21 |
+
print("Connected to the database successfully!")
|
22 |
+
connection.close()
|
app/database.py
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from sqlalchemy import create_engine
|
2 |
+
from sqlalchemy.ext.declarative import declarative_base
|
3 |
+
from sqlalchemy.orm import sessionmaker
|
4 |
+
from dotenv import load_dotenv
|
5 |
+
import os
|
6 |
+
|
7 |
+
# Load environment variables from .env file
|
8 |
+
load_dotenv()
|
9 |
+
|
10 |
+
# Ensure all required environment variables are set
|
11 |
+
# MYSQL_USER = os.environ.get('MYSQL_USER')
|
12 |
+
# MYSQL_PASSWORD = os.environ.get('MYSQL_PASSWORD')
|
13 |
+
# MYSQL_HOST = os.environ.get('MYSQL_HOST')
|
14 |
+
# MYSQL_PORT = os.environ.get('MYSQL_PORT')
|
15 |
+
# MYSQL_DB = os.environ.get('MYSQL_DB')
|
16 |
+
|
17 |
+
# if not all([MYSQL_USER, MYSQL_PASSWORD, MYSQL_HOST, MYSQL_PORT, MYSQL_DB]):
|
18 |
+
# raise ValueError("Some of the required environment variables are not set.")
|
19 |
+
|
20 |
+
# Create the database URL
|
21 |
+
engine = create_engine('sqlite:///:memory:', echo=True)
|
22 |
+
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
23 |
+
Base = declarative_base()
|
24 |
+
|
25 |
+
def get_db():
|
26 |
+
db = SessionLocal()
|
27 |
+
try:
|
28 |
+
yield db
|
29 |
+
finally:
|
30 |
+
db.close()
|
app/main.py
ADDED
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI
|
2 |
+
from app.routes.users import router as users_router
|
3 |
+
import uvicorn
|
4 |
+
|
5 |
+
app = FastAPI()
|
6 |
+
|
7 |
+
# Include the user routes
|
8 |
+
app.include_router(users_router)
|
9 |
+
|
10 |
+
|
11 |
+
@app.get("/")
|
12 |
+
async def root():
|
13 |
+
return {"message": "Hello World"}
|
14 |
+
|
15 |
+
|
16 |
+
if __name__ == "__main__":
|
17 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|
app/models.py
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from sqlalchemy import Column, Integer, String
|
2 |
+
from sqlalchemy import create_engine
|
3 |
+
from sqlalchemy.ext.declarative import declarative_base
|
4 |
+
from sqlalchemy.orm import sessionmaker
|
5 |
+
|
6 |
+
Base = declarative_base()
|
7 |
+
|
8 |
+
|
9 |
+
class User(Base):
|
10 |
+
__tablename__ = "users"
|
11 |
+
|
12 |
+
id = Column(Integer, primary_key=True, index=True)
|
13 |
+
name = Column(String, index=True)
|
14 |
+
email = Column(String, unique=True, index=True)
|
15 |
+
|
16 |
+
|
17 |
+
engine = create_engine('sqlite:///./test.db', echo=True)
|
18 |
+
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
19 |
+
Base.metadata.create_all(bind=engine)
|
20 |
+
|
21 |
+
|
22 |
+
def get_db():
|
23 |
+
db = SessionLocal()
|
24 |
+
try:
|
25 |
+
yield db
|
26 |
+
finally:
|
27 |
+
db.close()
|
app/routes/__init__.py
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
from . import users
|
app/routes/__pycache__/__init__.cpython-311.pyc
ADDED
Binary file (223 Bytes). View file
|
|
app/routes/__pycache__/__init__.cpython-312.pyc
ADDED
Binary file (167 Bytes). View file
|
|
app/routes/__pycache__/users.cpython-311.pyc
ADDED
Binary file (3.85 kB). View file
|
|
app/routes/__pycache__/users.cpython-312.pyc
ADDED
Binary file (3.43 kB). View file
|
|
app/routes/users.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import APIRouter, Depends, HTTPException
|
2 |
+
from sqlalchemy.orm import Session
|
3 |
+
from app import schemas, models
|
4 |
+
from app.models import SessionLocal
|
5 |
+
from typing import List
|
6 |
+
|
7 |
+
router = APIRouter(
|
8 |
+
prefix="/users",
|
9 |
+
tags=["users"]
|
10 |
+
)
|
11 |
+
|
12 |
+
|
13 |
+
def get_db():
|
14 |
+
db = SessionLocal()
|
15 |
+
try:
|
16 |
+
yield db
|
17 |
+
finally:
|
18 |
+
db.close()
|
19 |
+
|
20 |
+
|
21 |
+
@router.post("/", response_model=schemas.User)
|
22 |
+
def create_user(user: schemas.UserCreate, db: Session = Depends(get_db)):
|
23 |
+
db_user = models.User(name=user.name, email=user.email)
|
24 |
+
db.add(db_user)
|
25 |
+
db.commit()
|
26 |
+
db.refresh(db_user)
|
27 |
+
return db_user
|
28 |
+
|
29 |
+
|
30 |
+
@router.get("/", response_model=List[schemas.User])
|
31 |
+
def read_users(db: Session = Depends(get_db)):
|
32 |
+
users = db.query(models.User).all()
|
33 |
+
return users
|
34 |
+
|
35 |
+
|
36 |
+
@router.put("/{user_id}", response_model=schemas.User)
|
37 |
+
def update_user(user_id: int, user: schemas.UserUpdate, db: Session = Depends(get_db)):
|
38 |
+
db_user = db.query(models.User).filter(models.User.id == user_id).first()
|
39 |
+
if not db_user:
|
40 |
+
raise HTTPException(status_code=404, detail="User not found")
|
41 |
+
db_user.name = user.name
|
42 |
+
db_user.email = user.email
|
43 |
+
db.commit()
|
44 |
+
db.refresh(db_user)
|
45 |
+
return db_user
|
46 |
+
|
47 |
+
|
48 |
+
@router.delete("/{user_id}")
|
49 |
+
def delete_user(user_id: int, db: Session = Depends(get_db)):
|
50 |
+
db_user = db.query(models.User).filter(models.User.id == user_id).first()
|
51 |
+
if not db_user:
|
52 |
+
raise HTTPException(status_code=404, detail="User not found")
|
53 |
+
db.delete(db_user)
|
54 |
+
db.commit()
|
55 |
+
return {"message": "User deleted successfully"}
|
app/schemas.py
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from pydantic import BaseModel, EmailStr
|
2 |
+
|
3 |
+
|
4 |
+
class UserBase(BaseModel):
|
5 |
+
name: str
|
6 |
+
email: EmailStr
|
7 |
+
|
8 |
+
|
9 |
+
class UserCreate(UserBase):
|
10 |
+
pass
|
11 |
+
|
12 |
+
|
13 |
+
class UserUpdate(UserBase):
|
14 |
+
pass
|
15 |
+
|
16 |
+
|
17 |
+
class User(UserBase):
|
18 |
+
id: int
|
19 |
+
|
20 |
+
class Config:
|
21 |
+
from_attributes = True
|
app/test.db
ADDED
Binary file (20.5 kB). View file
|
|
requirements.txt
ADDED
Binary file (1.51 kB). View file
|
|