Infra
Configuration and credentials
Two sources, in order: an encrypted credentials file if one exists, then environment variables, which always win. No globals, no init(): config.Load() returns a struct once, at startup, and every field is validated then, so a missing setting is a refusal to boot rather than a nil a request trips over an hour later.
The environment variables
Since v0.3.0, a setting's variable is its path in the credentials file, upper-cased, with no app prefix: server.addr is SERVER_ADDR, database.url is DATABASE_URL. Only the variables that control the process itself keep the app's name: a service called blog reads BLOG_ENV, BLOG_CREDENTIALS, BLOG_MASTER_KEY, BLOG_CONFIG_DIR and BLOG_TEST_DATABASE_URL.
| Variable | What it selects |
|---|---|
<NAME>_ENV | A closed set: development, test or production. Selects behaviour. |
<NAME>_CREDENTIALS | Any name; selects which credentials file to read. Defaults to <NAME>_ENV. |
<NAME>_MASTER_KEY | The decryption key, handed in by a deployment so no key file has to exist in the image. |
SERVER_ADDR | The listen address, default :8080. |
DATABASE_URL | The database. Development defaults to the compose stack on :5440; production has no default and must be given. Tests read only <NAME>_TEST_DATABASE_URL. |
SERVER_LOG_LEVEL | debug, info, warn or error. |
DATABASE_MIGRATE_AT_BOOT | true by default; set to false only for a deployment that migrates from one place on purpose. |
(There is also SERVER_SHUTDOWN_GRACE, how long the server waits for in-flight requests on SIGTERM, and <NAME>_CONFIG_DIR for running the binary from somewhere other than the app root. Both have sensible defaults.)
<NAME>_ENV and <NAME>_CREDENTIALS are deliberately separate. <NAME>_ENV is a closed set with no "staging" member on purpose, because every behavioural switch in the app reads it, and a typo there should fail loudly rather than fall through to production behaviour silently. A staging deployment is:
BLOG_ENV=production BLOG_CREDENTIALS=staging
production strictness, staging secrets, which is what "staging" means here: not a third environment, but production behaviour reading a different credentials file.
The credentials files
Committed ciphertext, and a key that is never committed:
config/credentials.yml.enc + config/master.key development
config/credentials/staging.yml.enc + config/credentials/staging.key
config/credentials/production.yml.enc + config/credentials/production.key
Each file is groups at the top level, with no section named after the app. A key's path gives its environment variable: server.addr is overridden by SERVER_ADDR, so a value can move between the file and the environment without renaming anything.
database:
url: postgres://postgres:postgres@localhost:5440/blog_development?sslmode=disable
server:
addr: ":8080"
log_level: debug
The bundled Postgres port, 5440, is written into this file and into the defaults in config/config.go. Changing the port in docker-compose.yml alone is not enough: change database.url too (or set DATABASE_URL and <NAME>_TEST_DATABASE_URL), or the app keeps dialing 5440.
Adding settings: bogie g config
Since v0.3.0, bogie g config adds your own settings, typed and validated at boot:
bogie g config smtp address:req port:int=1025 password:secret # a group: config/smtp.go
bogie g config database pool_size:int=10 # keys into an app group
bogie g config hello=world # a flat top-level key
bogie d config smtp # remove a group
bogie g config smtp address:req port:int=1025 username password:secret prints one line per edit, then how to read the value:
create config/smtp.go
insert config/config.go: Smtp SmtpConfig `mapstructure:"smtp"`
insert config/config.go: c.Smtp.defaults(env)
insert config/config.go: c.Smtp.validate(c.Env),
insert config/credentials.yml.enc: smtp.address
insert config/credentials.yml.enc: smtp.port
insert config/credentials.yml.enc: smtp.username
insert config/credentials.yml.enc: smtp.password
next read it as s.Config.Smtp.Address; set it in production with make credentials ENV=production, or SMTP_ADDRESS
A flat key, bogie g config support_email=help@example.com, writes no file of its own:
insert config/config.go: SupportEmail string `mapstructure:"support_email"`
insert config/credentials.yml.enc: support_email
next read it as s.Config.SupportEmail; set it in production with make credentials ENV=production, or SUPPORT_EMAIL
A type Bogie does not know is refused. bogie g config smtp port:float prints:
bogie: generate config: port: "float" is neither a type (string int bool duration) nor a flag (req secret)
neither a type (string int bool duration) nor a flag (req secret): use one of those words after the colon.
Types are string (the default), int, bool (yes/no/on/off) and duration; flags are req and secret. A secret gets no development value: set it with make credentials. New top-level entries go in alphabetical order; keys in a group stay in the order typed. The app's own server and database groups take new keys but are never created or removed. Read a value as s.Config.Smtp.Address; its variable is SMTP_ADDRESS.
bogie credentials:edit
bogie credentials:edit -e production
make credentials ENV=staging
bogie credentials:edit opens development's file with its key; -e production (or make credentials ENV=production) opens that environment's own file, making it and its key the first time. A key opens only the file it was made for: development's key can never read production's file.
Test never reads a credentials file. It runs from defaults and the environment only, so tests need nobody's secrets and can never accidentally open the development database.
In a container
The encrypted files travel in the image; the keys never do. At runtime the key for whichever credentials file is selected arrives as <NAME>_MASTER_KEY, read directly with no file to write and no permissions to get wrong. See Deploying for where that value comes from.