Skip to content

Commit

Permalink
feat: cors
Browse files Browse the repository at this point in the history
  • Loading branch information
dartt0n committed Jul 21, 2024
1 parent c3c06d7 commit 8854e43
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 15 deletions.
31 changes: 18 additions & 13 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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(
Expand Down Expand Up @@ -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,
)


Expand Down
1 change: 1 addition & 0 deletions src/app/http/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CORS_HEADERS = {"Access-Control-Allow-Origin": "*", "Access-Control-Allow-Headers": "*"}
14 changes: 14 additions & 0 deletions src/app/http/routes/dataset.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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:
Expand Down
19 changes: 19 additions & 0 deletions src/app/http/routes/oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions src/app/http/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@


def build_server(
base_url: str,
service_secret_key: str,
user_service: UserService,
answer_service: AnswerService,
Expand All @@ -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)
Expand Down

0 comments on commit 8854e43

Please sign in to comment.