little of backend
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
FROM python:3.12-alpine
|
||||
WORKDIR /app
|
||||
COPY ./requirements.txt /app/requirements.txt
|
||||
WORKDIR /code
|
||||
COPY ./requirements.txt /code/requirements.txt
|
||||
RUN pip install --no-cache-dir -r requirements.txt
|
||||
COPY ./app /app
|
||||
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000", "--reload"]
|
||||
COPY ./app /code/app
|
||||
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000", "--reload"]
|
||||
0
backend/app/__init__.py
Normal file
0
backend/app/__init__.py
Normal file
0
backend/app/db/__init__.py
Normal file
0
backend/app/db/__init__.py
Normal file
19
backend/app/db/database.py
Normal file
19
backend/app/db/database.py
Normal file
@ -0,0 +1,19 @@
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
import os
|
||||
|
||||
POSTGRES_USER = os.environ.get("POSTGRES_USER", "user")
|
||||
POSTGRES_PASSWORD = os.environ.get("POSTGRES_PASSWORD", "password")
|
||||
POSTGRES_DB = os.environ.get("POSTGRES_DB", "db")
|
||||
POSTGRES_HOST = os.environ.get("POSTGRES_HOST", "postgres")
|
||||
|
||||
|
||||
SQLALCHEMY_DATABASE_URL = (
|
||||
f"postgresql://{POSTGRES_USER}:{POSTGRES_PASSWORD}@{POSTGRES_HOST}/{POSTGRES_DB}"
|
||||
)
|
||||
|
||||
engine = create_engine(SQLALCHEMY_DATABASE_URL)
|
||||
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||
|
||||
Base = declarative_base()
|
||||
13
backend/app/db/models.py
Normal file
13
backend/app/db/models.py
Normal file
@ -0,0 +1,13 @@
|
||||
from sqlalchemy import Boolean, Column, ForeignKey, Integer, String
|
||||
from sqlalchemy.orm import relationship
|
||||
|
||||
from .database import Base
|
||||
|
||||
|
||||
class User(Base):
|
||||
__tablename__ = "users"
|
||||
|
||||
id = Column(Integer, primary_key=True)
|
||||
email = Column(String, unique=True, index=True)
|
||||
hashed_password = Column(String)
|
||||
is_active = Column(Boolean, default=True)
|
||||
19
backend/app/db/schemas.py
Normal file
19
backend/app/db/schemas.py
Normal file
@ -0,0 +1,19 @@
|
||||
from typing import Union
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class UserBase(BaseModel):
|
||||
username: str
|
||||
email: str
|
||||
|
||||
|
||||
class UserCreate(UserBase):
|
||||
password: str
|
||||
|
||||
|
||||
class User(UserBase):
|
||||
id: int
|
||||
is_active: bool
|
||||
|
||||
class Config:
|
||||
orm_mode = True
|
||||
9
backend/app/dependencies.py
Normal file
9
backend/app/dependencies.py
Normal file
@ -0,0 +1,9 @@
|
||||
from .db.database import SessionLocal
|
||||
|
||||
|
||||
def get_db():
|
||||
db = SessionLocal()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
@ -1,15 +1,18 @@
|
||||
from typing import Union
|
||||
from fastapi import FastAPI, Depends
|
||||
|
||||
from fastapi import FastAPI
|
||||
from .db import models
|
||||
from .db.database import SessionLocal, engine
|
||||
from .dependencies import get_db
|
||||
|
||||
app = FastAPI()
|
||||
from .views.auth.api import router as auth_router
|
||||
|
||||
app = FastAPI(dependencies=[Depends(get_db)])
|
||||
models.Base.metadata.create_all(bind=engine)
|
||||
|
||||
app.include_router(auth_router)
|
||||
|
||||
|
||||
@app.get("/api/")
|
||||
@app.get("/")
|
||||
def read_root():
|
||||
return {"Hello": "Woasrld"}
|
||||
|
||||
|
||||
@app.get("/items/{item_id}")
|
||||
def read_item(item_id: int, q: Union[str, None] = None):
|
||||
return {"item_id": item_id, "q": q}
|
||||
return {"message": "OK"}
|
||||
|
||||
0
backend/app/views/__init__.py
Normal file
0
backend/app/views/__init__.py
Normal file
0
backend/app/views/auth/__init__.py
Normal file
0
backend/app/views/auth/__init__.py
Normal file
33
backend/app/views/auth/api.py
Normal file
33
backend/app/views/auth/api.py
Normal file
@ -0,0 +1,33 @@
|
||||
from fastapi import APIRouter, Depends, FastAPI, HTTPException
|
||||
from sqlalchemy.orm import Session
|
||||
from . import crud
|
||||
from ...dependencies import get_db
|
||||
from ...db import schemas
|
||||
|
||||
router = APIRouter(
|
||||
prefix="/auth",
|
||||
tags=["auth"],
|
||||
dependencies=[Depends(get_db)],
|
||||
responses={404: {"description": "Not found"}},
|
||||
)
|
||||
|
||||
|
||||
@router.post("/users/", response_model=schemas.User)
|
||||
def create_user(user: schemas.UserCreate, db: Session = Depends(get_db)):
|
||||
db_user = crud.get_user_by_email(db, email=user.email)
|
||||
if db_user:
|
||||
raise HTTPException(status_code=400, detail="Email already registered")
|
||||
return crud.create_user(db=db, user=user)
|
||||
|
||||
|
||||
@router.get("/", response_model=list[schemas.User])
|
||||
def read_users(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
|
||||
users = crud.get_users(db, skip=skip, limit=limit)
|
||||
return users
|
||||
|
||||
|
||||
@router.get("/users/{user_id}", response_model=schemas.User)
|
||||
def read_user(user_id: int, db: Session = Depends(get_db)):
|
||||
db_user = crud.get_user(db, user_id=user_id)
|
||||
if db_user is None:
|
||||
raise HTTPException
|
||||
24
backend/app/views/auth/crud.py
Normal file
24
backend/app/views/auth/crud.py
Normal file
@ -0,0 +1,24 @@
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ...db import models, schemas
|
||||
|
||||
|
||||
def get_user(db: Session, user_id: int):
|
||||
return db.query(models.User).filter(models.User.id == user_id).first()
|
||||
|
||||
|
||||
def get_user_by_email(db: Session, email: str):
|
||||
return db.query(models.User).filter(models.User.email == email).first()
|
||||
|
||||
|
||||
def get_users(db: Session, skip: int = 0, limit: int = 100):
|
||||
return db.query(models.User).offset(skip).limit(limit).all()
|
||||
|
||||
|
||||
def create_user(db: Session, user: schemas.UserCreate):
|
||||
fake_hashed_password = user.password + "notreallyhashed"
|
||||
db_user = models.User(email=user.email, hashed_password=fake_hashed_password)
|
||||
db.add(db_user)
|
||||
db.commit()
|
||||
db.refresh(db_user)
|
||||
return db_user
|
||||
@ -1,3 +1,4 @@
|
||||
fastapi[all]
|
||||
pydantic
|
||||
sqlalchemy
|
||||
|
||||
psycopg2-binary
|
||||
2
dev.yml
2
dev.yml
@ -8,7 +8,7 @@ services:
|
||||
- path: ./env/backend/dev.env
|
||||
required: true
|
||||
volumes:
|
||||
- ./backend/app:/app:z
|
||||
- ./backend/app:/code/app:z
|
||||
frontend:
|
||||
build:
|
||||
context: frontend
|
||||
|
||||
@ -1,8 +1,11 @@
|
||||
FROM node:21-alpine
|
||||
|
||||
WORKDIR /app
|
||||
COPY ./app /app
|
||||
|
||||
COPY ./app/package-lock.json /app/package-lock.json
|
||||
COPY ./app/package.json /app/package.json
|
||||
RUN npm ci
|
||||
|
||||
COPY ./app /app
|
||||
|
||||
CMD [ "npm", "start" ]
|
||||
|
||||
Reference in New Issue
Block a user