Skip to main content

Crate encore

Crate encore 

Source
Expand description

The native Rust SDK for Encore.

Unlike the Go and TypeScript SDKs (which reach the runtime over a C ABI / napi), this crate links the Rust encore_sdk_core directly — Rust calling Rust, no marshalling layer.

Infrastructure resources are declared as module-scope statics using const constructors, mirroring Encore’s other SDKs:

ⓘ
use encore::{pubsub, secrets};
use encore::storage::postgres;
use encore::pubsub::Topic;
use serde::{Serialize, Deserialize};

static DB: postgres::Database = postgres::Database::new("hello").migrations("./migrations");

#[derive(Serialize, Deserialize)]
struct SignupEvent { user_id: String }

static SIGNUPS: Topic<SignupEvent> = Topic::new("signups");

static API_KEY: secrets::Secret = secrets::Secret::new("ApiKey");

A service is a directory with an encore.service.json ({ "name": ..., "lang": "rust" }) and ordinary Rust code. Trait-based services with #[encore::service] / #[endpoint] and HTTP serving are a later milestone.

Re-exports§

pub use env::app_meta;
pub use ::log;
pub use encore_sdk_core::telemetry::opentelemetry;

Modules§

config
Application config: typed values a service reads, written in TOML or JSON files beside the code, and changed while the app runs.
cron
Cron jobs: requests Encore makes of a service on a schedule.
env
Metadata about the running application — its environment, build, deployment, API gateways and frontends.
frontends
Where the app’s frontends are reached.
gateways
Where the app’s gateways are reached.
metrics
Metrics: counters, gauges and histograms of what an app does.
objects
Object storage buckets. Declared at module scope; resolved from the runtime on use.
pubsub
Pub/Sub topics and subscriptions. Topics are typed by the message they carry and declared at module scope; a subscription names its topic and the function that handles its messages.
reqctx
Request Context: typed fields that travel with a request to every service it reaches, such as the user it is made for.
secrets
Application secrets. Declared at module scope and resolved from the runtime.
services
Calling the app’s other services.
shutdown
Graceful shutdown on SIGTERM or SIGINT (Ctrl+C/Ctrl+Break on Windows).
storage
Storage resources: databases an app declares and Encore provisions.
telemetry
Application-owned telemetry APIs.
testing
Tests. Under encore test every test gets infrastructure of its own — databases, buckets and topics no other test sees — with nothing to set up:

Macros§

fixture
Declares fixtures: Fixtures, with less to write.

Structs§

Error
An error returned by an Encore SDK operation or an API endpoint handler.

Enums§

ErrCode
A gRPC-style error code, the same set the rest of Encore uses.

Functions§

initialize
Eagerly initialize the runtime, so an app fails fast at startup rather than on first resource use. Optional — any resource access triggers initialization.
run_blocking
Block the calling thread until a shutdown signal, then drain infrastructure and exit. The usual way to keep a service process alive that serves no requests of its own.
tokio_handle
A handle to the core runtime’s tokio runtime, for apps that need to spawn onto it (e.g. background infra tasks).

Attribute Macros§

test
Marks a test, as #[tokio::test] does, and tells Encore about it before it runs. See testing.