chairo
chairo ("happy" in ancient Greek: χαίρω) is a Seneca micro-services plugin for hapi. The plugin integrates the Seneca functionality into hapi and provide tools to map its actions to server methods and views for easy access.
Lead Maintainer - Wyatt Preul
Usage
Plugin Registration
chairo is registered with a hapi server using the server.register()
method. Once
registered it decorates the server
object with a reference to the seneca
object initialized
using the provided plugin options. Default plugin options:
seneca
:false
log
:silent
actcache
:active
:false
You can use an existing instance to seneca
and let chairo
do the decorations
for your server. When setting the seneca
option on the configuration, please
note that the other settings won't be used.
const Chairo = ;const Hapi = ; const server = ; async { // Register plugin try await server; // Add a Seneca action let id = 0; serverseneca; // Invoke a Seneca action const result = await serverseneca; // result: { id: 1 } // Invoke a Seneca action without expecting an answer serverseneca; catch err {}};
In addition, the hapi request object is decorated with a reference to the seneca
object for
easy access:
server;
server.action(name, pattern, [options])
Maps a Seneca action pattern to a hapi server method where:
name
- the server method name (same as the name used inserver.method()
).pattern
- the Seneca action pattern (e.g.'generate:id'
or{ generate: 'id' }
) to map.options
- optional settings options where:cache
- method caching options (same as the name used inserver.method()
).generateKey
- method generating custom cache key (same as the name used inserver.method()
).
'use strict'; const Chairo = ;const Hapi = ; const server = ;async { try await server; // Set up a Seneca action let id = 0; serverseneca; serverseneca; // Map action to a hapi server method serveraction'generate' 'generate:id' cache: expiresIn: 1000 generateTimeout: 3000 ; // Map action to a hapi server method with custom generateKey method serveraction'average' 'calc:average' cache: expiresIn: 1000 generateTimeout: 3000 { return 'average-' + messagesamplesdatasetvalues0 + ':' + messagesamplesdatasetvalues1 ; } ; // Start hapi server (starts cache) await serverstart; // Invoke server method const result1 = await servermethods; // Invoke the same server method const result2 = await servermethods; // result1 === result2 (cached) const avg1 = await servermethods; const avg2 = await servermethods; // avg1 == avg2 (cached) catch err {}};
h.act(pattern)
Sends back a handler response using the result of a Seneca action where:
pattern
- the Seneca action called to generate the response.
const Chairo = ;const Hapi = ; const server = ; async { try await server; // Set up a Seneca action let id = 0; serverseneca; // Add route server; catch err {}};
In addition, the act
handler shortcut is also provided:
server;
reply.compose(template, context, [options])
Renders a template view using the provided template and context where:
template
- the view engine template (same as the name used inreply.view()
).context
- the context object used to render the template.Chairo
provides a special key$resolve
where you can map context variables to Seneca actions matching they key's value pattern. Each of the services mapped to keys is resolved and the resultant key value maps are copied to the context root before redering the template.options
- optional settings passed toreply.view()
.
It requires the vision
plugin to be registered with Hapi.
const Chairo = ;const Handlebars = ;const Vision = ;const Hapi = ; const server = ; async { try await server; // set up a few Seneca actions serverseneca; serverseneca; // Set up a hapi view engine server; // Add route server; catch err {}};
Using the template ./templates/example.html
:
{{today.date}} {{user.name}} {{general.message}}
In addition, the compose
handler shortcut is also provided:
server;