config

package
v0.1.35 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 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 (
	// 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"
	// VarAnthropicKey is the platform's key of Anthropic, by which platform AI
	// calls its models: a secret, read from the variable or from the file its
	// twin with the suffix _FILE names — on Cloud Run, a volume of a secret of
	// Secret Manager (R11, R46). Unset, platform AI has no route to Anthropic.
	VarAnthropicKey = "API_ANTHROPIC_KEY"
	// VarOpenAIKey was the platform's key of OpenAI. api gives tutor no adapter
	// of OpenAI, whose models the admission table admits for nothing, so the
	// variable, or its twin _FILE, set is a refusal: a deployment that still
	// passes it learns so at its start rather than holding a key nothing calls
	// (R54).
	VarOpenAIKey = "API_OPENAI_KEY"
	// VarTutorStub, set to 1, puts tutor's calls on a stub in api's process,
	// off a deployment alone: the stub reads a sheet by the shape of its crop and
	// words the tutor's words from a list of its own, and calls no provider
	// (R66; R146 spec). Admission, the prices and the quota stay tutor's own.
	VarTutorStub = "API_TUTOR_STUB"
)

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

View Source
const (
	RungAuthor = "author"
	RungFull   = "full"
)

The stages of admission, as tutor codes them.

View Source
const (
	// VarLegalDir is the absolute path of a self-hosted copy's folder of its
	// operator's legal texts, outside any git working tree; unset, api serves
	// the texts of its tag of spec (legal/README.md of spec, section 8).
	VarLegalDir = "API_LEGAL_DIR"
	// VarConsentFormsBucket is the operator's closed bucket of Cloud Storage the
	// scans of the forms of a parent's consent go to, under the prefix of
	// VarConsentFormsPrefix (R116 spec). A deployment alone names it: api
	// writes with its own account, and off a deployment takes no default
	// credentials.
	VarConsentFormsBucket = "API_CONSENT_FORMS_BUCKET"
	// VarConsentFormsPrefix is api's prefix in the bucket, ending in a slash.
	VarConsentFormsPrefix = "API_CONSENT_FORMS_PREFIX"
	// VarConsentFormsDir is the absolute path of a folder outside any git
	// working tree the scans go to instead, off a deployment: the dev projects
	// and the first app's.
	VarConsentFormsDir = "API_CONSENT_FORMS_DIR"
)

The variables of the texts and the forms of consent (R52).

View Source
const (
	FormsNone   = "none"
	FormsBucket = "bucket"
	FormsFolder = "folder"
)

The stores of the forms, as the lines of startup and admission name them.

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"
	// 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).

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 DefaultPackagesDir = "/packages"

DefaultPackagesDir is where the stage of the Dockerfile lays the packages in the image.

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 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
	// Keys are the platform's keys of the providers, each empty when unset.
	Keys PlatformKeys
	// Stub says whether tutor's calls go to the stub of the dev mode (R66).
	Stub bool
}

Admission is what the deployment declares for the function of admission.

func AdmissionFrom added in v0.1.16

func AdmissionFrom(environ []string) (Admission, error)

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.

The stub of tutor is the dev mode's alone, as R32 refuses the emulator of the sign-in service: deployed, it is refused, and so it is beside a platform key, which nothing would call — with the stub no key of a provider is in the process (R66).

type After added in v0.1.20

type After struct {
	// DatabaseURL is the pooler's connection string of the database api.
	DatabaseURL Secret
	// PackagesDir is the absolute path of the folder of the packages.
	PackagesDir string
}

After is what the job after a rollout opens (R07, R51): the database api, by the pooler's connection string, and the folder of the packages, as serve reads it — of which the job learns what the image no longer gives, and builds the evidence of the attempts it closes. The rights and the sign-in service it does not open. The level of its log is the process's Config, read as serve reads it.

func AfterFrom added in v0.1.20

func AfterFrom(environ []string) (*After, error)

AfterFrom reads what the job after a rollout opens from a set of KEY=VALUE strings, as SweepFrom reads sweep's, and validates it.

type Agreements added in v0.1.21

type Agreements struct {
	// LegalDir is the copy's folder of texts, or empty for spec's.
	LegalDir string
	// FormsBucket and FormsPrefix are the bucket of the scans and api's prefix in
	// it, FormsDir the folder instead; all empty, no store is configured, and
	// SendConsentForm is refused unavailable.
	FormsBucket string
	FormsPrefix string
	FormsDir    string
}

Agreements is where the legal texts come from and where the scans of the forms go.

func AgreementsFrom added in v0.1.21

func AgreementsFrom(environ []string) (Agreements, error)

AgreementsFrom reads where the texts come from and the scans go from a set of KEY=VALUE strings, and validates it: a folder is an absolute path outside any git working tree, a bucket comes with its prefix and only on a deployment — K_SERVICE or CLOUD_RUN_JOB set —, and a bucket and a folder are not both named.

func (Agreements) Forms added in v0.1.21

func (a Agreements) Forms() string

Forms is the store of the scans that is configured, by its word.

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 PlatformKeys added in v0.1.17

type PlatformKeys struct {
	Anthropic Secret
}

PlatformKeys are the platform's keys of the providers whose models platform AI calls (R11, R46): Anthropic's alone, the one provider api gives tutor an adapter of (R54). It is optional, since a provider with no key is no route of platform AI — tutor's Choose takes none of its models.

func (PlatformKeys) ByProvider added in v0.1.17

func (k PlatformKeys) ByProvider() map[string]Secret

ByProvider is each key set, by its provider's code in the admission table: the one list of the providers whose keys the platform may hold.

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
	// PackagesDir is the absolute path of the folder of the packages (R41).
	PackagesDir string
	// Agreements is where the legal texts come from and where the scans of the
	// forms of consent go (R52).
	Agreements Agreements
	// 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

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
	// PackagesDir is the absolute path of the folder of the packages, as serve
	// reads it: the evidence the job writes is built of them (R49).
	PackagesDir string
	// Agreements is where the scans of the forms of consent lie, which the job
	// erases past their term (R52).
	Agreements Agreements
}

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