11"""Postgres API configuration."""
22
3- from typing import List , Optional , Type
3+ from typing import List , Type
44from urllib .parse import quote_plus as quote
55
66from pydantic import BaseModel , field_validator
7- from pydantic_settings import SettingsConfigDict
7+ from pydantic_settings import BaseSettings , SettingsConfigDict
88from stac_fastapi .types .config import ApiSettings
99
1010from stac_fastapi .pgstac .types .base_item_cache import (
@@ -43,7 +43,7 @@ class ServerSettings(BaseModel):
4343 model_config = SettingsConfigDict (extra = "allow" )
4444
4545
46- class Settings ( ApiSettings ):
46+ class PostgresSettings ( BaseSettings ):
4747 """Postgres-specific API settings.
4848
4949 Attributes:
@@ -57,12 +57,12 @@ class Settings(ApiSettings):
5757 invalid_id_chars: list of characters that are not allowed in item or collection ids.
5858 """
5959
60- postgres_user : Optional [ str ] = None
61- postgres_pass : Optional [ str ] = None
62- postgres_host_reader : Optional [ str ] = None
63- postgres_host_writer : Optional [ str ] = None
64- postgres_port : Optional [ int ] = None
65- postgres_dbname : Optional [ str ] = None
60+ postgres_user : str
61+ postgres_pass : str
62+ postgres_host_reader : str
63+ postgres_host_writer : str
64+ postgres_port : int
65+ postgres_dbname : str
6666
6767 db_min_conn_size : int = 10
6868 db_max_conn_size : int = 10
@@ -71,9 +71,28 @@ class Settings(ApiSettings):
7171
7272 server_settings : ServerSettings = ServerSettings ()
7373
74+ model_config = {"env_file" : ".env" , "extra" : "ignore" }
75+
76+ @property
77+ def reader_connection_string (self ):
78+ """Create reader psql connection string."""
79+ return f"postgresql://{ self .postgres_user } :{ quote (self .postgres_pass )} @{ self .postgres_host_reader } :{ self .postgres_port } /{ self .postgres_dbname } "
80+
81+ @property
82+ def writer_connection_string (self ):
83+ """Create writer psql connection string."""
84+ return f"postgresql://{ self .postgres_user } :{ quote (self .postgres_pass )} @{ self .postgres_host_writer } :{ self .postgres_port } /{ self .postgres_dbname } "
85+
86+ @property
87+ def testing_connection_string (self ):
88+ """Create testing psql connection string."""
89+ return f"postgresql://{ self .postgres_user } :{ quote (self .postgres_pass )} @{ self .postgres_host_writer } :{ self .postgres_port } /pgstactestdb"
90+
91+
92+ class Settings (ApiSettings ):
7493 use_api_hydrate : bool = False
75- base_item_cache : Type [BaseItemCache ] = DefaultBaseItemCache
7694 invalid_id_chars : List [str ] = DEFAULT_INVALID_ID_CHARS
95+ base_item_cache : Type [BaseItemCache ] = DefaultBaseItemCache
7796
7897 cors_origins : str = "*"
7998 cors_methods : str = "GET,POST,OPTIONS"
@@ -89,45 +108,3 @@ def parse_cors_origin(cls, v):
89108 def parse_cors_methods (cls , v ):
90109 """Parse CORS methods."""
91110 return [method .strip () for method in v .split ("," )]
92-
93- @property
94- def reader_connection_string (self ):
95- """Create reader psql connection string."""
96- self ._validate_postgres_settings ()
97- return f"postgresql://{ self .postgres_user } :{ quote (self .postgres_pass )} @{ self .postgres_host_reader } :{ self .postgres_port } /{ self .postgres_dbname } "
98-
99- @property
100- def writer_connection_string (self ):
101- """Create writer psql connection string."""
102- self ._validate_postgres_settings ()
103- return f"postgresql://{ self .postgres_user } :{ quote (self .postgres_pass )} @{ self .postgres_host_writer } :{ self .postgres_port } /{ self .postgres_dbname } "
104-
105- @property
106- def testing_connection_string (self ):
107- """Create testing psql connection string."""
108- self ._validate_postgres_settings ()
109- return f"postgresql://{ self .postgres_user } :{ quote (self .postgres_pass )} @{ self .postgres_host_writer } :{ self .postgres_port } /pgstactestdb"
110-
111- def _validate_postgres_settings (self ) -> None :
112- """Validate that required PostgreSQL settings are configured."""
113- required_settings = [
114- "postgres_host_writer" ,
115- "postgres_host_reader" ,
116- "postgres_user" ,
117- "postgres_pass" ,
118- "postgres_port" ,
119- "postgres_dbname" ,
120- ]
121-
122- missing = [
123- setting for setting in required_settings if getattr (self , setting ) is None
124- ]
125-
126- if missing :
127- raise ValueError (
128- f"Missing required PostgreSQL settings: { ', ' .join (missing )} " ,
129- )
130-
131- model_config = SettingsConfigDict (
132- ** {** ApiSettings .model_config , ** {"env_nested_delimiter" : "__" }}
133- )
0 commit comments