Skip to content

Commit

Permalink
trifid: register plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
ludovicm67 committed Oct 18, 2023
1 parent c5b2199 commit f6290e9
Show file tree
Hide file tree
Showing 11 changed files with 150 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .changeset/early-ads-rescue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"trifid": patch
---

Install Helmet
5 changes: 5 additions & 0 deletions .changeset/sixty-garlics-return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"trifid": minor
---

Allow to register plugins
36 changes: 35 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions packages/trifid/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
// @ts-check
import Fastify from 'fastify'
import cors from '@fastify/cors'
import helmet from '@fastify/helmet'

import healthMiddleware from './middlewares/health.js'
import { registerPlugin } from './lib/registerPlugin.js'
import healthPlugin from './plugins/health.js'
import templateEngine from './plugins/templateEngine.js'

const server = Fastify({
logger: {
Expand All @@ -14,9 +17,12 @@ await server.register(cors, {
origin: '*',
credentials: true,
})
await server.register(helmet, {
global: false,
})

// Health check
server.get('/healthz', healthMiddleware)
await registerPlugin(server, templateEngine)
await registerPlugin(server, healthPlugin)

const start = async () => {
try {
Expand Down
20 changes: 20 additions & 0 deletions packages/trifid/lib/registerPlugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// @ts-check

/**
* Register a Trifid plugin.
*
* @param {import('fastify').FastifyInstance} fastifyInstance Fastify instance
* @param {import('../types/plugin.d.ts').TrifidPlugin} trifidPlugin Trifid plugin
* @returns {Promise<void>}
*/
export const registerPlugin = async (fastifyInstance, trifidPlugin) => {
const { plugin, pluginOptions, routeOptions } = await trifidPlugin()

if (plugin) {
fastifyInstance.register(plugin, pluginOptions)
}

if (routeOptions) {
fastifyInstance.route(routeOptions)
}
}
16 changes: 0 additions & 16 deletions packages/trifid/middlewares/health.js

This file was deleted.

6 changes: 5 additions & 1 deletion packages/trifid/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"url": "https://github.com/zazuko/trifid/issues"
},
"scripts": {
"dev": "node --watch index.js",
"test": "true",
"help": "node server.js --help",
"start:tbbt": "node server.js -c instances/tbbt/config.yaml",
Expand All @@ -25,6 +26,9 @@
},
"dependencies": {
"@fastify/cors": "^8.4.0",
"fastify": "^4.23.2"
"@fastify/helmet": "^11.1.1",
"@fastify/view": "^8.2.0",
"fastify": "^4.23.2",
"handlebars": "^4.7.8"
}
}
28 changes: 28 additions & 0 deletions packages/trifid/plugins/health.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// @ts-check

/**
* Health plugin.
*
* @param {import('fastify').FastifyRequest} request Fastify request
* @param {import('fastify').FastifyReply} reply Fastify reply
* @returns {Promise<void>}
*/
const healthPlugin = async (request, reply) => {
request.log.debug('Some info about the current request')
reply
.code(200)
.header('Content-Type', 'text/plain; charset=utf-8')
.send('OK')
}

/** @type {import('../types/plugin.d.ts').TrifidPlugin}'} */
const trifidPlugin = async () => {
return {
routeOptions: {
method: 'GET',
url: '/healthz',
handler: healthPlugin,
},
}
}
export default trifidPlugin
36 changes: 36 additions & 0 deletions packages/trifid/plugins/templateEngine.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// @ts-check
import { dirname, join as pathJoin } from 'node:path'
import { fileURLToPath } from 'node:url'

import fastifyView from '@fastify/view'
import Handlebars from 'handlebars'

const trifidRootPath = dirname(dirname(fileURLToPath(import.meta.url)))

/** @type {import('@fastify/view').default} */
const plugin = fastifyView

/** @type {import('@fastify/view').FastifyViewOptions} */
const pluginOptions = {
engine: {
handlebars: Handlebars,
},
root: pathJoin(trifidRootPath, 'views'),
layout: './layouts/main',
includeViewExtension: true,
viewExt: 'hbs',
propertyName: 'view',
defaultContext: {
dev: process.env.NODE_ENV === 'development',
},
options: {},
}

/** @type {import('../types/plugin.d.ts').TrifidPlugin} */
const trifidPlugin = async () => {
return {
plugin,
pluginOptions,
}
}
export default trifidPlugin
6 changes: 6 additions & 0 deletions packages/trifid/types/plugin.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export type TrifidPluginResult = {
plugin?: any;
pluginOptions?: any;
routeOptions?: import("fastify").RouteOptions;
};
export type TrifidPlugin = () => Promise<TrifidPluginResult>;
1 change: 1 addition & 0 deletions packages/trifid/views/layouts/main.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello world!

0 comments on commit f6290e9

Please sign in to comment.