|
| 1 | +from enum import StrEnum |
| 2 | + |
| 3 | +from pydantic import Field, field_validator, model_validator |
| 4 | +from pydantic.networks import IPvAnyAddress |
1 | 5 | from pydantic_settings import BaseSettings, SettingsConfigDict |
2 | 6 |
|
3 | 7 |
|
| 8 | +class LogLevel(StrEnum): |
| 9 | + debug = "debug" |
| 10 | + info = "info" |
| 11 | + warning = "warning" |
| 12 | + error = "error" |
| 13 | + critical = "critical" |
| 14 | + |
| 15 | + |
4 | 16 | class Settings(BaseSettings): |
5 | 17 | # database |
6 | 18 | database_url: str |
7 | 19 | database_echo: bool = False |
8 | | - database_pool_size: int = 5 |
9 | | - database_max_overflow: int = 10 |
| 20 | + database_pool_size: int = Field(default=5, ge=1) |
| 21 | + database_max_overflow: int = Field(default=10, ge=1) |
10 | 22 | database_pool_pre_ping: bool = True |
11 | 23 |
|
12 | 24 | # log |
13 | 25 | log_formatter: str = ( |
14 | 26 | "asctime=%(asctime)s level=%(levelname)s pathname=%(pathname)s line=%(lineno)s message=%(message)s" |
15 | 27 | ) |
16 | | - log_level: str = "info" |
| 28 | + log_level: LogLevel = LogLevel.info |
17 | 29 |
|
18 | 30 | # subscription defaults |
19 | | - subscription_max_attempts: int = 5 |
20 | | - subscription_backoff_min_seconds: int = 5 |
21 | | - subscription_backoff_max_seconds: int = 300 |
| 31 | + subscription_max_attempts: int = Field(default=5, ge=1) |
| 32 | + subscription_backoff_min_seconds: int = Field(default=5, ge=1) |
| 33 | + subscription_backoff_max_seconds: int = Field(default=300, ge=1) |
22 | 34 |
|
23 | 35 | # api |
24 | 36 | api_debug: bool = False |
25 | | - api_host: str = "0.0.0.0" |
26 | | - api_port: int = 8000 |
27 | | - api_num_workers: int = 1 |
| 37 | + api_host: IPvAnyAddress = Field(default="0.0.0.0") |
| 38 | + api_port: int = Field(default=8000, ge=1) |
| 39 | + api_num_workers: int = Field(default=1, ge=1) |
28 | 40 |
|
29 | 41 | # workers |
30 | | - cleanup_acked_messages_older_than_seconds: int = 3600 |
31 | | - cleanup_stuck_messages_lock_timeout_seconds: int = 60 |
| 42 | + cleanup_acked_messages_older_than_seconds: int = Field(default=3600, ge=1) |
| 43 | + cleanup_stuck_messages_lock_timeout_seconds: int = Field(default=60, ge=1) |
32 | 44 |
|
33 | 45 | # load .env |
34 | 46 | model_config = SettingsConfigDict(env_file=".env", env_prefix="fastpubsub_") |
35 | 47 |
|
| 48 | + @field_validator("database_url") |
| 49 | + def validate_database_url_format(cls, v: str): |
| 50 | + if not v.startswith("postgresql+psycopg://"): |
| 51 | + raise ValueError("must start with 'postgresql+psycopg://'") |
| 52 | + return v |
| 53 | + |
| 54 | + @model_validator(mode="after") |
| 55 | + def check_subscription_backoff_order(self) -> "Settings": |
| 56 | + if self.subscription_backoff_max_seconds < self.subscription_backoff_min_seconds: |
| 57 | + raise ValueError( |
| 58 | + "subscription_backoff_max_seconds must be greater than or equal to subscription_backoff_min_seconds" |
| 59 | + ) |
| 60 | + return self |
| 61 | + |
36 | 62 |
|
37 | 63 | settings = Settings() |
0 commit comments