Skip to content

Example: Plugin Loader

Nick Marus edited this page Aug 21, 2016 · 3 revisions

This is an a modified example from the README that incorporates the flint.use() command to load a plugin. For an example of what a plugin file is structured like, check out Hotel California. This type of application structure allows you to separate you various flint commands in separate files so that you can enable/disable functionality without modifying the main program file.

var Flint = require('node-flint');
var webhook = require('node-flint/webhook');
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.json());

var path = require('path');
var fs = require('fs');
var pluginFolder = '/path/to/plugins';

// flint options
var config = {
  webhookUrl: 'http://myserver.com/flint',
  token: 'Tm90aGluZyB0byBzZWUgaGVyZS4uLiBNb3ZlIGFsb25nLi4u',
  port: 80
};

// init flint
var flint = new Flint(config);
flint.start();

// register plugins
flint.on('start', function() {
  fs.readdirSync(pluginFolder).forEach(function(plugin) {
    var newPlugin = path.join(pluginFolder, plugin);
    if(path.parse(newPlugin).ext === '.js') {
      flint.use(newPlugin);
    }
  });
});

// define express path for incoming webhooks
app.post('/flint', webhook(flint));

// start express server
var server = app.listen(config.port, function () {
  flint.debug('Flint listening on port %s', config.port);
});

// gracefully shutdown (ctrl-c)
process.on('SIGINT', function() {
  flint.debug('stoppping...');
  server.close();
  flint.stop().then(function() {
    process.exit();
  });
});
Clone this wiki locally