Add mandatory vMajor endpoints, i.e. all except /versions.
Source code in optimade/server/create_app.py38
39
40
41
42
43 | def add_major_version_base_url(app: FastAPI, index: bool = False):
"""Add mandatory vMajor endpoints, i.e. all except /versions."""
for endpoint in INDEX_ENDPOINTS if index else MAIN_ENDPOINTS:
app.include_router(
endpoint.router, prefix=BASE_URL_PREFIXES["major"], include_in_schema=False
)
|
Add the following OPTIONAL prefixes/base URLs to server:
46
47
48
49
50
51
52
53
54
55
56
57
58
59 | def add_optional_versioned_base_urls(app: FastAPI, index: bool = False):
"""Add the following OPTIONAL prefixes/base URLs to server:
```
/vMajor.Minor
/vMajor.Minor.Patch
```
"""
for version in ("minor", "patch"):
for endpoint in INDEX_ENDPOINTS if index else MAIN_ENDPOINTS:
app.include_router(
endpoint.router,
prefix=BASE_URL_PREFIXES[version],
include_in_schema=False,
)
|
Create and configure a FastAPI app for the OPTIMADE API.
Sets up logging, middleware, exception handlers, routers, and optional test/JSONL data insertion. Can be used for both a regular OPTIMADE API or the index meta-database variant.
Note that the global ServerConfig instance is read from the "OPTIMADE_" env variables or the config json file, but this function allows to override config options for individual apps by passing a custom ServerConfig.
Parameters:
| config | ServerConfig | None |
ServerConfig instance to override config options specific to this app. |
None |
| index | bool |
If True, create an index meta-database instance. |
False |
| logger_tag | str | None |
Optional tag for the logger. |
None |
Returns:
| FastAPI |
Configured FastAPI application. |
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 | def create_app(
config: ServerConfig | None = None,
index: bool = False,
logger_tag: str | None = None,
) -> FastAPI:
"""
Create and configure a FastAPI app for the OPTIMADE API.
Sets up logging, middleware, exception handlers, routers, and optional
test/JSONL data insertion. Can be used for both a regular OPTIMADE API
or the index meta-database variant.
Note that the global ServerConfig instance is read from the "OPTIMADE_"
env variables or the config json file, but this function allows to
override config options for individual apps by passing a custom ServerConfig.
Args:
config: ServerConfig instance to override config options specific to this app.
index: If True, create an index meta-database instance.
logger_tag: Optional tag for the logger.
Returns:
Configured FastAPI application.
"""
# create app-specific logger
logger = create_logger(logger_tag, config)
if config_warnings:
logger.warning(
f"Invalid config file or no config file provided, running server with default settings. Errors: "
f"{[warnings.formatwarning(w.message, w.category, w.filename, w.lineno, '') for w in config_warnings]}"
)
else:
logger.info(
f"Loaded settings from {os.getenv('OPTIMADE_CONFIG_FILE', DEFAULT_CONFIG_FILE_PATH)}"
)
if config is None:
config = ServerConfig()
if config.debug: # pragma: no cover
logger.info("DEBUG MODE")
title = "OPTIMADE API" if not index else "OPTIMADE API - Index meta-database"
description = """The [Open Databases Integration for Materials Design (OPTIMADE) consortium](https://www.optimade.org/) aims to make materials databases interoperational by developing a common REST API.\n"""
if index:
description += 'This is the "special" index meta-database.\n'
description += f"\nThis specification is generated using [`optimade-python-tools`](https://github.com/Materials-Consortia/optimade-python-tools/tree/v{__version__}) v{__version__}."
if index:
config.is_index = True
app = FastAPI(
root_path=config.root_path,
title=title,
description=description,
version=__api_version__,
docs_url=f"{BASE_URL_PREFIXES['major']}/extensions/docs",
redoc_url=f"{BASE_URL_PREFIXES['major']}/extensions/redoc",
openapi_url=f"{BASE_URL_PREFIXES['major']}/extensions/openapi.json",
default_response_class=JSONAPIResponse,
separate_input_output_schemas=False,
)
# Save the config in the app state for access in endpoints
app.state.config = config
# create entry collections and save in app state for access in endpoints
entry_collections = create_entry_collections(config)
app.state.entry_collections = entry_collections
# store also the BaseResourceMapper
app.state.base_resource_mapper = BaseResourceMapper()
if not index:
if config.insert_test_data or config.insert_from_jsonl:
insert_main_data(config, entry_collections, logger)
else:
if config.insert_test_data and config.index_links_path.exists():
insert_index_data(config, entry_collections, logger)
# Add middleware to set logging context
@app.middleware("http")
async def set_context(request, call_next):
set_logging_context(logger_tag)
response = await call_next(request)
return response
# Add CORS middleware
app.add_middleware(CORSMiddleware, allow_origins=["*"])
# Then add required OPTIMADE middleware
for middleware in OPTIMADE_MIDDLEWARE:
app.add_middleware(middleware)
# Enable GZIP after other middleware.
if config.gzip.enabled:
app.add_middleware(
GZipMiddleware,
minimum_size=config.gzip.minimum_size,
compresslevel=config.gzip.compresslevel,
)
# Add exception handlers
for exception, handler in OPTIMADE_EXCEPTIONS:
app.add_exception_handler(exception, handler)
# Add various endpoints to unversioned URL
endpoints = INDEX_ENDPOINTS if index else MAIN_ENDPOINTS
endpoints += [versions]
for endpoint in endpoints:
app.include_router(endpoint.router)
# add the versioned endpoints
add_major_version_base_url(app, index=index)
add_optional_versioned_base_urls(app, index=index)
return app
|