Factory
Router factory functions for creating and registering DiracX routers with proper configuration and dependency injection.
The factory provides the infrastructure for dynamically loading and configuring routers at application startup.
factory
Logic for creating and configuring the FastAPI application.
Attributes
T = TypeVar('T')
module-attribute
T2 = TypeVar('T2', bound=(BaseSQLDB | BaseOSDB))
module-attribute
DIRACX_MIN_CLIENT_VERSION = '0.0.1a1'
module-attribute
_db_alive_cache = TTLCache(maxsize=1024, ttl=10)
module-attribute
Classes
ClientMinVersionCheckMiddleware
Bases: BaseHTTPMiddleware
Custom FastAPI middleware to verify that the client has the required minimum version.
Source code in diracx-routers/src/diracx/routers/factory.py
Attributes
min_client_version = get_min_client_version()
instance-attribute
parsed_min_client_version = parse(self.min_client_version)
instance-attribute
Functions
__init__(app)
dispatch(request, call_next)
async
Source code in diracx-routers/src/diracx/routers/factory.py
is_version_too_old(client_version)
Verify that client version is ge than min.
Source code in diracx-routers/src/diracx/routers/factory.py
Functions
configure_logger()
Configure the console logger.
Access logs come from uvicorn, which configure its logger in a certain way
(https://github.com/tiangolo/fastapi/discussions/7457)
This method adds a timestamp to the uvicorn output,
and define a console handler for all the diracx loggers
We cannot configure just the root handler, as uvicorn
attaches handler to the uvicorn logger
Source code in diracx-routers/src/diracx/routers/factory.py
create_app_inner(*, enabled_systems, all_service_settings, database_urls, os_database_conn_kwargs, config_source, all_access_policies)
This method does the heavy lifting work of putting all the pieces together.
When starting the application normally, this method is called by create_app, and the values of the parameters are taken from environment variables or entrypoints.
When running tests, the parameters are mocks or test settings.
We rely on the dependency_override mechanism to implement the actual behavior we are interested in for settings, DBs or policy. This allows an extension to override any of these components
:param enabled_system:
this contains the name of all the routers we have to load
:param all_service_settings:
list of instance of each Settings type required
:param database_urls:
dict
Source code in diracx-routers/src/diracx/routers/factory.py
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 | |
create_app()
Load settings from the environment and create the application object.
The configuration may be placed in .env files pointed to by environment variables DIRACX_SERVICE_DOTENV. They can be followed by "_X" where X is a number, and the order is respected.
We then loop over all the diracx.services definitions.
A specific route can be disabled with an environment variable
DIRACX_SERVICE_
We attempt to load each setting classes to make sure that the settings are correctly defined.
Source code in diracx-routers/src/diracx/routers/factory.py
dirac_error_handler(request, exc)
http_response_handler(request, exc)
route_unavailable_error_hander(request, exc)
validation_error_handler(request, exc)
async
Source code in diracx-routers/src/diracx/routers/factory.py
find_dependents(obj, cls)
Source code in diracx-routers/src/diracx/routers/factory.py
is_db_unavailable(db)
async
Cache the result of pinging the DB (exceptions are not cacheable).
Source code in diracx-routers/src/diracx/routers/factory.py
db_transaction(db)
async
Initiate a DB transaction.
Source code in diracx-routers/src/diracx/routers/factory.py
get_min_client_version()
Extracting min client version from entry_points and searching for extension.