DialectoInContext.Config (dialecto_in_context v0.1.0)

Copy Markdown View Source

The add-on's settings, with the Astro add-on's names (spec-0017 §1). Environment variables win over config :dialecto_in_context, …:

  • url / DIALECTO_URL — the Dialecto that serves the overlay and the sidebar (default https://dialecto.eu);
  • project / DIALECTO_PROJECT — a project slug or id, when the git remote isn't enough to find it;
  • catalogs / DIALECTO_CATALOGS — the catalogs folder whose uncommitted files the sidebar warns about (default priv/gettext);
  • enabled / DIALECTO_IN_CONTEXT=off — switches the add-on off;
  • paths / DIALECTO_PATHS (comma-separated) — path prefixes whose pages get the editor (default: every page), to keep it off admin screens, say;
  • exclude_paths / DIALECTO_EXCLUDE_PATHS (comma-separated) — path prefixes whose pages never get it, even under paths. For an app that wants every page but a few, such as the one that serves the sidebar.

Live-site editing (spec-0025 §4) adds four keys, read from the app config only and safe to commit:

  • mode — :dev, :live or :off. Unset, it is :dev in a development or test build and :off in any other; the build environment is read when this package compiles;
  • repo — "owner/name", the Dialecto repo editors work on;
  • origin — the site's own origin, "https://www.example.com". Required in :live (the plug falls back to the endpoint's configured URL). It is never taken from request headers, which a proxy can rewrite;
  • public_key — the repo's "ed25519:…" key from its Dialecto settings. Without it, :live stays off.

Summary

Functions

The live-site settings, or nil unless :live has everything it needs: a repo and a valid public_key, an origin that parses if one is set, and the add-on not switched off. origin is nil when the plug should ask the endpoint. Resolved once by resolve!/1.

The mode in force: the :mode setting, else the build's default. :dev in a build whose default is :off (a production build) is refused and means :off. Read from the value resolve!/1 stored at boot, so a lookup costs one term read.

The http(s) origin of url — lowercase, default port dropped, IPv6 bracketed — or nil for anything that isn't a web URL (the same form Dialecto stores a repo's sites in).

True when the editor belongs on path: under settings.paths (any page when unset) and not under settings.exclude_paths.

Reads the app config once and keeps the mode and live settings for the requests and lookups that follow. The application calls it at boot, with log? true so that a misconfigured :live says why it stayed off.

Settings from env (the OS environment) and app_env (the app config).

Types

live()

@type live() :: %{
  origin: String.t() | nil,
  paths: [String.t()] | nil,
  exclude_paths: [String.t()] | nil,
  public_key: binary(),
  repo: String.t(),
  url: String.t()
}

mode()

@type mode() :: :dev | :live | :off

t()

@type t() :: %{
  enabled: boolean(),
  url: String.t(),
  url_valid: boolean(),
  project: String.t() | nil,
  catalogs: String.t(),
  paths: [String.t()] | nil,
  exclude_paths: [String.t()] | nil
}

Functions

live()

@spec live() :: live() | nil

The live-site settings, or nil unless :live has everything it needs: a repo and a valid public_key, an origin that parses if one is set, and the add-on not switched off. origin is nil when the plug should ask the endpoint. Resolved once by resolve!/1.

mode()

@spec mode() :: mode()

The mode in force: the :mode setting, else the build's default. :dev in a build whose default is :off (a production build) is refused and means :off. Read from the value resolve!/1 stored at boot, so a lookup costs one term read.

origin(url)

@spec origin(term()) :: String.t() | nil

The http(s) origin of url — lowercase, default port dropped, IPv6 bracketed — or nil for anything that isn't a web URL (the same form Dialecto stores a repo's sites in).

path_allowed?(settings, path)

@spec path_allowed?(map(), String.t()) :: boolean()

True when the editor belongs on path: under settings.paths (any page when unset) and not under settings.exclude_paths.

resolve!(log? \\ false)

@spec resolve!(boolean()) :: :ok

Reads the app config once and keeps the mode and live settings for the requests and lookups that follow. The application calls it at boot, with log? true so that a misconfigured :live says why it stayed off.

settings(env \\ System.get_env(), app_env \\ Application.get_all_env(:dialecto_in_context))

@spec settings(map(), keyword()) :: t()

Settings from env (the OS environment) and app_env (the app config).