Agora

You're viewing the docs for Config 0.x. The latest is 1.x (rc). Go to the 1.x (rc) docs

Getting started

Install the server and a tenant, and move the first key.

The server

node ace add @adonis-agora/config
config/config.ts
import env from '#start/env'
import { defineConfig, sharedSecretVerifier, stores } from '@adonis-agora/config'

export default defineConfig({
  topology: 'control-plane',
  verifyTenant: sharedSecretVerifier({
    partitions: {
      streaming: env.get('CONFIG_SECRET_STREAMING'),
      billing: env.get('CONFIG_SECRET_BILLING'),
    },
  }),
  // Default: the tables in your database, created at boot.
  store: stores.lucid(),
})

The server mounts, under /_config:

  • GET /v1/config — a tenant's document, with ETag;
  • GET /v1/config/stream — server-sent events on every change;
  • GET /v1/whoami — what the presented token is.

Exempt the tenant API from Shield's CSRF check: tenants are servers, they carry no cookie.

config/shield.ts (the server)
import { configCsrfExceptions } from '@adonis-agora/config'

csrf: { enabled: true, exceptRoutes: (ctx) => configCsrfExceptions(ctx.request.url()) },

A tenant

node ace add @adonis-agora/config
config/config.ts (an app)
import env from '#start/env'
import { defineConfig } from '@adonis-agora/config'

export default defineConfig({
  topology: 'tenant',
  tenant: {
    url: env.get('CONFIG_URL'),
    partition: 'streaming',
    token: env.get('CONFIG_TOKEN'),
    snapshotPath: 'storage/config/snapshot.json',
  },
})

Register the provider in adonisrc.ts (the install hook does it):

adonisrc.ts
providers: [
  // …
  () => import('@adonis-agora/config/config_provider'),
],

Before the app's config loads

config/*.ts files read env while they load, before any provider boots. To have the server's values by then, import them at the top of start/env.ts, before Env.create. This is the counterpart of Spring's spring.config.import:

start/env.ts
import { Env } from '@adonisjs/core/env'
import { importRemoteConfig } from '@adonis-agora/config/bootstrap'

await importRemoteConfig(new URL('../', import.meta.url), (env) => ({
  issuer: env.AUTHKIT_ISSUER,
  clientId: env.SERVICE_CLIENT_ID,
  clientSecret: env.SERVICE_CLIENT_SECRET,
  url: env.CONFIG_URL, // optional: without it, the issuer's discovery says where the server is
}))

export default await Env.create(new URL('../', import.meta.url), {
  // your schema, unchanged
})
  • Precedence, like Spring's default: the process environment, then the .env files, then the config server. A variable set locally is never replaced; the server only fills what is missing, and everything goes through your env schema.
  • Credentials come from the local environment you are handed (env), so they can sit in .env in development. With @adonis-agora/authkit-client installed, an issuer plus a service client is enough: the token (urn:agora:config) and the server URL are found for you, and the app name is the token's partition. Without it, pass token, url and partition.
  • No credentials: nothing is fetched. Tests and local development keep their .env.
  • Retry and fail-fast, like Spring Cloud Config (spring-retry, fail-fast). retry tries again with backoff, for example { attempts: 6, initialMs: 1000, maxMs: 5000 }. When every attempt failed and there is no snapshot, failFast: true fails the boot with E_REMOTE_CONFIG_UNAVAILABLE instead of booting without the server's values. The platform restarts the process, and a rolling deploy keeps the old instances serving.
  • The server down: the last snapshot is used (tmp/config/snapshot.json, snapshotPath to move it, false for none). Without one, the app boots on its local environment and a warning is printed. The boot never fails because of the config server.
  • Commands: node ace loads start/env.ts too, so commands get the same values.

The provider reuses the client opened here, so the document is not fetched twice.

bootstrapConfig(), which needs explicit url, partition and token, still works at the top of bin/server.ts. Use it when you do not want the .env precedence.

Read a value

import config from '@adonis-agora/config/services/main'

config.get('checkout.timeoutMs', 3000) // reloadable: applies without a restart
config.get('db.host')                 // boot-time: needs a restart
config.all()                          // every resolved value
config.status()                       // source, version, staleness

Write a value

Through the console at /_config, or from a terminal:

node ace config:entry:set checkout.timeoutMs 5000 --app=streaming --reloadable
node ace config:entry:list --app=streaming
node ace config:history --app=streaming
node ace config:rollback --to=3 --app=streaming --force

On this page

Partner

Your product as the official integration for Config, right here.

Become a partner →