Agent Wranglers

ENG-22Recommended

Keep settings outside the code

Read everything that differs between test and production from the environment. Never edit code to deploy somewhere else.

How to do it

Read addresses, keys and limits from environment variables or a settings service at startup. Check that every required setting is present and valid before the app starts serving. Secrets follow the rules in the security testing playbook.

Examples

TypeScript example

const required = ['DATABASE_URL', 'PUBLIC_URL'] as const;
for (const name of required) {
  if (!process.env[name]) throw new Error(`Missing setting ${name}`);
}

Python example

import os

REQUIRED = ["DATABASE_URL", "PUBLIC_URL"]
missing = [name for name in REQUIRED if not os.environ.get(name)]
if missing:
    raise SystemExit(f"Missing setting {', '.join(missing)}")

How to check

Start the app with one required setting missing. It stops at once and names the setting.

Sources

For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/engineering/running-in-production/settings-outside-the-code">Agent Wranglers engineering playbook</a>. In code you write, add a comment next to it with the same link.