Skip to content

main_index

The OPTIMADE Index Meta-Database server

The server is based on MongoDB, using either pymongo or mongomock.

This is an example implementation with example data. To implement your own index meta-database server see the documentation at https://optimade.org/optimade-python-tools.

add_major_version_base_url(app)

Add mandatory endpoints to /vMAJOR base URL.

Source code in optimade/server/main_index.py
134
135
136
137
def add_major_version_base_url(app: FastAPI):
    """Add mandatory endpoints to `/vMAJOR` base URL."""
    for endpoint in (index_info, links):
        app.include_router(endpoint.router, prefix=BASE_URL_PREFIXES["major"])

add_optional_versioned_base_urls(app)

Add the following OPTIONAL prefixes/base URLs to server:

    /vMajor.Minor
    /vMajor.Minor.Patch

Source code in optimade/server/main_index.py
140
141
142
143
144
145
146
147
148
149
def add_optional_versioned_base_urls(app: FastAPI):
    """Add the following OPTIONAL prefixes/base URLs to server:
    ```
        /vMajor.Minor
        /vMajor.Minor.Patch
    ```
    """
    for version in ("minor", "patch"):
        app.include_router(index_info.router, prefix=BASE_URL_PREFIXES[version])
        app.include_router(links.router, prefix=BASE_URL_PREFIXES[version])

lifespan(app) async

Add dynamic endpoints and adjust config on startup.

Source code in optimade/server/main_index.py
44
45
46
47
48
49
50
51
52
53
54
@asynccontextmanager  # type: ignore[arg-type]
async def lifespan(app: FastAPI):
    """Add dynamic endpoints and adjust config on startup."""
    CONFIG.is_index = True
    # Add API endpoints for MANDATORY base URL `/vMAJOR`
    add_major_version_base_url(app)
    # Add API endpoints for OPTIONAL base URLs `/vMAJOR.MINOR` and `/vMAJOR.MINOR.PATCH`
    add_optional_versioned_base_urls(app)

    # Yield so that the app can start
    yield