Skip to content

Deployment and operations ​

APP_KEY and the endpoint prefix ​

The render fragment and Linker POST routes share one site-specific prefix derived from APP_KEY (/{prefix}/render, /{prefix}/linker…):

text
prefix    = hex(HMAC-SHA256(APP_KEY, "cg:endpoint-prefix:v1"))[..16]
meta name = "m-" + hex(HMAC-SHA256(APP_KEY, "cg:endpoint-meta:v1"))[..16]
  • Every server for one site must use the same key, or a page served by machine A gets 404 from machine B.
  • After changing the key, the prefix, sessions and signed URLs are all invalid; pages already open must be reloaded.
  • With the same key as the PHP version you get the same prefix (watch for route conflicts when both run on one domain).
  • The prefix isn't a secret; it just makes each site's endpoints live somewhere different, so generic scanning is harder.

endpoints::prefix() returns the current prefix, endpoints::meta_name() the meta name, and endpoints::derive(purpose, key) is the underlying function. With an empty APP_KEY the prefix is sixteen 0s; always set a key in production.

Linker storage ​

Stateful instances and locks use Laravel for Rust's default cache store (CACHE_STORE in .env; the starter defaults to database).

ItemValueDescription
Instance keylinker:{user}:{uuid}Stores the serialized struct, rev and expiry
Lock keylinker:dispatch:{user}:{instance}Queues calls on the same instance
Idle timeout30 minutes (store::IDLE)Slides forward on each call
Renewal interval90 seconds (store::RENEW_INTERVAL)Frequent calls don't rewrite the expiry every time
Maximum lifetime8 hours (store::MAX_LIFETIME)Counted from mount; an absolute limit
Lock lease60 secondsMust exceed the longest single call
Lock wait3 secondsOn timeout returns service ("操作忙碌中", busy)

These are constants, not settings. To change them, edit crates/cg-component/src/runtime/linker/store.rs and mod.rs.

  • Several machines: the cache store must be shared (Redis or the same database); file / memory won't do.
  • Clearing the cache clears every in-progress flow, and users get expired on their next action.
  • Deploying a change to a Linker struct's fields: old instances fail to deserialize → expired. #[serde(default)] lets new fields read old data, for a smooth upgrade.

Don't lengthen lifetimes to let users "resume"

Server instances don't survive a reload (the uuid goes away with the DOM). To let users resume, write a DB draft row.

Sessions and render tokens ​

Render tokens live in the session (key cg_render_before, at most 16). So:

  • The session driver must be shared across machines (the starter defaults to database).
  • Session expires → tokens are invalid → fragment requests get 419 and the browser console shows an error; the user has to reload the page.

Production ​

  • APP_DEBUG=false: removes error details from Linker fatal responses and error pages.
  • When HTTPS is terminated by a proxy in front, check Laravel for Rust's trusted proxy settings, or the scheme of signed URLs won't match (Stateless calls get 403).
  • Build the front-end assets with npm run build; resources/js/cg/ and cg-component.css are already in the Vite entries.
  • /up is the starter's built-in health check route.

Health checks and CI ​

Rust has no php artisan linker:list --health. Instead:

bash
cargo test --workspace
  • tests/runtime.rs walks through the whole protocol: meta tags, one-time tokens, the prefix, Stateful state and rev, other users' instances, logged-out calls, Stateless signature renewal.
  • crates/cg-component/tests/php_parity.rs checks that component output matches the PHP snapshots.
  • The one-to-one check of the action table is in Commands.

In CI, in-memory SQLite is enough for tests:

bash
DB_CONNECTION=sqlite DATABASE_URL='sqlite::memory:' cargo test --workspace