Documentation
¶
Overview ¶
Package config reads the service's configuration from the environment.
Environment variables are the only source: there is no flag and no remote source, and a secret may come from a file only by a variable that names it (see Secret). They are read here and nowhere else, so that every value has one origin and one validation.
Only the variables the service actually uses are here. A variable arrives with the code that consumes it, because a required variable with no reader is a deployment that fails for no reason.
Index ¶
Constants ¶
const ( VarPort = "PORT" VarLogLevel = "API_LOG_LEVEL" VarReadHeaderTimeout = "API_HTTP_READ_HEADER_TIMEOUT" VarReadTimeout = "API_HTTP_READ_TIMEOUT" VarWriteTimeout = "API_HTTP_WRITE_TIMEOUT" VarIdleTimeout = "API_HTTP_IDLE_TIMEOUT" VarShutdownTimeout = "API_SHUTDOWN_TIMEOUT" VarRequestTimeout = "API_REQUEST_TIMEOUT" VarService = "K_SERVICE" // VarMigrateDatabaseURL is the direct connection string of the database api, // which migrate takes: a secret, read from the variable or from the file its // twin with the suffix _FILE names (R29). VarMigrateDatabaseURL = "API_MIGRATE_DATABASE_URL" // VarMigrateOpenFGADatabaseURL is the direct connection string of the database // openfga, the store of the rights, which migrate takes the same way (R31). VarMigrateOpenFGADatabaseURL = "API_MIGRATE_OPENFGA_DATABASE_URL" )
The names of the variables. The service's own carry the prefix API_; PORT and K_SERVICE are the names Cloud Run sets.
const ( DefaultPort = "8080" DefaultLogLevel = "info" DefaultReadHeaderTimeout = 5 * time.Second DefaultReadTimeout = 30 * time.Second DefaultWriteTimeout = 60 * time.Second DefaultIdleTimeout = 120 * time.Second DefaultShutdownTimeout = 9 * time.Second DefaultRequestTimeout = 30 * time.Second )
Defaults for every optional variable. They are named constants rather than literals in LoadFrom so that the documentation and the code cannot drift.
const ( // VarDatabaseURL is the pooler's connection string of the database api: a // secret, read as VarMigrateDatabaseURL is. VarDatabaseURL = "API_DATABASE_URL" // VarOpenFGADatabaseURL is the pooler's connection string of the database // openfga, the store of the rights. VarOpenFGADatabaseURL = "API_OPENFGA_DATABASE_URL" // VarFirebaseProjectID is the Firebase project whose tokens api accepts. VarFirebaseProjectID = "API_FIREBASE_PROJECT_ID" // VarFirebaseCredentials is the key of a service account of that project: a // secret, off a deployment only. Deployed, api takes the token of its // service's own account from the metadata server, with no key. VarFirebaseCredentials = "API_FIREBASE_CREDENTIALS" // VarAppOrigin is the exact origin of app, which CORS answers when api has an // origin of its own; unset, api stands on app's origin and answers no CORS. VarAppOrigin = "API_APP_ORIGIN" )
The variables of the command serve beyond the server's own (R29, R31, R32, R33).
const CloudRunStopGrace = 10 * time.Second
CloudRunStopGrace is how long Cloud Run waits after it asks an instance to stop before it kills it. A deployed way out has to end before then, or the close it keeps time for never runs.
const MinShutdownTimeout = time.Second
MinShutdownTimeout is the shortest way out a service may be given: the close after the drain has a quarter of it, and a quarter of less is no time to close anything in.
Variables ¶
var ErrInvalid = errors.New("config: invalid")
ErrInvalid is returned by LoadFrom; callers branch on it with errors.Is.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// Port is the TCP port to listen on, as plain digits.
Port string
// LogLevel is the lowest level the log writes.
LogLevel slog.Level
// ReadHeaderTimeout is how long a client may take to send its headers.
ReadHeaderTimeout time.Duration
// ReadTimeout is how long a client may take to send its whole request.
ReadTimeout time.Duration
// WriteTimeout is how long the server may take to send its answer.
WriteTimeout time.Duration
// IdleTimeout is how long a connection is kept open between requests.
IdleTimeout time.Duration
// ShutdownTimeout is the whole way out once a stop is asked for: the
// server's drain and the close after it together. The platform stops the
// process ten seconds after asking it to, so the default stops a second
// short of it.
ShutdownTimeout time.Duration
// RequestTimeout is the longest a request may run, its calls of the ports
// among it; a client's Connect-Timeout-Ms may shorten it (R33). It is under
// WriteTimeout, so that the answer of a request cut short still goes out.
RequestTimeout time.Duration
// Service is the name K_SERVICE carries. Cloud Run sets it on every
// service and a developer's machine does not, which is what makes it the
// gate for what a deployment must not be given.
Service string
}
Config is the whole configuration of the process.
func LoadFrom ¶
LoadFrom reads the configuration from a set of KEY=VALUE strings, in the shape os.Environ returns them, applies the defaults and validates the result. The source is an argument — the command hands over os.Environ — so that a test can hand over an environment instead of changing the one the process runs in.
A variable set to an empty string counts as unset: an empty value is what a deployment leaves behind when it stops setting something, and it should not silently override a default with nothing.
func (*Config) CloseTimeout ¶
CloseTimeout is the part of the way out kept for closing what the service ran on: a quarter of it.
func (*Config) DrainTimeout ¶
DrainTimeout is the part of the way out the server waits for the requests still in flight: three quarters of it. The drain and the close after it share one budget in fixed shares, so that a slow request cannot eat the time the close needs.
type Migrate ¶ added in v0.1.1
type Migrate struct {
// LogLevel is the lowest level the log writes.
LogLevel slog.Level
// DatabaseURL is the database's direct connection string. Neon's pooler
// keeps no session, and goose locks the migration by an advisory lock of
// the session, so migrate never goes through the pooler.
DatabaseURL Secret
// OpenFGADatabaseURL is the direct connection string of the database openfga,
// which migrate brings the store of the rights in, by the same lock (R31).
OpenFGADatabaseURL Secret
}
Migrate is the configuration of the command migrate: the level of its log and the direct connection strings of the databases it migrates, api and openfga (R29, R31).
func MigrateFrom ¶ added in v0.1.1
MigrateFrom reads the configuration of migrate from a set of KEY=VALUE strings, as LoadFrom reads the server's, and validates it.
type Secret ¶
type Secret struct {
// contains filtered or unexported fields
}
Secret is a value that must reach only the code it is for: a key, a token, a connection string. Printing it — by fmt with any verb, by the log, in an error — gives "[redacted]"; only Reveal gives the value, and the one caller that needs it calls that.
type Serve ¶ added in v0.1.4
type Serve struct {
// DatabaseURL is the pooler's connection string of the database api (R29).
DatabaseURL Secret
// OpenFGADatabaseURL is the pooler's connection string of the database
// openfga (R31).
OpenFGADatabaseURL Secret
// FirebaseProjectID is the Firebase project of the tokens (R32).
FirebaseProjectID string
// FirebaseCredentials is a service account's key off a deployment; deployed,
// none (R32).
FirebaseCredentials Secret
// AppOrigin is the exact origin of app, or empty for none (R33).
AppOrigin string
}
Serve is what serve opens beyond the server: the databases api and openfga through the pooler, the sign-in service and the origin of app.
func ServeFrom ¶ added in v0.1.4
ServeFrom reads what serve opens from a set of KEY=VALUE strings, as MigrateFrom reads migrate's, and validates it. Deployed — cfg.Deployed — a key of the service account is refused; off a deployment it is required: the machine's default credentials, which might be gcloud's, are never taken.