🏷️ computer-science 🏷️ personal-opinion 🏷️ tech

# Configuration, Environments and Secrets

# Configuration

A way to easily find and change the behavior of a program based on different intentions by changing the values of appropriate variables.

For example:

So, configuration should be:

# Environments

A collection of values for different configuration variables defining a particular intention.

For example:

So, environments should be:

# Secrets

Values that are not meant to be shared with the world.

For example:

So, secrets should be:

# Combining All 3

# Proof of Concept

# Python implementation

# settings.py

import os
from pathlib import Path
from typing import Callable, TypeVar

T = TypeVar("T")

def env(**kwargs: T | Callable[[], T]) -> T:
    val = kwargs[ENVIRONMENT]
    return val() if callable(val) else val

def readfile(path: str | Path, type_: Callable[[str], T] = str) -> Callable[[], T]:
    return lambda: type_(Path(path).read_text().strip())


ENVIRONMENT = os.environ.get("ENVIRONMENT", "dev")

DATABASE_URL = env(
    dev="dev_url",
    prod=readfile("/home/user/secrets/database_url"),
)
copy success


✏️ Edit this note