Skip to content

config

Configuration of the FastAPI server.

ServerConfig

Bases: OptimadeServerConfig

This class stores server config parameters in a way that can be easily extended for new config file types.

Source code in optimade_gateway/common/config.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
class ServerConfig(OptimadeServerConfig):
    """This class stores server config parameters in a way that
    can be easily extended for new config file types.

    """

    databases_collection: str = Field(
        "databases",
        description="Mongo collection name for `/databases` endpoint resources.",
    )
    gateways_collection: str = Field(
        "gateways",
        description="Mongo collection name for `/gateways` endpoint resources.",
    )
    queries_collection: str = Field(
        "queries",
        description="Mongo collection name for `/queries` endpoint resources.",
    )
    load_optimade_providers_databases: bool = Field(
        True,
        description=(
            "Whether or not to load all valid OPTIMADE providers' databases from the "
            "[Materials-Consortia list of OPTIMADE providers]"
            "(https://providers.optimade.org) on server startup."
        ),
    )

    @validator("mongo_uri")
    def replace_with_env_vars(cls, value: str) -> str:
        """Replace string variables with environment variables, if possible"""
        res = value
        for match in re.finditer(r"\{[^{}]+\}", value):
            string_var = match.group()[1:-1]
            env_var = os.getenv(
                string_var, os.getenv(string_var.upper(), os.getenv(string_var.lower()))
            )
            if env_var is not None:
                res = res.replace(match.group(), env_var)
            else:
                warn(
                    OptimadeGatewayWarning(
                        detail=(
                            "Could not find an environment variable for "
                            f"{match.group()!r} from mongo_uri: {value}"
                        )
                    )
                )
        return res

replace_with_env_vars(value)

Replace string variables with environment variables, if possible

Source code in optimade_gateway/common/config.py
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
@validator("mongo_uri")
def replace_with_env_vars(cls, value: str) -> str:
    """Replace string variables with environment variables, if possible"""
    res = value
    for match in re.finditer(r"\{[^{}]+\}", value):
        string_var = match.group()[1:-1]
        env_var = os.getenv(
            string_var, os.getenv(string_var.upper(), os.getenv(string_var.lower()))
        )
        if env_var is not None:
            res = res.replace(match.group(), env_var)
        else:
            warn(
                OptimadeGatewayWarning(
                    detail=(
                        "Could not find an environment variable for "
                        f"{match.group()!r} from mongo_uri: {value}"
                    )
                )
            )
    return res