Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ poetry run alembic revision --autogenerate -m "변경 설명"
| `FANCALL_OPENAI_MODEL` | `gpt-4o-mini` | 사용할 OpenAI LLM 모델 |
| `HEDRA_ENABLED` | `false` | Hedra 아바타 활성화 |
| `HEDRA_API_KEY` | - | Hedra API 키 (enabled=true일 때 필수) |
| `CORS_ALLOWED_ORIGINS` | `http://localhost:3000` | 쿠키를 실은 요청을 허용할 프런트 origin (쉼표로 구분). 운영에서는 실제 도메인으로 설정 |

> **참고**: LiveKit, 데이터베이스, 모델 등 추가 설정은 기본값으로 로컬 개발 가능합니다.
> 변경이 필요한 경우 `fancall/settings.py`의 Settings 클래스를 참고하세요.
Expand Down
23 changes: 23 additions & 0 deletions backend/fancall/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,29 @@ def check_timeline(self) -> "FancallCallSettings":
return self


class CorsSettings(BaseSettings):
"""CORS settings for the API server.

The frontend fetches with ``credentials: "include"``, so browsers reject a
wildcard origin and every allowed origin must be listed explicitly.

Attributes:
allowed_origins: Comma-separated list of origins allowed to make
credentialed requests (CORS_ALLOWED_ORIGINS). Defaults to the local
Next.js dev server.
"""

allowed_origins: str = "http://localhost:3000"

class Config:
env_prefix = "CORS_"

@property
def origins(self) -> list[str]:
"""Parsed list of allowed origins; blank entries are ignored."""
return [o.strip() for o in self.allowed_origins.split(",") if o.strip()]


class LiveKitSettings(BaseSettings):
"""Settings for LiveKit API integration

Expand Down
4 changes: 2 additions & 2 deletions backend/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

from fancall.api.router import create_fancall_router
from fancall.factories import LiveRoomRepositoryFactory
from fancall.settings import LiveKitSettings
from fancall.settings import CorsSettings, LiveKitSettings

# Configure logging
logging.basicConfig(
Expand Down Expand Up @@ -85,7 +85,7 @@
# CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Configure appropriately for production
allow_origins=CorsSettings().origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
Expand Down
28 changes: 28 additions & 0 deletions backend/tests/unit/test_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""
Unit tests for fancall settings
"""

import os
import unittest
from unittest.mock import patch

from fancall.settings import CorsSettings


class TestCorsSettings(unittest.TestCase):
"""Test cases for CorsSettings"""

def test_defaults_to_local_dev_origin(self) -> None:
"""Without configuration only the local dev frontend is allowed"""
with patch.dict(os.environ, clear=True):
self.assertEqual(CorsSettings().origins, ["http://localhost:3000"])

def test_parses_comma_separated_origins(self) -> None:
"""Origins come from the environment, trimmed, without blanks"""
env = {"CORS_ALLOWED_ORIGINS": "https://a.com, https://b.com,,"}
with patch.dict(os.environ, env, clear=True):
self.assertEqual(CorsSettings().origins, ["https://a.com", "https://b.com"])


if __name__ == "__main__":
unittest.main()
Loading