Documentation
¶
Overview ¶
Package encore is the root of Encore's Go SDK, which Encore applications are built against. It holds Run and Serve, which run a service from main until Encore shuts it down, serving its net/http handler, and Start, which starts the runtime and its subscriptions for a main that serves some other way; the rest of the SDK is its packages.
- sdk.encore.dev/v2/env: the running application, its environment and deployment.
- sdk.encore.dev/v2/storage/postgres, sdk.encore.dev/v2/storage/redis and sdk.encore.dev/v2/storage/objects: databases, caches and buckets.
- sdk.encore.dev/v2/pubsub: topics and subscriptions.
- sdk.encore.dev/v2/cron: cron jobs.
- sdk.encore.dev/v2/secrets: the application's secrets.
- sdk.encore.dev/v2/metrics: counters, gauges and histograms.
- sdk.encore.dev/v2/services: reaching the application's other services.
- sdk.encore.dev/v2/frontends and sdk.encore.dev/v2/gateways: where the application's frontends and gateways are reached.
- sdk.encore.dev/v2/reqctx: Request Context, typed values that travel with a request to every service it reaches, such as its user.
- sdk.encore.dev/v2/shutdown: the graceful shutdown.
- sdk.encore.dev/v2/gateway: Middleware in the gateway a service hosts, which authenticates, limits and meters public requests.
Encore's API framework, a service's typed endpoints declared with an //encore:api directive, is a module of its own built on this one: framework.encore.dev.
For more information visit https://encore.dev/docs.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Run ¶
func Run(h http.Handler, opts ...ServeOption)
Run runs the service until Encore shuts it down, serving h as Serve does, and then exits the process: with code 0 if the shutdown completed in time, else 1, having written why to stderr. It never returns, so main's deferred calls don't run; a main with work to do once the service has stopped calls Serve instead.
func main() {
mux := http.NewServeMux()
mux.HandleFunc("GET /orders/{id}", getOrder)
encore.Run(mux)
}
func Serve ¶
func Serve(h http.Handler, opts ...ServeOption) error
Serve runs the service until Encore shuts it down, and returns once the shutdown is complete: nil if it completed in time, else why not. The process's exit is main's, as Run makes it for a main with nothing more to do:
if err := encore.Serve(mux); err != nil {
log.Fatal(err)
}
With a handler, it serves h where Encore assigns the service, ENCORE_LISTEN_ADDR (outside Encore on the port PORT names, or 8080), over HTTP/1.1 and unencrypted HTTP/2 (h2c). Each request has its trace and Request Context in its context, and a server span named after the route it matched, as tracing.Middleware records it. With nil, as for a service that only handles Pub/Sub messages, it runs the rest.
It listens, starts the runtime and the service's subscriptions as Start does, and serves. On SIGTERM or SIGINT, or shutdown.Begin, the server takes no new requests, and Encore closes the infrastructure only once the requests in flight are answered and the app's other drains have returned (see sdk.encore.dev/v2/shutdown), or the time to drain is over. A second signal stops waiting for them. Serve then returns shutdown.ErrTimeout or shutdown.ErrFailed if the shutdown did not complete in time or its cleanup failed.
It returns at once if it cannot listen, as when the address is taken. If serving fails later, it shuts down as on a signal and returns the failure.
Outside Encore, as under a plain go run, it records no spans, and handles SIGTERM and SIGINT itself: the server answers the requests in flight, and the drains registered with shutdown.OnShutdown run.
func Start ¶
func Start()
Start starts the Encore runtime and the app's Pub/Sub subscriptions. Call it in main, once the app is ready to handle messages:
func main() {
encore.Start()
// …serve the app's API…
}
Other infrastructure starts on first use, so a database or bucket needs no Start. A subscription is never used, only handed messages: it handles none until Start is called, and Encore logs a warning when an app that declares subscriptions has not called it. Shortly after Start, the runtime warns of each subscription the service hosts that has no handler, such as one declared in a package the program does not import.
Serve and Run call Start, for a service they serve.
Later calls do nothing. Outside Encore, as under a plain go run, there is nothing to start.
Types ¶
type ServeOption ¶
type ServeOption func(*runConfig)
ServeOption configures Serve and Run.
func WithServer ¶
func WithServer(fn func(*http.Server)) ServeOption
WithServer has fn configure the http.Server Serve serves, before it serves: its timeouts, ErrorLog or ConnContext, say. Its Addr and Handler are Serve's, and are set after fn.
func WithoutTracing ¶
func WithoutTracing() ServeOption
WithoutTracing serves the handler without a server span for its requests: for one that traces them itself, as with otelhttp. Requests then have no trace or Request Context in their context unless the handler extracts them, as otelhttp does with tracing.Propagator.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package appruntime provides internal functionality for use by Encore to run Encore applications.
|
Package appruntime provides internal functionality for use by Encore to run Encore applications. |
|
clients
Package clients sends the calls of the service clients Encore generates, which fail with a *services.CallError and take services.CallOption values and services.ClientSettings: see sdk.encore.dev/v2/services.
|
Package clients sends the calls of the service clients Encore generates, which fail with a *services.CallError and take services.CallOption values and services.ClientSettings: see sdk.encore.dev/v2/services. |
|
exported/stack
Package stack collects stack traces.
|
Package stack collects stack traces. |
|
infrasdk/sdkcore
Package sdkcore links the Encore SDK core Rust library (crates/encore_sdk_core) over its C ABI and exposes its infrastructure integrations — secrets, SQL, Pub/Sub, object storage, and metrics — to the Go SDK.
|
Package sdkcore links the Encore SDK core Rust library (crates/encore_sdk_core) over its C ABI and exposes its infrastructure integrations — secrets, SQL, Pub/Sub, object storage, and metrics — to the Go SDK. |
|
shared/appconf
Package appconf answers whether the process runs under Encore.
|
Package appconf answers whether the process runs under Encore. |
|
shared/cloud
Package cloud contains some helpers for referring to different cloud implementations
|
Package cloud contains some helpers for referring to different cloud implementations |
|
shared/drain
Package drain holds the app's shutdown drains, registered with sdk.encore.dev/v2/shutdown.OnShutdown and run when the shutdown begins.
|
Package drain holds the app's shutdown drains, registered with sdk.encore.dev/v2/shutdown.OnShutdown and run when the shutdown begins. |
|
shared/serverspan
Package serverspan is how the handlers a request reaches tell the server span Encore started for it, as encore.Serve does for every request, what they know of it: the route it matched, and whether it is sensitive.
|
Package serverspan is how the handlers a request reaches tell the server span Encore started for it, as encore.Serve does for every request, what they know of it: the route it matched, and whether it is sensitive. |
|
Package config provides typed application config: values a service reads, written in TOML or JSON files beside the code, and changed while the app runs.
|
Package config provides typed application config: values a service reads, written in TOML or JSON files beside the code, and changed while the app runs. |
|
Package cron provides support for cron jobs: recurring tasks that run on a schedule.
|
Package cron provides support for cron jobs: recurring tasks that run on a schedule. |
|
Package encoretest gives each test infrastructure of its own: databases, Redis databases and buckets no other test sees, and a record of what it published.
|
Package encoretest gives each test infrastructure of its own: databases, Redis databases and buckets no other test sees, and a record of what it published. |
|
system
Package system is what only a system test does: wait for the app to settle, reach it with a client of the test's own, and read what it did.
|
Package system is what only a system test does: wait for the app to settle, reach it with a client of the test's own, and read what it did. |
|
system/chromedp
Package encorechromedp drives a system test's browser with chromedp.
|
Package encorechromedp drives a system test's browser with chromedp. |
|
Package env describes the running Encore application: its environment, build and deployment, and its gateways and frontends.
|
Package env describes the running Encore application: its environment, build and deployment, and its gateways and frontends. |
|
Package frontends says where the app's frontends are reached.
|
Package frontends says where the app's frontends are reached. |
|
Package gateway runs code in an application's gateway: Middleware that authenticates, authorizes, limits, logs or meters the public requests the gateway serves, before they reach a service and after they return.
|
Package gateway runs code in an application's gateway: Middleware that authenticates, authorizes, limits, logs or meters the public requests the gateway serves, before they reach a service and after they return. |
|
Package gateways says where the app's gateways are reached.
|
Package gateways says where the app's gateways are reached. |
|
internal
|
|
|
clientcall
Package clientcall prepares the calls of the service clients Encore generates, for sdk.encore.dev/v2/appruntime/clients and its gRPC package alike: where a call goes, with which headers, through which transport and by when.
|
Package clientcall prepares the calls of the service clients Encore generates, for sdk.encore.dev/v2/appruntime/clients and its gRPC package alike: where a call goes, with which headers, through which transport and by when. |
|
reqfields
Package reqfields holds the Request Context of a context in its wire form, and the configs of the fields declared in this process: what package reqctx keeps typed, and what tracing, pubsub and services move.
|
Package reqfields holds the Request Context of a context in its wire form, and the configs of the fields declared in this process: what package reqctx keeps typed, and what tracing, pubsub and services move. |
|
secretplaceholder
Package secretplaceholder marks the secret placeholders in a config's value as the runtime's, so that a secrets.Secret decoded from JSON names a secret only when config decodes it.
|
Package secretplaceholder marks the secret placeholders in a config's value as the runtime's, so that a secrets.Secret decoded from JSON names a secret only when config decodes it. |
|
svclookup
Package svclookup lets the generated service clients reach the app's services as sdk.encore.dev/v2/services does, which sets its variables when it is initialized: the generated clients import services, so they are set before any client is used.
|
Package svclookup lets the generated service clients reach the app's services as sdk.encore.dev/v2/services does, which sets its variables when it is initialized: the generated clients import services, so they are set before any client is used. |
|
systest
Package systest is what encoretest and encoretest/system share of system tests: how a test that ended says so to its test session.
|
Package systest is what encoretest and encoretest/system share of system tests: how a test that ended says so to its test session. |
|
testruntime
Package testruntime hands the SDK core a runtime config in tests.
|
Package testruntime hands the SDK core a runtime config in tests. |
|
testscope
Package testscope is which test a resource call belongs to, for the resource packages to use the test's own instances: the scope encoretest.Begin puts on the context, or, for a call whose context has none, the test whose goroutine makes it (see Scope.ClaimGoroutine), or else the one test that is running.
|
Package testscope is which test a resource call belongs to, for the resource packages to use the test's own instances: the scope encoretest.Begin puts on the context, or, for a call whose context has none, the test whose goroutine makes it (see Scope.ClaimGoroutine), or else the one test that is running. |
|
teststop
Package teststop stops a test's child process as a shutdown signal does: SIGTERM on Unix, and on Windows, which has no SIGTERM, Ctrl-Break, which Go delivers as os.Interrupt (SIGINT) and the SDK shuts down on as it does on SIGTERM.
|
Package teststop stops a test's child process as a shutdown signal does: SIGTERM on Unix, and on Windows, which has no SIGTERM, Ctrl-Break, which Go delivers as os.Interrupt (SIGINT) and the SDK shuts down on as it does on SIGTERM. |
|
Package metrics records metrics: counters, gauges and histograms of what an app does.
|
Package metrics records metrics: counters, gauges and histograms of what an app does. |
|
Package pubsub provides Pub/Sub topics and subscriptions.
|
Package pubsub provides Pub/Sub topics and subscriptions. |
|
Package redaction applies deployed policy to telemetry copies, not live data.
|
Package redaction applies deployed policy to telemetry copies, not live data. |
|
Package reqctx provides Request Context: typed fields that travel with a request to every service it reaches, such as the user it is made for.
|
Package reqctx provides Request Context: typed fields that travel with a request to every service it reaches, such as the user it is made for. |
|
Package secrets provides access to an application's secrets.
|
Package secrets provides access to an application's secrets. |
|
Package services calls the app's other services: it resolves their base URLs, and sends them the trace and Request Context of the calls made to them.
|
Package services calls the app's other services: it resolves their base URLs, and sends them the trace and Request Context of the calls made to them. |
|
grpcclient
Package grpcclient holds the streams of the gRPC clients Encore generates, and sends their calls: in the gRPC protocol, over HTTP/2, through sdk.encore.dev/v2/services, so that they carry the trace and Request Context to the app's services.
|
Package grpcclient holds the streams of the gRPC clients Encore generates, and sends their calls: in the gRPC protocol, over HTTP/2, through sdk.encore.dev/v2/services, so that they carry the trace and Request Context to the app's services. |
|
Package shutdown lets an app take part in Encore's graceful shutdown, with the app's main in charge of the process.
|
Package shutdown lets an app take part in Encore's graceful shutdown, with the app's main in charge of the process. |
|
storage
|
|
|
clickhouse
Package clickhouse provides Encore services access to their ClickHouse databases.
|
Package clickhouse provides Encore services access to their ClickHouse databases. |
|
objects
Package objects provides Encore applications with the ability to create and use Object Storage buckets (like for example Amazon S3) for storing and retrieving files in a cloud-agnostic manner.
|
Package objects provides Encore applications with the ability to create and use Object Storage buckets (like for example Amazon S3) for storing and retrieving files in a cloud-agnostic manner. |
|
postgres
Package postgres provides Encore services direct access to their Postgres databases.
|
Package postgres provides Encore services direct access to their Postgres databases. |
|
postgres/sqlerr
Package sqlerr provides a set of common error codes for SQL datbaases.
|
Package sqlerr provides a set of common error codes for SQL datbaases. |
|
redis
Package redis provides Redis databases.
|
Package redis provides Redis databases. |
|
redis/internal/gencommands
command
Command gencommands writes the redis package's command groups, commands_gen.go, from the table in table.go.
|
Command gencommands writes the redis package's command groups, commands_gen.go, from the table in table.go. |
|
Package tracing records spans through Encore's telemetry pipeline.
|
Package tracing records spans through Encore's telemetry pipeline. |
|
internal/testhook
Package testhook holds the tracer provider tracing.TracerProvider returns in place of the runtime's while one is set: tracingtest records Encore's own spans with it.
|
Package testhook holds the tracer provider tracing.TracerProvider returns in place of the runtime's while one is set: tracingtest records Encore's own spans with it. |
|
tracingtest
Package tracingtest records spans without the Encore runtime, for tests of code that traces.
|
Package tracingtest records spans without the Encore runtime, for tests of code that traces. |