Documentation
¶
Overview ¶
Package ormweb serves an orm.Databases in a github.com/vuka-lang/web app: the databases open when the app starts (migrated in development) and close gracefully when it stops; *orm.Databases and the connection handles (@orm.Database) are dependencies; each request's context carries the databases and a read-your-writes Scope; GET /healthz reports every database's state (and, in development, its pool and errors); a model or handle naming a database the app doesn't declare fails the app's Build. In development, the app's toolbar lists each request's statements in its SQL panel.
app := web.New(
ormweb.Databases(
orm.Declare("main", os.Getenv("DATABASE_URL"), orm.AsDefault()),
orm.Declare("analytics", os.Getenv("ANALYTICS_URL"), orm.ReadOnly()),
).MigrateOnStart(true),
web.Use(ormweb.UsingFrom(tenantOf)), // a tenant's database per request
)
app.Run("")
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DetailedHealthHandler ¶ added in v0.4.0
DetailedHealthHandler answers with s's whole Report: the summary, and each database's pool, breaker, last error (the driver's text) and replicas (their redacted URLs). Serve it to operators only.
func HealthHandler ¶
HealthHandler answers with s's Summary as JSON: 200, or 503 when the default database is down. It says nothing of errors, pools or replicas, so it can be public.
func Middleware ¶
func Middleware(s *orm.Databases) web.Middleware
Middleware puts s and a read-your-writes Scope on each request's context.
func UsingFrom ¶
func UsingFrom(name func(*http.Request) string) web.Middleware
UsingFrom sends each request's queries to the database name gives for it (a tenant's, by host or header); "" leaves them routed as they are.
app.Use(ormweb.UsingFrom(func(r *http.Request) string { return "tenant_" + r.Header.Get("X-Tenant") }))
Types ¶
type DBState ¶ added in v0.4.0
type DBState struct {
Name string `json:"name"`
Default bool `json:"default"`
State orm.State `json:"state"`
}
DBState is a database's state, as Summary reports it.
type Plugin ¶
Plugin is the web.Option Databases and Set return; its methods tune it before web.New applies it.
func Databases ¶
func Databases(opts ...orm.OpenOption) *Plugin
Databases serves the databases opts declare (orm.Declare), with their routers and settings (orm.Routers, orm.Logger, orm.OnOpen, …); with none declared, the app's start reads DATABASE_URL and DATABASE_URL_<NAME>.
func Set ¶
func Set(s *orm.Databases, opts ...orm.OpenOption) *Plugin
Set serves a Databases made elsewhere (orm.NewDatabases), opts applied to it, opened by the app's start.
func (*Plugin) Health ¶
Health serves the databases' health at path (GET /healthz by default; "" serves none).
func (*Plugin) HealthDetails ¶ added in v0.4.0
HealthDetails has the health route answer each database's full Report (pool statistics, breaker, last error, replicas) rather than its state alone. By default it does only in development (web.IsDev, by the real environment): a driver's error text and replica addresses are for operators, not whoever reaches the route.
func (*Plugin) MigrateOnStart ¶
MigrateOnStart applies pending migrations once the databases open (orm.Migrate). By default it does only in development (web.IsDev: VUKA_ENV=development in the program's real environment, never an env file). Production migrates when this says so, or with the orm migrate command.
type Report ¶
Report is what the health endpoint answers: Status "ok", "degraded" (a database or replica isn't answering) or "down" (the default database isn't), and each database's stats.