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 ( // VarAdmissionFactsFile is the absolute path of the file of the deployment's // facts (the admission text of spec, section 5), as the path of a secret's // file is given; on Cloud Run, a volume of an exact version of a secret. Its // type and how it is read are tutor's. VarAdmissionFactsFile = "API_ADMISSION_FACTS_FILE" // VarAdmissionRung is the least stage of tutor's rule of admission of // models, which api hands to Table.Select: "author" only in the first app, // "full" anywhere else. VarAdmissionRung = "API_ADMISSION_RUNG" )
The variables of admission to AI (R25, R45): what the deployment declares, beside its rollout list in infra, and the first app in a file of the author's own (the amendment of 2026-11-08 to R106 spec).
const ( RungAuthor = "author" RungFull = "full" )
The stages of admission, as tutor codes them.
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" // VarPackagesDir is the folder of the packages of modules that api packages // laid: in the image, DefaultPackagesDir; for the first app, a folder outside // git (R41). VarPackagesDir = "API_PACKAGES_DIR" )
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 DefaultPackagesDir = "/packages"
DefaultPackagesDir is where the stage of the Dockerfile lays the packages in the image.
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.
const VarJob = "CLOUD_RUN_JOB"
VarJob is the name of the Cloud Run job being run, which Cloud Run sets in every task of a job: deployed, sweep runs as one, and the variable says so, as K_SERVICE says it of serve.
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 Admission ¶ added in v0.1.16
type Admission struct {
// FactsFile is the absolute path of the file of facts, or empty for none:
// no fact then holds, and platform AI is closed.
FactsFile string
// Rung is the least stage, RungAuthor or RungFull.
Rung string
// RungSet says whether the deployment named the stage; unset, it is
// RungFull, closed by default as the function of admission is.
RungSet bool
}
Admission is what the deployment declares for the function of admission.
func AdmissionFrom ¶ added in v0.1.16
AdmissionFrom reads what the deployment declares for admission from a set of KEY=VALUE strings, and validates it. Deployed — K_SERVICE set, as serve runs on Cloud Run, or CLOUD_RUN_JOB, as api check runs before the migrations — the stage "author" is refused: it is the first app's alone, where the author alone learns, and any deployment, even one where the author alone learns, passes "full" (R25). Only Cloud Run's variables tell a deployment here: one of its own elsewhere passes "full" by its own rule, as it keeps its own facts (R25). The file of facts holds no secret, but it lies outside any git working tree as a secret's file does: the first app's is the author's own, outside the repository.
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 Operator ¶ added in v0.1.5
type Operator struct {
// LogLevel is the lowest level the log writes.
LogLevel slog.Level
// DatabaseURL is the pooler's connection string of the database api.
DatabaseURL Secret
}
Operator is the configuration of a command of the operator that writes to the database api, such as invite (R34): the level of its log and the pooler's connection string, which serve takes too. Its writes keep no session, so the pooler serves them as it serves the requests (R29).
func OperatorFrom ¶ added in v0.1.5
OperatorFrom reads the configuration of a command of the operator from a set of KEY=VALUE strings, as MigrateFrom reads migrate'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
// PackagesDir is the absolute path of the folder of the packages (R41).
PackagesDir string
// Admission is what the deployment declares for admission to AI (R25, R45).
Admission Admission
}
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.
type Sweep ¶ added in v0.1.9
type Sweep struct {
// DatabaseURL is the pooler's connection string of the database api.
DatabaseURL Secret
// OpenFGADatabaseURL is the pooler's connection string of the database
// openfga.
OpenFGADatabaseURL Secret
// FirebaseProjectID is the Firebase project whose users the job lists.
FirebaseProjectID string
// FirebaseCredentials is a service account's key off a deployment;
// deployed, none (R32).
FirebaseCredentials Secret
// Job is the name of the Cloud Run job, or empty off a deployment.
Job string
}
Sweep is what the daily job sweep opens (R21, R38): what serve opens but the origin of app. The level of its log and the longest a request may run — a mark or a tuple younger than that may be a request's still going — are the process's Config, read as serve reads them, so that a deployment that gives both the same variables gives them the same values.