config

package
v0.1.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 9, 2026 License: Apache-2.0 Imports: 12 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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.

View Source
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.

View Source
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).

View Source
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.

View Source
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.

View Source
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

View Source
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

func LoadFrom(environ []string) (*Config, error)

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

func (c *Config) CloseTimeout() time.Duration

CloseTimeout is the part of the way out kept for closing what the service ran on: a quarter of it.

func (*Config) Deployed

func (c *Config) Deployed() bool

Deployed reports whether the process is running as a deployed service.

func (*Config) DrainTimeout

func (c *Config) DrainTimeout() time.Duration

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

func MigrateFrom(environ []string) (*Migrate, error)

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

func OperatorFrom(environ []string) (*Operator, error)

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.

func (Secret) Format

func (Secret) Format(f fmt.State, _ rune)

Format is what fmt prints for every verb, so that none of them — %d, %x, %q — reflects its way to the value.

func (Secret) GoString

func (Secret) GoString() string

GoString is what fmt prints for %#v.

func (Secret) LogValue

func (Secret) LogValue() slog.Value

LogValue is what slog writes.

func (Secret) Reveal

func (s Secret) Reveal() string

Reveal is the value itself.

func (Secret) String

func (Secret) String() string

String is what fmt prints for %v and %s.

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

func ServeFrom(environ []string, cfg *Config) (*Serve, error)

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.

func SweepFrom added in v0.1.9

func SweepFrom(environ []string) (*Sweep, error)

SweepFrom reads what sweep opens from a set of KEY=VALUE strings, as ServeFrom reads what serve opens, and validates it.

func (*Sweep) Deployed added in v0.1.9

func (s *Sweep) Deployed() bool

Deployed reports whether the job runs as a deployed Cloud Run job.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL