@@ -69,30 +69,35 @@ def to_dict(self):
6969 }
7070
7171
72- def load_config () -> Config :
73- """Load configuration from the provided JSON file and environment variables."""
74- try :
75- with open (CONFIG_FILE ) as f :
76- config = json .load (f )
77- except FileNotFoundError :
78- print (f"Config file { CONFIG_FILE } not found." )
79- raise
80- except json .JSONDecodeError :
81- print (f"Config file { CONFIG_FILE } is not a valid JSON file." )
82- raise
83-
84- load_dotenv ()
85- config ["env" ] = {
86- var .replace (ACCEPTED_ENVIRONMENT_VARIABLE_PREFIX , "" , 1 ).lower (): value
87- for var in ACCEPTED_ENVIRONMENT_VARIABLES
88- if (value := os .getenv (var ))
89- }
90- log .info (f"Loaded config: { config } " )
91- return Config (config )
72+ _config_instance : Config = None
9273
9374
94- config = load_config ()
75+ def load_config () -> Config :
76+ """Load configuration from the provided JSON file and environment variables."""
77+ global _config_instance
78+ if _config_instance is None :
79+ try :
80+ with open (CONFIG_FILE ) as f :
81+ config = json .load (f )
82+ except FileNotFoundError :
83+ log .error (f"Config file { CONFIG_FILE } not found." )
84+ raise
85+ except json .JSONDecodeError :
86+ log .error (f"Config file { CONFIG_FILE } is not a valid JSON file." )
87+ raise
88+
89+ load_dotenv ()
90+ config ["env" ] = {
91+ var .replace (ACCEPTED_ENVIRONMENT_VARIABLE_PREFIX , "" , 1 ).lower (): value
92+ for var in ACCEPTED_ENVIRONMENT_VARIABLES
93+ if (value := os .getenv (var ))
94+ }
95+ log .info (f"Loaded config: { config } " )
96+ _config_instance = Config (config )
97+ return _config_instance
9598
9699
97100def get_config () -> Config :
98- return config
101+ if _config_instance is None :
102+ raise RuntimeError ("Config not initialized. Call load_config() first." )
103+ return _config_instance
0 commit comments