From 93e2ffe021f74cb8fcdf5d97ace11d1936bb61de Mon Sep 17 00:00:00 2001 From: maksim Date: Tue, 17 Dec 2024 00:31:14 +0400 Subject: [PATCH] =?UTF-8?q?Init=20=D0=BA=D0=BE=D0=BC=D0=B8=D1=82=20=D1=88?= =?UTF-8?q?=D0=B0=D0=B1=D0=BB=D0=BE=D0=BD=D0=B0=20FastAPI?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fastapi-app/.dockeringore | 3 +++ fastapi-app/.gitignore | 4 ++++ fastapi-app/Dockerfile | 14 ++++++++++++++ fastapi-app/README.md | Bin 0 -> 2978 bytes fastapi-app/database.py | 30 ++++++++++++++++++++++++++++++ fastapi-app/main.py | 20 ++++++++++++++++++++ fastapi-app/repository.py | 26 ++++++++++++++++++++++++++ fastapi-app/requirements.txt | Bin 0 -> 572 bytes fastapi-app/router.py | 25 +++++++++++++++++++++++++ fastapi-app/schemas.py | 19 +++++++++++++++++++ 10 files changed, 141 insertions(+) create mode 100644 fastapi-app/.dockeringore create mode 100644 fastapi-app/.gitignore create mode 100644 fastapi-app/Dockerfile create mode 100644 fastapi-app/README.md create mode 100644 fastapi-app/database.py create mode 100644 fastapi-app/main.py create mode 100644 fastapi-app/repository.py create mode 100644 fastapi-app/requirements.txt create mode 100644 fastapi-app/router.py create mode 100644 fastapi-app/schemas.py diff --git a/fastapi-app/.dockeringore b/fastapi-app/.dockeringore new file mode 100644 index 0000000..2b8052f --- /dev/null +++ b/fastapi-app/.dockeringore @@ -0,0 +1,3 @@ +venv +Dockerfile +tasks.db \ No newline at end of file diff --git a/fastapi-app/.gitignore b/fastapi-app/.gitignore new file mode 100644 index 0000000..979d491 --- /dev/null +++ b/fastapi-app/.gitignore @@ -0,0 +1,4 @@ +venv +tasks.db +.idea +__pycache__ \ No newline at end of file diff --git a/fastapi-app/Dockerfile b/fastapi-app/Dockerfile new file mode 100644 index 0000000..a929a1f --- /dev/null +++ b/fastapi-app/Dockerfile @@ -0,0 +1,14 @@ +FROM python:3.11-slim + +COPY ../../../../../AppData/Local/Temp . + +RUN pip install -r requirements.txt + +CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "80"] + +# Для запуска введите две команды: +# docker build . --tag fastapi_app +# docker run -p 80:80 fastapi_app + +# Или одной командой +# docker build . --tag fastapi_app && docker run -p 80:80 fastapi_app diff --git a/fastapi-app/README.md b/fastapi-app/README.md new file mode 100644 index 0000000000000000000000000000000000000000..b7630ff1abf80e64da4f76113a7026a1c65d7c6a GIT binary patch literal 2978 zcmcJR&2QUA5XI-Prvf<^IkZ5xb`Yd!Wy+2}WW}N-yD3oAje{1=B?)3#Qf#BLLWoil z1O40E{@yNGigFwS2?~N-aydKm=FP|MKYy>4RP-gk$t z$Dcd)h0X1)UD&|h*wiMhxwJmt6KgYOT;3X7QybYBnZ6B8t8_;j-?*;Za+_$}4{zJN zZ(*&)Q&L}I=@DPv4!Jk5-eY`fS2nYAG|J{fTeDrpRw1d?+pPbAIpN(aG#)T|JFz`G zKw{smTZ+VA+~v#$Gbem^SS86n(0>uEJpWHdtE72x%}ZqKetipfJNVKpeDNkf_rq+< z9WZvma2$L*VRc-8hP4^G#h~7#vz!x$mtdJO`oQFnQM_LS88H!MyTy1;bWY($(YYid zd-&BNZXJ;9!b)mMm}`Pm%tqGz2XUt8iQ0u}t+*;2@t=@P0;W+7yW%diYqi;k>$32< z0Y+~th31u-r*cCGwv1%YS3NfAYJ!Dq#lg*nY75yB!J~cDG!A^-< zIAm{Nf1-C9G=B~HZK9cmib<&$<(;Z{C-~LGzawUk`QJjW$9I++^ zfjK9060T?^Cq9ez%Ir$8J_(FWcuPVCS3V>m6X!_GcuNAxvRc(iQV_PlAl4%~Z<^f% zVOh=c`2Xd30OJjq6pu4Jc+IUjMJ$Jr0obQSL?*mv_*;+Ua^zx8H*LqZxRkl70&z7% zTY_%oND^Xqg>J1s!IzjX&VnDY4^}x@ z&Z>J4!P^I&y3aItq5Qtxi!x_oW~zT&XG|3>z2c>B-v|Alqgl+2;7)skw-ay?h=M9tK1)uWSUQ!v>cZb4n=`9kH)4GQKCjUBskmRF z??cu0IdNU|U0$wXqr&$F{N=8!*yi}*?LLWDkNnE9V=XXuO~cBDz2Wr$BYQ!KAjO*NW zu=D98<;f)VWp(Ox`gY4H`Z|$a*SEz9qmE|L3m int: + async with new_session() as session: + task_dict = data.model_dump() + + task = TaskOrm(**task_dict) + session.add(task) + await session.flush() + await session.commit() + return task.id + + @classmethod + async def find_all(cls) -> list[STask]: + async with new_session() as session: + query = select(TaskOrm) + result = await session.execute(query) + task_models = result.scalars().all() + task_schemas = [STask.model_validate(task_model) for task_model in task_models] + return task_schemas diff --git a/fastapi-app/requirements.txt b/fastapi-app/requirements.txt new file mode 100644 index 0000000000000000000000000000000000000000..712cf652d3530a59c89d60688e1f3af52aa7ccc2 GIT binary patch literal 572 zcmZ9J!A`?45JcyU#HUczEwpgpz?}=?1F9%Vi$zI84T$_a@OGD`1zCDJJq?)RHwf9?4|3}Xl&VgbY9p8FUGKN#G2dUK6>~M^@Pu(+YS9KtAncrYpA*FPdII!Cp6PZ zxADiMSq*)mY(1gcGi$?b2U}L{oi@=GI~e~XR-kN-$kVXNa|pfyQ`VYg>6<^xAUvAC zM?L6XclfXI+LFaEYTDb(P&Qm7vHzX(^!4WMEjcP)Dx STaskId: + task_id = await TaskRepository.add_one(task) + return {"ok": True, "task_id": task_id} + + +@router.get("") +async def get_tasks() -> list[STask]: + tasks = await TaskRepository.find_all() + return tasks diff --git a/fastapi-app/schemas.py b/fastapi-app/schemas.py new file mode 100644 index 0000000..3918fcb --- /dev/null +++ b/fastapi-app/schemas.py @@ -0,0 +1,19 @@ +from typing import Optional + +from pydantic import BaseModel, ConfigDict + + +class STaskAdd(BaseModel): + name: str + description: Optional[str] = None + + +class STask(STaskAdd): + id: int + + model_config = ConfigDict(from_attributes=True) + + +class STaskId(BaseModel): + ok: bool = True + task_id: int