From 02116d52c0b2d3a9387b88f0fed441146569892b Mon Sep 17 00:00:00 2001 From: sergey Date: Tue, 8 Oct 2024 17:33:15 +0300 Subject: [PATCH] add tg router and refactor --- auth/__init__.py | 6 ++++ auth/static_env.py | 31 +++++++++++++++++ config/.env-template | 4 +++ config/config.py | 13 ++++++++ docker/docker-compose.yaml | 1 - poetry.lock | 68 +++++++++++++++++++++++++++++++++++++- pyproject.toml | 1 + reddis/__init__.py | 0 routers/__init__.py | 6 ++++ routers/swagger.py | 8 +++-- routers/tg_send.py | 16 +++++++++ schemas/__init__.py | 4 +++ schemas/tg_send.py | 6 ++++ telegram/__init__.py | 0 14 files changed, 160 insertions(+), 4 deletions(-) create mode 100644 auth/__init__.py create mode 100644 auth/static_env.py create mode 100644 reddis/__init__.py create mode 100644 routers/tg_send.py create mode 100644 schemas/tg_send.py create mode 100644 telegram/__init__.py diff --git a/auth/__init__.py b/auth/__init__.py new file mode 100644 index 0000000..1562624 --- /dev/null +++ b/auth/__init__.py @@ -0,0 +1,6 @@ +from .static_env import verify_token_admin, verify_user_pwd + +__all__ = [ + "verify_token_admin", + "verify_user_pwd", +] diff --git a/auth/static_env.py b/auth/static_env.py new file mode 100644 index 0000000..07ddf10 --- /dev/null +++ b/auth/static_env.py @@ -0,0 +1,31 @@ +from fastapi import Depends, HTTPException, status +from fastapi.security import APIKeyHeader +from fastapi.security import HTTPBasic, HTTPBasicCredentials +from config import conf +import logging as log + +security = HTTPBasic() +api_key_header = APIKeyHeader(name="X-API-KEY", auto_error=False) + + +def verify_token_admin(token: str = Depends(api_key_header)): + if token != conf.token.admin: + log.warning("Invalid token") + raise HTTPException( + status_code=status.HTTP_401_UNAUTHORIZED, + detail="Invalid authentication credentials", + headers={"WWW-Authenticate": "Bearer"}, + ) + + +def verify_user_pwd(credentials: HTTPBasicCredentials = Depends(security)): + if ( + credentials.username != conf.admin_user.login + or credentials.password != conf.admin_user.pwd + ): + log.warning("Invalid credentials") + raise HTTPException( + status_code=status.HTTP_401_UNAUTHORIZED, + detail="Invalid credentials", + headers={"WWW-Authenticate": "Basic"}, + ) diff --git a/config/.env-template b/config/.env-template index fdf6220..0e4d414 100644 --- a/config/.env-template +++ b/config/.env-template @@ -2,3 +2,7 @@ OAA_CFG__RUN__HOST=0.0.0.0 OAA_CFG__RUN__PORT=8000 OAA_CFG__RUN__RELOAD=True OAA_CFG__LOG__LEVEL=20 +OAA_CFG__TOKEN__ADMIN=pE0ULNppBtQOOxRuVw1tdiXTG5dZTaSx +OAA_CFG__TOKEN__USER=A3mojtenQqYwJAgrSiiFaVHX6fRoVW15 +OAA_CFG__ADMIN_USER__LOGIN=admin +OAA_CFG__ADMIN_USER__PWD=P@ssw0rd! \ No newline at end of file diff --git a/config/config.py b/config/config.py index 44b3e2b..ae7a203 100644 --- a/config/config.py +++ b/config/config.py @@ -31,6 +31,17 @@ class LogConfig(BaseModel): class Prefix(BaseModel): swagger: str = "/docs" api_v1: str = "/api/v1" + tg_v1: str = api_v1 + "/tg" + + +class Token(BaseModel): + admin: str + user: str + + +class AdminUser(BaseModel): + login: str + pwd: str class SwaggerConfig(BaseModel): @@ -56,6 +67,8 @@ class Settings(BaseSettings): swagger: SwaggerConfig = SwaggerConfig() log: LogConfig = LogConfig() prefix: Prefix = Prefix() + token: Token + admin_user: AdminUser conf = Settings() diff --git a/docker/docker-compose.yaml b/docker/docker-compose.yaml index 9be1f75..e69de29 100644 --- a/docker/docker-compose.yaml +++ b/docker/docker-compose.yaml @@ -1 +0,0 @@ -router \ No newline at end of file diff --git a/poetry.lock b/poetry.lock index 50ac329..03f6446 100644 --- a/poetry.lock +++ b/poetry.lock @@ -204,6 +204,72 @@ files = [ {file = "mypy_extensions-1.0.0.tar.gz", hash = "sha256:75dbf8955dc00442a438fc4d0666508a9a97b6bd41aa2f0ffe9d2f2725af0782"}, ] +[[package]] +name = "orjson" +version = "3.10.7" +description = "Fast, correct Python JSON library supporting dataclasses, datetimes, and numpy" +optional = false +python-versions = ">=3.8" +files = [ + {file = "orjson-3.10.7-cp310-cp310-macosx_10_15_x86_64.macosx_11_0_arm64.macosx_10_15_universal2.whl", hash = "sha256:74f4544f5a6405b90da8ea724d15ac9c36da4d72a738c64685003337401f5c12"}, + {file = "orjson-3.10.7-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:34a566f22c28222b08875b18b0dfbf8a947e69df21a9ed5c51a6bf91cfb944ac"}, + {file = "orjson-3.10.7-cp310-cp310-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:bf6ba8ebc8ef5792e2337fb0419f8009729335bb400ece005606336b7fd7bab7"}, + {file = "orjson-3.10.7-cp310-cp310-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:ac7cf6222b29fbda9e3a472b41e6a5538b48f2c8f99261eecd60aafbdb60690c"}, + {file = "orjson-3.10.7-cp310-cp310-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:de817e2f5fc75a9e7dd350c4b0f54617b280e26d1631811a43e7e968fa71e3e9"}, + {file = "orjson-3.10.7-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:348bdd16b32556cf8d7257b17cf2bdb7ab7976af4af41ebe79f9796c218f7e91"}, + {file = "orjson-3.10.7-cp310-cp310-musllinux_1_2_aarch64.whl", hash = "sha256:479fd0844ddc3ca77e0fd99644c7fe2de8e8be1efcd57705b5c92e5186e8a250"}, + {file = "orjson-3.10.7-cp310-cp310-musllinux_1_2_x86_64.whl", hash = "sha256:fdf5197a21dd660cf19dfd2a3ce79574588f8f5e2dbf21bda9ee2d2b46924d84"}, + {file = "orjson-3.10.7-cp310-none-win32.whl", hash = "sha256:d374d36726746c81a49f3ff8daa2898dccab6596864ebe43d50733275c629175"}, + {file = "orjson-3.10.7-cp310-none-win_amd64.whl", hash = "sha256:cb61938aec8b0ffb6eef484d480188a1777e67b05d58e41b435c74b9d84e0b9c"}, + {file = "orjson-3.10.7-cp311-cp311-macosx_10_15_x86_64.macosx_11_0_arm64.macosx_10_15_universal2.whl", hash = "sha256:7db8539039698ddfb9a524b4dd19508256107568cdad24f3682d5773e60504a2"}, + {file = "orjson-3.10.7-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:480f455222cb7a1dea35c57a67578848537d2602b46c464472c995297117fa09"}, + {file = "orjson-3.10.7-cp311-cp311-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:8a9c9b168b3a19e37fe2778c0003359f07822c90fdff8f98d9d2a91b3144d8e0"}, + {file = "orjson-3.10.7-cp311-cp311-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:8de062de550f63185e4c1c54151bdddfc5625e37daf0aa1e75d2a1293e3b7d9a"}, + {file = "orjson-3.10.7-cp311-cp311-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:6b0dd04483499d1de9c8f6203f8975caf17a6000b9c0c54630cef02e44ee624e"}, + {file = "orjson-3.10.7-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:b58d3795dafa334fc8fd46f7c5dc013e6ad06fd5b9a4cc98cb1456e7d3558bd6"}, + {file = "orjson-3.10.7-cp311-cp311-musllinux_1_2_aarch64.whl", hash = "sha256:33cfb96c24034a878d83d1a9415799a73dc77480e6c40417e5dda0710d559ee6"}, + {file = "orjson-3.10.7-cp311-cp311-musllinux_1_2_x86_64.whl", hash = "sha256:e724cebe1fadc2b23c6f7415bad5ee6239e00a69f30ee423f319c6af70e2a5c0"}, + {file = "orjson-3.10.7-cp311-none-win32.whl", hash = "sha256:82763b46053727a7168d29c772ed5c870fdae2f61aa8a25994c7984a19b1021f"}, + {file = "orjson-3.10.7-cp311-none-win_amd64.whl", hash = "sha256:eb8d384a24778abf29afb8e41d68fdd9a156cf6e5390c04cc07bbc24b89e98b5"}, + {file = "orjson-3.10.7-cp312-cp312-macosx_10_15_x86_64.macosx_11_0_arm64.macosx_10_15_universal2.whl", hash = "sha256:44a96f2d4c3af51bfac6bc4ef7b182aa33f2f054fd7f34cc0ee9a320d051d41f"}, + {file = "orjson-3.10.7-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:76ac14cd57df0572453543f8f2575e2d01ae9e790c21f57627803f5e79b0d3c3"}, + {file = "orjson-3.10.7-cp312-cp312-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:bdbb61dcc365dd9be94e8f7df91975edc9364d6a78c8f7adb69c1cdff318ec93"}, + {file = "orjson-3.10.7-cp312-cp312-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:b48b3db6bb6e0a08fa8c83b47bc169623f801e5cc4f24442ab2b6617da3b5313"}, + {file = "orjson-3.10.7-cp312-cp312-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:23820a1563a1d386414fef15c249040042b8e5d07b40ab3fe3efbfbbcbcb8864"}, + {file = "orjson-3.10.7-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:a0c6a008e91d10a2564edbb6ee5069a9e66df3fbe11c9a005cb411f441fd2c09"}, + {file = "orjson-3.10.7-cp312-cp312-musllinux_1_2_aarch64.whl", hash = "sha256:d352ee8ac1926d6193f602cbe36b1643bbd1bbcb25e3c1a657a4390f3000c9a5"}, + {file = "orjson-3.10.7-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:d2d9f990623f15c0ae7ac608103c33dfe1486d2ed974ac3f40b693bad1a22a7b"}, + {file = "orjson-3.10.7-cp312-none-win32.whl", hash = "sha256:7c4c17f8157bd520cdb7195f75ddbd31671997cbe10aee559c2d613592e7d7eb"}, + {file = "orjson-3.10.7-cp312-none-win_amd64.whl", hash = "sha256:1d9c0e733e02ada3ed6098a10a8ee0052dd55774de3d9110d29868d24b17faa1"}, + {file = "orjson-3.10.7-cp313-cp313-macosx_10_15_x86_64.macosx_11_0_arm64.macosx_10_15_universal2.whl", hash = "sha256:77d325ed866876c0fa6492598ec01fe30e803272a6e8b10e992288b009cbe149"}, + {file = "orjson-3.10.7-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:9ea2c232deedcb605e853ae1db2cc94f7390ac776743b699b50b071b02bea6fe"}, + {file = "orjson-3.10.7-cp313-cp313-musllinux_1_2_aarch64.whl", hash = "sha256:3dcfbede6737fdbef3ce9c37af3fb6142e8e1ebc10336daa05872bfb1d87839c"}, + {file = "orjson-3.10.7-cp313-cp313-musllinux_1_2_x86_64.whl", hash = "sha256:11748c135f281203f4ee695b7f80bb1358a82a63905f9f0b794769483ea854ad"}, + {file = "orjson-3.10.7-cp313-none-win32.whl", hash = "sha256:a7e19150d215c7a13f39eb787d84db274298d3f83d85463e61d277bbd7f401d2"}, + {file = "orjson-3.10.7-cp313-none-win_amd64.whl", hash = "sha256:eef44224729e9525d5261cc8d28d6b11cafc90e6bd0be2157bde69a52ec83024"}, + {file = "orjson-3.10.7-cp38-cp38-macosx_10_15_x86_64.macosx_11_0_arm64.macosx_10_15_universal2.whl", hash = "sha256:6ea2b2258eff652c82652d5e0f02bd5e0463a6a52abb78e49ac288827aaa1469"}, + {file = "orjson-3.10.7-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:430ee4d85841e1483d487e7b81401785a5dfd69db5de01314538f31f8fbf7ee1"}, + {file = "orjson-3.10.7-cp38-cp38-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:4b6146e439af4c2472c56f8540d799a67a81226e11992008cb47e1267a9b3225"}, + {file = "orjson-3.10.7-cp38-cp38-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:084e537806b458911137f76097e53ce7bf5806dda33ddf6aaa66a028f8d43a23"}, + {file = "orjson-3.10.7-cp38-cp38-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:4829cf2195838e3f93b70fd3b4292156fc5e097aac3739859ac0dcc722b27ac0"}, + {file = "orjson-3.10.7-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:1193b2416cbad1a769f868b1749535d5da47626ac29445803dae7cc64b3f5c98"}, + {file = "orjson-3.10.7-cp38-cp38-musllinux_1_2_aarch64.whl", hash = "sha256:4e6c3da13e5a57e4b3dca2de059f243ebec705857522f188f0180ae88badd354"}, + {file = "orjson-3.10.7-cp38-cp38-musllinux_1_2_x86_64.whl", hash = "sha256:c31008598424dfbe52ce8c5b47e0752dca918a4fdc4a2a32004efd9fab41d866"}, + {file = "orjson-3.10.7-cp38-none-win32.whl", hash = "sha256:7122a99831f9e7fe977dc45784d3b2edc821c172d545e6420c375e5a935f5a1c"}, + {file = "orjson-3.10.7-cp38-none-win_amd64.whl", hash = "sha256:a763bc0e58504cc803739e7df040685816145a6f3c8a589787084b54ebc9f16e"}, + {file = "orjson-3.10.7-cp39-cp39-macosx_10_15_x86_64.macosx_11_0_arm64.macosx_10_15_universal2.whl", hash = "sha256:e76be12658a6fa376fcd331b1ea4e58f5a06fd0220653450f0d415b8fd0fbe20"}, + {file = "orjson-3.10.7-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:ed350d6978d28b92939bfeb1a0570c523f6170efc3f0a0ef1f1df287cd4f4960"}, + {file = "orjson-3.10.7-cp39-cp39-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:144888c76f8520e39bfa121b31fd637e18d4cc2f115727865fdf9fa325b10412"}, + {file = "orjson-3.10.7-cp39-cp39-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:09b2d92fd95ad2402188cf51573acde57eb269eddabaa60f69ea0d733e789fe9"}, + {file = "orjson-3.10.7-cp39-cp39-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:5b24a579123fa884f3a3caadaed7b75eb5715ee2b17ab5c66ac97d29b18fe57f"}, + {file = "orjson-3.10.7-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:e72591bcfe7512353bd609875ab38050efe3d55e18934e2f18950c108334b4ff"}, + {file = "orjson-3.10.7-cp39-cp39-musllinux_1_2_aarch64.whl", hash = "sha256:f4db56635b58cd1a200b0a23744ff44206ee6aa428185e2b6c4a65b3197abdcd"}, + {file = "orjson-3.10.7-cp39-cp39-musllinux_1_2_x86_64.whl", hash = "sha256:0fa5886854673222618638c6df7718ea7fe2f3f2384c452c9ccedc70b4a510a5"}, + {file = "orjson-3.10.7-cp39-none-win32.whl", hash = "sha256:8272527d08450ab16eb405f47e0f4ef0e5ff5981c3d82afe0efd25dcbef2bcd2"}, + {file = "orjson-3.10.7-cp39-none-win_amd64.whl", hash = "sha256:974683d4618c0c7dbf4f69c95a979734bf183d0658611760017f6e70a145af58"}, + {file = "orjson-3.10.7.tar.gz", hash = "sha256:75ef0640403f945f3a1f9f6400686560dbfb0fb5b16589ad62cd477043c4eee3"}, +] + [[package]] name = "packaging" version = "24.1" @@ -763,4 +829,4 @@ files = [ [metadata] lock-version = "2.0" python-versions = "^3.12" -content-hash = "eec980888916b8e436ed86dbd2c7de1ee94b9d33b9de3effefe8d70bf576a51d" +content-hash = "03bfacdc4e1c32b9de32fc239cf6219e6c07d189a07fe6d29f8d050fc7166f53" diff --git a/pyproject.toml b/pyproject.toml index e16434c..1f74ff8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -11,6 +11,7 @@ python = "^3.12" pydantic-settings = "^2.5.2" fastapi = "^0.115.0" uvicorn = {extras = ["standard"], version = "^0.31.0"} +orjson = "^3.10.7" [tool.poetry.group.dev.dependencies] diff --git a/reddis/__init__.py b/reddis/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/routers/__init__.py b/routers/__init__.py index 0d94ad0..9a9eba4 100644 --- a/routers/__init__.py +++ b/routers/__init__.py @@ -2,6 +2,7 @@ from fastapi import APIRouter from .swagger import router as swagger_router +from .tg_send import router as tg_send_router from config import conf @@ -12,3 +13,8 @@ router.include_router( prefix=conf.prefix.swagger, tags=["Swagger"], ) +router.include_router( + tg_send_router, + prefix=conf.prefix.tg_v1, + tags=["Telegram"], +) diff --git a/routers/swagger.py b/routers/swagger.py index bef9e1a..c58e147 100644 --- a/routers/swagger.py +++ b/routers/swagger.py @@ -1,15 +1,19 @@ -from fastapi import APIRouter +from fastapi import APIRouter, Depends from fastapi.openapi.docs import get_swagger_ui_html +from fastapi.security import HTTPBasicCredentials from config import conf +from auth import verify_user_pwd router = APIRouter() @router.get("", include_in_schema=False) -async def custom_swagger_ui_html(): +async def custom_swagger_ui_html( + credentials: HTTPBasicCredentials = Depends(verify_user_pwd), +): return get_swagger_ui_html( openapi_url=conf.swagger.openapi_url, title=conf.swagger.title, diff --git a/routers/tg_send.py b/routers/tg_send.py new file mode 100644 index 0000000..66cd441 --- /dev/null +++ b/routers/tg_send.py @@ -0,0 +1,16 @@ +from fastapi import APIRouter, Depends +import logging as log + +from schemas import TelegramSendMessage +from auth import verify_token_admin + +router = APIRouter() + + +@router.post("/send") +async def send_message( + message: TelegramSendMessage, + token: str = Depends(verify_token_admin), +): + log.info(message.chat_id) + return diff --git a/schemas/__init__.py b/schemas/__init__.py index e69de29..03f2be6 100644 --- a/schemas/__init__.py +++ b/schemas/__init__.py @@ -0,0 +1,4 @@ +from .tg_send import TelegramSendMessage + +__all__ = ["TelegramSendMessage", + ] \ No newline at end of file diff --git a/schemas/tg_send.py b/schemas/tg_send.py new file mode 100644 index 0000000..cb7fdad --- /dev/null +++ b/schemas/tg_send.py @@ -0,0 +1,6 @@ +from pydantic import BaseModel + + +class TelegramSendMessage(BaseModel): + chat_id: str + text: str diff --git a/telegram/__init__.py b/telegram/__init__.py new file mode 100644 index 0000000..e69de29