mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-20 08:33:11 +00:00
feat: add functionality to register plugins via http
Also: - Add functionality for serving the responses from plugins - Add tests for related methods and routes Closes #2, #37
This commit is contained in:
committed by
Rainer Killinger
parent
59ea7a5ba6
commit
3d51ccfac2
24
src/app.ts
24
src/app.ts
@@ -25,15 +25,17 @@ import * as cors from 'cors';
|
||||
import * as express from 'express';
|
||||
import * as morgan from 'morgan';
|
||||
import {join} from 'path';
|
||||
import {configFile, isTestEnvironment, mailer, validator} from './common';
|
||||
import {configFile, isTestEnvironment, mailer, plugins, validator} from './common';
|
||||
import {MailQueue} from './notification/mail-queue';
|
||||
import {bulkAddRouter} from './routes/bulk-add-route';
|
||||
import {bulkDoneRouter} from './routes/bulk-done-route';
|
||||
import {bulkRouter} from './routes/bulk-route';
|
||||
import {indexRouter} from './routes/index-route';
|
||||
import {multiSearchRouter} from './routes/multi-search-route';
|
||||
import {pluginRegisterRouter} from './routes/plugin-register-route';
|
||||
import {searchRouter} from './routes/search-route';
|
||||
import {thingUpdateRouter} from './routes/thing-update-route';
|
||||
import {virtualPluginRoute} from './routes/virtual-plugin-route';
|
||||
import {BulkStorage} from './storage/bulk-storage';
|
||||
import {DatabaseConstructor} from './storage/database';
|
||||
import {Elasticsearch} from './storage/elasticsearch/elasticsearch';
|
||||
@@ -193,16 +195,32 @@ export async function configureApp() {
|
||||
bulkRouter,
|
||||
indexRouter,
|
||||
multiSearchRouter,
|
||||
pluginRegisterRouter,
|
||||
searchRouter,
|
||||
thingUpdateRouter,
|
||||
);
|
||||
|
||||
// for plugins, as Express doesn't really want you to unregister routes (and doesn't offer any method to do so at all)
|
||||
app.all('*', async (req, res, next) => {
|
||||
// if the route exists then call virtual route on the plugin that registered that route
|
||||
if (plugins.has(req.originalUrl)) {
|
||||
try {
|
||||
res.json(await virtualPluginRoute(req, plugins.get(req.originalUrl)!));
|
||||
} catch (e) {
|
||||
// in case of error send an error response
|
||||
res.status(e.statusCode);
|
||||
res.json(e);
|
||||
}
|
||||
} else {
|
||||
// pass to the next matching route (which is 404)
|
||||
next();
|
||||
}
|
||||
});
|
||||
|
||||
// add a route for a missing resource (404)
|
||||
app.use((_req, res) => {
|
||||
const errorResponse = new SCNotFoundErrorResponse(isTestEnvironment);
|
||||
res.status(errorResponse.statusCode);
|
||||
res.json(errorResponse);
|
||||
});
|
||||
|
||||
// TODO: implement a route to register plugins
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user