Skip to main content
Version: 2.16.0

Plugin

To extend the functionality of a service in Platformatic, you can register a plugin. These plugins are standard Fastify plugins.

Plugin Configuration

Specify the location of your plugin files in the configuration file, as shown in the example below. This path is relative to the config file path.

{
...
"plugins": {
"paths": ["./plugin/index.js"]
}
}

Creating a Plugin

Your plugin should export an asynchronous function that receives the parameters:

  • app (FastifyInstance): this is the main fastify instance.
  • opts: contains all the options specified in the config file after path.

Example Plugin

Here's a simple example of a Fastify plugin:

module.exports = async function (app, opts) {
app.get('/hello', async (request, reply) => {
return 'Hello from Platformatic!';
});
}

Hot Reload

The plugin file is monitored by the fs.watch function. There's no need to manually reload the Platformatic Composer server while developing your plugin. Changes are detected automatically, triggering a server restart to load your updated code.

tip

Currently, on Linux, file watching in subdirectories is not supported due to a Node.js limitation, as documented here.

Directory Structure

Plugins can also be directories, which are loaded using @fastify/autoload. This approach automatically configures routes matching the folder structure.

Example Directory Structure

Consider the following directory structure for organizing multiple plugins:

├── routes
│ ├── foo
│ │ ├── something.js
│ │ └── bar
│ │ └── baz.js
│ ├── single-plugin
│ │ └── utils.js
│ └── another-plugin.js
└── platformatic.json

By default, each folder will be added as a prefix to the routes defined within them. Refer to the @fastify/autoload documentation for customization options.

Loading Multiple Plugins

To load multiple plugins in parallel, specify an array of paths in the configuration:

{
...
"plugins": {
"paths": [{
"path": "./plugin/index.js"
}, {
"path": "./routes/"
}]
}
}

Issues

If you run into a bug or have a suggestion for improvement, please raise an issue on GitHub or join our Discord feedback channel.