generated from moevm/nsql-clean-tempate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from moevm/hello_world
Hello world
- Loading branch information
Showing
6 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 .. . | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |