44from pydantic import HttpUrl
55
66from syftai_space .components .marketplaces .handlers import MarketplaceHandler
7+ from syftai_space .components .settings .repository import SettingsRepository
78from syftai_space .components .settings .schemas import PublicUrlResponse
89from syftai_space .components .shared .syfthub_client import SyftHubClient , SyftHubError
910from syftai_space .components .tenants .entities import Tenant
@@ -14,33 +15,37 @@ class SettingsHandler:
1415 """Handler for settings business logic."""
1516
1617 def __init__ (
17- self , marketplace_handler : MarketplaceHandler , config : AppSettings
18+ self ,
19+ settings_repository : SettingsRepository ,
20+ marketplace_handler : MarketplaceHandler ,
21+ config : AppSettings ,
1822 ) -> None :
1923 """Initialize the settings handler.
2024
2125 Args:
26+ settings_repository: Repository for settings persistence
2227 marketplace_handler: Marketplace handler for syncing to SyftHub
2328 config: Application settings
2429 """
30+ self .settings_repository = settings_repository
2531 self .marketplace_handler = marketplace_handler
2632 self .config = config
2733
2834 def get_public_url (self ) -> PublicUrlResponse :
29- """Get the current public URL.
35+ """Get the current public URL from database (source of truth) .
3036
3137 Returns:
3238 Public URL response
3339 """
34- return PublicUrlResponse (
35- public_url = str (self .config .public_url ) if self .config .public_url else None
36- )
40+ settings = self .settings_repository .get_settings ()
41+ return PublicUrlResponse (public_url = settings .public_url )
3742
3843 def update_public_url (
3944 self , tenant : Tenant , new_url : HttpUrl | str
4045 ) -> PublicUrlResponse :
4146 """Update the public URL.
4247
43- Updates the local config and syncs to SyftHub marketplace.
48+ Updates the database (source of truth) and syncs to SyftHub marketplace.
4449
4550 Args:
4651 tenant: Tenant context
@@ -52,19 +57,18 @@ def update_public_url(
5257 Raises:
5358 HTTPException: If sync to marketplace fails
5459 """
55- # Convert to HttpUrl if str, then store
56- if isinstance (new_url , str ):
57- new_url = HttpUrl (new_url )
60+ # Convert to string for storage
61+ url_str = str (new_url ) if new_url else None
5862
59- # Update local config
60- self .config . public_url = new_url
63+ # Update database (source of truth)
64+ self .settings_repository . update_public_url ( url_str )
6165
6266 # Sync to SyftHub if marketplace is configured
6367 try :
6468 marketplace = self .marketplace_handler .get_default_marketplace (tenant )
6569 with SyftHubClient (str (marketplace .url )) as syfthub :
6670 syfthub .login (marketplace .email , marketplace .password )
67- syfthub .update_profile (domain = str ( new_url ) )
71+ syfthub .update_profile (domain = url_str )
6872 except HTTPException as e :
6973 if e .status_code == 404 :
7074 # No marketplace configured, just update local config
@@ -77,4 +81,12 @@ def update_public_url(
7781 detail = f"Failed to sync public URL to marketplace: { e .message } " ,
7882 ) from e
7983
80- return PublicUrlResponse (public_url = str (new_url ))
84+ return PublicUrlResponse (public_url = url_str )
85+
86+ def initialize_from_config (self ) -> None :
87+ """Initialize settings from config on startup.
88+
89+ If SYFT_PUBLIC_URL env var is set, it overwrites the database value.
90+ """
91+ if self .config .public_url :
92+ self .settings_repository .update_public_url (str (self .config .public_url ))
0 commit comments