From d1fec0a6cfb62558e6b220502a263a946f7ee007 Mon Sep 17 00:00:00 2001 From: dlexeyn Date: Sat, 28 Sep 2024 12:19:44 +0300 Subject: [PATCH 1/4] hello world commit --- Dockerfile | 10 ++++++++ docker-compose.yaml | 33 ++++++++++++++++++++++++++ main.py | 57 +++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 7 ++++++ users/schemas.py | 14 +++++++++++ users/utils.py | 6 +++++ 6 files changed, 127 insertions(+) create mode 100644 Dockerfile create mode 100644 docker-compose.yaml create mode 100644 main.py create mode 100644 requirements.txt create mode 100644 users/schemas.py create mode 100644 users/utils.py diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..9b3cb44 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,10 @@ +FROM python:3.12-alpine + +WORKDIR /app + +COPY requirements.txt . + +RUN python3 -m pip install --no-cache-dir -r requirements.txt + +COPY . . + diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..703160e --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,33 @@ +services: + mongo_db: + image: mongo + container_name: mongo_db + ports: + - "27017:27017" + networks: + - custom + + app: + container_name: repair-app + image: app + restart: on-failure + environment: + - MONGODB_URL=mongodb://mongo_db/repair_db + ports: + - "8000:8000" + networks: + - custom + build: . + volumes: + - .:/app + command: uvicorn main:app --host 0.0.0.0 --port 8000 --reload + depends_on: + - mongo_db + + + +networks: + custom: + driver: bridge + + \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..de45881 --- /dev/null +++ b/main.py @@ -0,0 +1,57 @@ +import os +from dataclasses import dataclass + +from fastapi import FastAPI, APIRouter +from fastapi.routing import APIRoute +from motor import motor_asyncio +from motor.motor_asyncio import AsyncIOMotorClient +from starlette.requests import Request + +from users.schemas import UserRegisterSchema +from users.utils import get_password_hash + +app = FastAPI() + +client = motor_asyncio.AsyncIOMotorClient(os.environ["MONGODB_URL"]) +db = client.get_database("repair_db") +user_collection = db.get_collection("users") + + +async def ping() -> dict: + return {"Ping": "Pong!"} + + +async def mainpage() -> str: + return "YOU ARE ON THE MAIN PAGE" + + +async def create_user(request: Request) -> dict: + await user_collection.insert_one({"sample": "user"}) + return {"Success": True} + + +async def get_users(request: Request) -> list: + cursor = user_collection.find({}) + res = [] + for document in await cursor.to_list(length=100): + document["_id"] = str(document["_id"]) + res.append(document) + return res + + +@app.post("/register") +async def register(user_data: UserRegisterSchema) -> dict: + data = user_data.model_dump() + data["password"] = get_password_hash(user_data.password) + await user_collection.insert_one(data) + return {"Success": True} + + +routes = [ + APIRoute(path="/ping", endpoint=ping, methods=["GET"]), + APIRoute(path="/", endpoint=mainpage, methods=["GET"]), + APIRoute(path="/create_user", endpoint=create_user, methods=["POST"]), + APIRoute(path="/get_users", endpoint=get_users, methods=["GET"]), +] + +app.include_router(APIRouter(routes=routes)) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..190bbe6 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,7 @@ +fastapi~=0.115.0 +motor~=3.6.0 +starlette~=0.38.6 +uvicorn +pydantic~=2.9.2 +pydantic[email] +passlib~=1.7.4 \ No newline at end of file diff --git a/users/schemas.py b/users/schemas.py new file mode 100644 index 0000000..e8a7e38 --- /dev/null +++ b/users/schemas.py @@ -0,0 +1,14 @@ +from pydantic import BaseModel, EmailStr, Field + + +class UserRegisterSchema(BaseModel): + email: EmailStr = Field(...) + password: str = Field(..., min_length=8, max_length=32) + first_name: str = Field(..., min_length=3, max_length=50) + last_name: str = Field(..., min_length=3, max_length=50) + middle_name: str = Field(min_length=3, max_length=50) + + +class UserLoginSchema(BaseModel): + email: EmailStr = Field(...) + password: str = Field(..., min_length=8, max_length=32) diff --git a/users/utils.py b/users/utils.py new file mode 100644 index 0000000..e4a5c4f --- /dev/null +++ b/users/utils.py @@ -0,0 +1,6 @@ +from passlib.context import CryptContext + +pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto") + +def get_password_hash(password: str) -> str: + return pwd_context.hash(password) \ No newline at end of file From 6cf5795a39fb9b965892b6cf297b08c9b68cbefa Mon Sep 17 00:00:00 2001 From: dlexeyn Date: Sat, 28 Sep 2024 12:26:46 +0300 Subject: [PATCH 2/4] delete old function creating user --- main.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/main.py b/main.py index de45881..4ae9c4c 100644 --- a/main.py +++ b/main.py @@ -25,10 +25,6 @@ async def mainpage() -> str: return "YOU ARE ON THE MAIN PAGE" -async def create_user(request: Request) -> dict: - await user_collection.insert_one({"sample": "user"}) - return {"Success": True} - async def get_users(request: Request) -> list: cursor = user_collection.find({}) From 4a7e273676e55b780de8993a05738b5fd225d2f2 Mon Sep 17 00:00:00 2001 From: dlexeyn Date: Sat, 28 Sep 2024 12:27:26 +0300 Subject: [PATCH 3/4] fix --- main.py | 1 - 1 file changed, 1 deletion(-) diff --git a/main.py b/main.py index 4ae9c4c..74acdf5 100644 --- a/main.py +++ b/main.py @@ -46,7 +46,6 @@ async def register(user_data: UserRegisterSchema) -> dict: routes = [ APIRoute(path="/ping", endpoint=ping, methods=["GET"]), APIRoute(path="/", endpoint=mainpage, methods=["GET"]), - APIRoute(path="/create_user", endpoint=create_user, methods=["POST"]), APIRoute(path="/get_users", endpoint=get_users, methods=["GET"]), ] From 71c7560c8a4eda09523f8e5746929c337c243d77 Mon Sep 17 00:00:00 2001 From: dlexeyn Date: Sat, 28 Sep 2024 13:56:23 +0300 Subject: [PATCH 4/4] replace code to hello_world --- Dockerfile => hello_world/Dockerfile | 2 +- docker-compose.yaml => hello_world/docker-compose.yaml | 2 +- main.py => hello_world/main.py | 3 --- requirements.txt => hello_world/requirements.txt | 0 {users => hello_world/users}/schemas.py | 0 {users => hello_world/users}/utils.py | 0 6 files changed, 2 insertions(+), 5 deletions(-) rename Dockerfile => hello_world/Dockerfile (92%) rename docker-compose.yaml => hello_world/docker-compose.yaml (97%) rename main.py => hello_world/main.py (93%) rename requirements.txt => hello_world/requirements.txt (100%) rename {users => hello_world/users}/schemas.py (100%) rename {users => hello_world/users}/utils.py (100%) diff --git a/Dockerfile b/hello_world/Dockerfile similarity index 92% rename from Dockerfile rename to hello_world/Dockerfile index 9b3cb44..c789f39 100644 --- a/Dockerfile +++ b/hello_world/Dockerfile @@ -6,5 +6,5 @@ COPY requirements.txt . RUN python3 -m pip install --no-cache-dir -r requirements.txt -COPY . . +COPY .. . diff --git a/docker-compose.yaml b/hello_world/docker-compose.yaml similarity index 97% rename from docker-compose.yaml rename to hello_world/docker-compose.yaml index 703160e..c372fa6 100644 --- a/docker-compose.yaml +++ b/hello_world/docker-compose.yaml @@ -17,7 +17,7 @@ services: - "8000:8000" networks: - custom - build: . + build: .. volumes: - .:/app command: uvicorn main:app --host 0.0.0.0 --port 8000 --reload diff --git a/main.py b/hello_world/main.py similarity index 93% rename from main.py rename to hello_world/main.py index 74acdf5..805a280 100644 --- a/main.py +++ b/hello_world/main.py @@ -1,10 +1,8 @@ import os -from dataclasses import dataclass from fastapi import FastAPI, APIRouter from fastapi.routing import APIRoute from motor import motor_asyncio -from motor.motor_asyncio import AsyncIOMotorClient from starlette.requests import Request from users.schemas import UserRegisterSchema @@ -25,7 +23,6 @@ async def mainpage() -> str: return "YOU ARE ON THE MAIN PAGE" - async def get_users(request: Request) -> list: cursor = user_collection.find({}) res = [] diff --git a/requirements.txt b/hello_world/requirements.txt similarity index 100% rename from requirements.txt rename to hello_world/requirements.txt diff --git a/users/schemas.py b/hello_world/users/schemas.py similarity index 100% rename from users/schemas.py rename to hello_world/users/schemas.py diff --git a/users/utils.py b/hello_world/users/utils.py similarity index 100% rename from users/utils.py rename to hello_world/users/utils.py