Skip to content

Commit 9320712

Browse files
committed
Setting for defining the frontend and backend host
1 parent fd11216 commit 9320712

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

src/app/core/config.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
11
import os
22
from enum import Enum
33

4-
from pydantic import SecretStr, computed_field
4+
from pydantic import SecretStr, computed_field, field_validator
55
from pydantic_settings import BaseSettings, SettingsConfigDict
66

77

88
class AppSettings(BaseSettings):
99
APP_NAME: str = "FastAPI app"
1010
APP_DESCRIPTION: str | None = None
1111
APP_VERSION: str | None = None
12+
APP_BACKEND_HOST: str = "http://localhost:8000"
13+
APP_FRONTEND_HOST: str | None = None
1214
LICENSE_NAME: str | None = None
1315
CONTACT_NAME: str | None = None
1416
CONTACT_EMAIL: str | None = None
1517

18+
@field_validator("APP_BACKEND_HOST", "APP_FRONTEND_HOST", mode="after")
19+
@classmethod
20+
def validate_hosts(cls, host: str) -> str:
21+
if host is not None and not (host.startswith("http://") or host.startswith("https://")):
22+
raise ValueError(
23+
f"HOSTS must define their protocol and start with http:// or https://. Received the host {host}."
24+
)
25+
return host
26+
1627

1728
class CryptSettings(BaseSettings):
1829
SECRET_KEY: SecretStr = SecretStr("secret-key")
@@ -172,5 +183,14 @@ class Settings(
172183
extra="ignore",
173184
)
174185

186+
@field_validator("APP_FRONTEND_HOST")
187+
@classmethod
188+
def validate_app_frontend_host_protocol(cls, host: str) -> str:
189+
if EnvironmentSettings.ENVIRONMENT == EnvironmentOption.PRODUCTION and not host.startswith("https://"):
190+
raise ValueError(
191+
f"In production, APP_FRONTEND_HOST must start with the https:// protocol. Received the host {host}."
192+
)
193+
return host
194+
175195

176196
settings = Settings()

0 commit comments

Comments
 (0)