Skip to content

Commit

Permalink
Merge pull request #1 from moevm/hello_world
Browse files Browse the repository at this point in the history
Hello world
  • Loading branch information
Dlexeyn authored Sep 28, 2024
2 parents 43e23ab + 71c7560 commit 71dff17
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 0 deletions.
10 changes: 10 additions & 0 deletions hello_world/Dockerfile
Original file line number Diff line number Diff line change
@@ -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 .. .

33 changes: 33 additions & 0 deletions hello_world/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -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


49 changes: 49 additions & 0 deletions hello_world/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import os

from fastapi import FastAPI, APIRouter
from fastapi.routing import APIRoute
from motor import motor_asyncio
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 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="/get_users", endpoint=get_users, methods=["GET"]),
]

app.include_router(APIRouter(routes=routes))
7 changes: 7 additions & 0 deletions hello_world/requirements.txt
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions hello_world/users/schemas.py
Original file line number Diff line number Diff line change
@@ -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)
6 changes: 6 additions & 0 deletions hello_world/users/utils.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 71dff17

Please sign in to comment.