From 8854e43ecf015439035d869f45438a51fb8965c7 Mon Sep 17 00:00:00 2001 From: Anton Kudryavtsev Date: Sun, 21 Jul 2024 17:11:12 +0300 Subject: [PATCH] feat: cors --- main.py | 31 ++++++++++++++++++------------- src/app/http/common.py | 1 + src/app/http/routes/dataset.py | 14 ++++++++++++++ src/app/http/routes/oauth.py | 19 +++++++++++++++++++ src/app/http/server.py | 5 +++-- 5 files changed, 55 insertions(+), 15 deletions(-) create mode 100644 src/app/http/common.py diff --git a/main.py b/main.py index c01af82..ada1eff 100644 --- a/main.py +++ b/main.py @@ -5,8 +5,8 @@ from src.adapter.external.auth.telegram import TelegramOauthAdapter -# from src.adapter.internal.database.memorydb.service import MemoryDBAdapter -from src.adapter.internal.database.mongodb.service import MongoDBAdapter +# from src.adapter.internal.database.mongodb.service import MongoDBAdapter +from src.adapter.internal.database.memorydb.service import MemoryDBAdapter from src.app.http.server import build_server from src.service.allocation import AllocationService from src.service.answer import AnswerService @@ -20,8 +20,8 @@ async def app(): load_dotenv() - repo = await MongoDBAdapter.create("mongodb://localhost:27017/randorm") - # repo = MemoryDBAdapter() + # repo = await MongoDBAdapter.create("mongodb://localhost:27017/randorm") + repo = MemoryDBAdapter() secret_token = os.getenv("SECRET_TOKEN") if secret_token is None: @@ -35,6 +35,10 @@ async def app(): if service_secret_key is None: raise RuntimeError("SERVICE_SECRET_KEY is not set") + base_url = os.getenv("BASE_URL") + if base_url is None: + raise RuntimeError("BASE_URL is not set") + user_service = UserService(repo) allocation_service = AllocationService( @@ -70,15 +74,16 @@ async def app(): oauth_adapter = TelegramOauthAdapter(secret_token, jwt_secret, user_service) return build_server( - service_secret_key, - user_service, - answer_service, - allocation_service, - form_field_service, - participant_service, - preference_service, - room_service, - oauth_adapter, + base_url=base_url, + service_secret_key=service_secret_key, + user_service=user_service, + answer_service=answer_service, + allocation_service=allocation_service, + form_field_service=form_field_service, + participant_service=participant_service, + preference_service=preference_service, + room_service=room_service, + oauth_adapter=oauth_adapter, ) diff --git a/src/app/http/common.py b/src/app/http/common.py new file mode 100644 index 0000000..14cc78e --- /dev/null +++ b/src/app/http/common.py @@ -0,0 +1 @@ +CORS_HEADERS = {"Access-Control-Allow-Origin": "*", "Access-Control-Allow-Headers": "*"} diff --git a/src/app/http/routes/dataset.py b/src/app/http/routes/dataset.py index 4796193..fd9df23 100644 --- a/src/app/http/routes/dataset.py +++ b/src/app/http/routes/dataset.py @@ -1,6 +1,7 @@ import ujson from aiohttp import web +from src.app.http.common import CORS_HEADERS from src.protocol.internal.database.user import ReadUser from src.service.answer import AnswerService from src.service.participant import ParticipantService @@ -38,9 +39,22 @@ def regiter_routers(self, app: web.Application): self.participants_handler, name="dataset_participants_router", ), + web.options( + "/private/dataset/option", + self.options_handler, + name="dataset_option_router", + ), + web.options( + "/private/dataset/answer", + self.options_handler, + name="dataset_answer_router", + ), ] ) + async def options_handler(self, request: web.Request) -> web.Response: + return web.Response(status=200, text="OK", headers=CORS_HEADERS) + async def answer_handler(self, request: web.Request) -> web.Response: secret_key = request.headers.get("X-Secret-Key") if secret_key is None or secret_key != self._secret_key: diff --git a/src/app/http/routes/oauth.py b/src/app/http/routes/oauth.py index edf6de1..5a8b613 100644 --- a/src/app/http/routes/oauth.py +++ b/src/app/http/routes/oauth.py @@ -3,6 +3,7 @@ from aiohttp import web from pydantic import BaseModel +from src.app.http.common import CORS_HEADERS from src.domain.exception.auth import ( AuthException, UserAlreadyExistsException, @@ -49,9 +50,27 @@ def regiter_routers(self, app: web.Application): self.login_handler, name="oauth_login", ), + web.options( + "/oauth/telegram/callback", + self.options_handler, + name="oauth_callback_router", + ), + web.options( + "/oauth/telegram/register", + self.options_handler, + name="oauth_register_router", + ), + web.options( + "/oauth/telegram/login", + self.options_handler, + name="oauth_login_router", + ), ] ) + async def options_handler(self, request: web.Request) -> web.Response: + return web.Response(status=200, text="OK", headers=CORS_HEADERS) + async def callback_handler(self, request: web.Request) -> web.Response: try: payload = request.query diff --git a/src/app/http/server.py b/src/app/http/server.py index 1954faa..2b75a05 100644 --- a/src/app/http/server.py +++ b/src/app/http/server.py @@ -14,6 +14,7 @@ def build_server( + base_url: str, service_secret_key: str, user_service: UserService, answer_service: AnswerService, @@ -27,8 +28,8 @@ def build_server( app = web.Application() oauth.OAuthRouter( - user_form_redirect_url="http://localhost:8080/user/form", - user_profile_redirect_url="http://localhost:8080/user/profile", + user_form_redirect_url=f"{base_url}/form", + user_profile_redirect_url=f"{base_url}/profile", oauth_adapter=oauth_adapter, service=user_service, ).regiter_routers(app)