Skip to content
This repository has been archived by the owner on Sep 14, 2022. It is now read-only.

Commit

Permalink
Rewrote plugins loading (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
babolivier committed May 7, 2017
1 parent 1a56b93 commit bf039f2
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 222 deletions.
87 changes: 0 additions & 87 deletions server/config.ts

This file was deleted.

38 changes: 0 additions & 38 deletions server/controllers/routes.js

This file was deleted.

29 changes: 14 additions & 15 deletions server/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as Hapi from 'hapi';

import {loadRoutes} from './utils/routesLoader';
import {loadPlugins} from './utils/pluginsLoader';

const pkg = require('../../package.json');
const log = require('printit')({
Expand Down Expand Up @@ -33,17 +34,15 @@ server.connection({ port: port, host: host });

server.register([
require('inert'),
require('vision'),
{
register: require('hapi-swagger'),
options: {
info: {
'title': pkg.name,
'description': pkg.description,
'version': pkg.version,
},
}
},
require('vision')
options: { info: {
'title': pkg.name,
'description': pkg.description,
'version': pkg.version,
}}
}
], (err) => {
if(err) {
log.error(err);
Expand All @@ -54,16 +53,16 @@ server.register([
server.route({
method: 'GET',
path: '/{file*}',
handler: {
directory: {
path: __dirname + '/../client/',
listing: true
}
}
handler: { directory: {
path: __dirname + '/../client/',
listing: true
}}
});

// Load the routes from the main router
loadRoutes(server);
// Load the install plugins
loadPlugins(server);
});

// Start the server
Expand Down
5 changes: 5 additions & 0 deletions server/router.ts
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
import * as admin from './controllers/admin';
import * as data from './controllers/pluginData';
import * as plugin from './controllers/plugin';
import * as user from './controllers/user';

module.exports = {}
53 changes: 0 additions & 53 deletions server/scripts/sethome.ts

This file was deleted.

29 changes: 0 additions & 29 deletions server/scripts/updateschema.js

This file was deleted.

3 changes: 3 additions & 0 deletions server/utils/handleErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ export function handleErr(err: Error, res: e.Response): void {
case 'NUMBER_MISSING':
code = 400;
break;
case 'UNAUTHORISED':
code = 401;
break;
case 'NOT_ADMIN':
case 'PLUGIN_DISABLED':
code = 403;
Expand Down
57 changes: 57 additions & 0 deletions server/utils/pluginsLoader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import * as path from 'path';
import * as fs from 'fs';

import * as Hapi from 'hapi';

import * as Plugins from '../lib/plugins';

const log = require('printit')({
date: true,
prefix: 'Plugins'
});

const root = path.resolve('./plugins');

export function loadPlugins(server: Hapi.Server) {
// Check and update plugins currently stored in the database
Plugins.PluginConnector.update().catch(processError);

// Retrieve a list of the plugins currently present in the /plugins directory
let plugins = fs.readdirSync(root).filter(file => fs.statSync(path.join(root, file)).isDirectory());

// Add plugin's public directories as static routes
for(let plugin of plugins) {
// Get instance of the connector so the plugin gets registered if
// it wasn't before
Plugins.PluginConnector.getInstance(plugin)
.then((connector) => {
if(!connector) throw new Error('Couldn\'t get database connector');

return connector.getState()
}).then((state) => {
// Only create the root if the plugin is enabled
if(state === Plugins.State.enabled) {
// Get the plugin's public path
let pluginPublicPath = path.join(root, plugin, 'public');

// Check if the plugin has a public directory
if(fs.existsSync(pluginPublicPath)) {
// Create a static route for the plugin
server.route({
method: 'GET',
path: '/' + plugin + '/{file*}',
handler: { directory: {
path: __dirname + pluginPublicPath,
listing: true
}}
});
}
}
}).catch(processError);
}
}

function processError(e) {
log.error(e);
process.exit(1);
}

0 comments on commit bf039f2

Please sign in to comment.