Skip to main content

Addons

An addon extends Castmill without changing its core. Most of the dashboard is built from addons: Medias, Playlists, Layouts, Widgets and Devices are addons that ship with Castmill. You can use the same mechanism for your own features, such as billing, a custom report or an integration with an internal system.

An addon has a server part written in Elixir and, optionally, a dashboard part written in SolidJS and TypeScript.

An addon canThrough
Add a page to the dashboard sidebarcomponent_info/0 and a SolidJS component
Add authenticated API routesapi_routes/0, mounted under /api/addons/<id>/
Add public API routespublic_api_routes/0
Receive webhookswebhook_handlers/0, mounted under /webhooks/addons/<id>/
React to system eventsregister_hooks/0
Contribute results to global searchsearch/3
Serve its own static filesstatic_path/0, served under /assets/addons/<id>/
Check its configuration at start-uprequired_config/0

The server part​

An addon is a module that uses Castmill.Addons.Addon. That makes it a supervised GenServer: Castmill starts it with the server and restarts it if it crashes. Every callback is optional.

lib/castmill/addons/reports/reports.addon.ex
defmodule Castmill.Addons.Reports do
use Castmill.Addons.Addon

@impl Castmill.Addons.AddonBehaviour
def component_info do
%Castmill.Addons.ComponentInfo{
id: "reports",
name: "Reports",
name_key: "sidebar.reports",
description: "Proof-of-play reports",
version: "1.0.0",
path: "/reports.js",
icon: "/reports_icon.js",
mount_path: "/reports/*",
mount_point: "sidepanel.reports",
keyboard_shortcut: %{key: "R", description_key: "shortcuts.gotoReports"},
translations_path: "/reports/i18n"
}
end

@impl Castmill.Addons.AddonBehaviour
def api_routes do
[
{:get, "/summary", Castmill.Addons.Reports.Controller, :summary}
]
end

@impl Castmill.Addons.AddonBehaviour
def register_hooks do
Castmill.Hooks.register_hook(:user_signup, &__MODULE__.on_user_signup/1)
end

def on_user_signup(%{user_id: _user_id, email: _email}), do: :ok
end

Component info​

FieldDescription
idUnique identifier. Used in API, webhook and asset paths.
name, name_keySidebar label. name_key is a translation key; name is the fallback.
description, versionInformational.
pathBundle of the page component, relative to the addon asset path.
iconBundle of the sidebar icon component.
mount_pathDashboard route of the page, below /org/:orgId. Append /* if the addon has its own sub-routes, for example /reports/*.
mount_pointPosition in the sidebar as a dotted path, such as sidepanel.reports or sidepanel.content.reports. The parent must exist.
keyboard_shortcutOptional key that opens the page as a G then key sequence, for example G then R. Pick a letter that no other page uses (C, D, I, L, M, O, P and T are taken).
translations_pathOptional folder with <locale>.json files. The dashboard loads the user's language and falls back to en.json.

Hooks​

Castmill.Hooks.register_hook/2 subscribes a function to a system event. The :user_signup hook receives %{user_id: ..., email: ...} after a new account is created; the built-in onboarding addon uses it to send a welcome email.

API routes and webhooks​

Routes from api_routes/0 are mounted under /api/addons/<id>/ and require a signed-in dashboard user. Routes from public_api_routes/0 do not require authentication, so check the input carefully. Each webhook handler is a map with a :path, a :handler function and an optional :verify function, which should check the provider's signature before the handler runs.

Configuration​

Addon options live in the application environment under the addon module. List the required keys in required_config/0, and the addon refuses to start with a clear message if one is missing:

def required_config do
[{:api_key, "API key of the reporting service"}]
end

# Read a value
get_config(:api_key)

The dashboard part​

The page component is a SolidJS component that receives AddonComponentProps:

lib/castmill/addons/reports/components/index.tsx
import { Component } from 'solid-js';
import { AddonComponentProps } from '../../common/interfaces/addon-store';

const ReportsPage: Component<AddonComponentProps> = (props) => {
const t = (key: string) => props.store.i18n?.t(key) ?? key;

return (
<div>
<h1>{t('reports.title')}</h1>
<p>Organization: {props.selectedOrgId}</p>
</div>
);
};

export default ReportsPage;

props.store gives the addon what it needs from the dashboard:

PropertyContents
organizations.selectedIdThe current organization
envServer address and other environment values
socketThe dashboard's Phoenix socket, for live updates
permissionsThe user's role and permission matrix, to hide actions the user cannot perform
i18nt, tp, formatDate, formatNumber, formatCurrency and the current locale
themelight or dark
keyboardShortcutsRegister page shortcuts such as C for create
routernavigate and location

props.params holds the URL search parameters and a setter, and props.routeParams the route parameters.

Build the user interface with components from @castmill/ui-common, so the page looks like the rest of the dashboard, and take every colour from the --cm-* theme tokens so it works in light and dark mode. Use the translation function for every visible string.

Building​

Built-in addons are bundled by packages/castmill/assets/build.js. It builds components/index.tsx of every folder in lib/castmill/addons into priv/static/assets/addons/<folder>.js, and components/icon.tsx into <folder>_icon.js. Each bundle includes its own copy of @castmill/ui-common and injects its CSS when it loads.

Installing an addon​

Built-in addons are listed in config/config.exs:

config :castmill, :addons, [
Castmill.Addons.Onboarding,
Castmill.Addons.Content,
Castmill.Addons.Playlists,
# ...
]

Addons that live in their own Mix project are added in config/runtime.exs, with options that are passed to init/1:

config :castmill, :external_addons, [
{MyCompany.ReportsAddon, [api_key: System.get_env("REPORTS_API_KEY")]}
]

An external addon serves its compiled JavaScript from its own priv folder through static_path/0, for example {:priv, :my_company_reports, "static"}.

Testing​

Run the dashboard tests of addons from packages/castmill/assets, after building @castmill/ui-common:

yarn workspace @castmill/ui-common build
cd packages/castmill/assets
npx vitest run ../lib/castmill/addons/reports/

Test the server part with ExUnit like any other module in packages/castmill.