Skip to content
This repository has been archived by the owner on Oct 9, 2023. It is now read-only.

Add middlewares to the config. (This can be useful if you want to pro… #187

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const defaultConfig = {
files: [],
logger: {},
plugins: [],
middlewares: [],
port: 8080,
host: '0.0.0.0',
processJobsConcurrent: true,
Expand Down
20 changes: 17 additions & 3 deletions src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,26 @@ import bodyParser from 'body-parser';
import './environment';
import logger from './utils/logger';
import renderBatch from './utils/renderBatch';
import { runAppLifecycle, errorSync, raceTo } from './utils/lifecycle';
import { errorSync, raceTo, runAppLifecycle } from './utils/lifecycle';
import BatchManager from './utils/BatchManager';

const attachMiddleware = (app, config) => {
app.use(bodyParser.json(config.bodyParser));
};

const attachMiddlewares = (app, config) => {
attachMiddleware(app, config);

const { middlewares } = config;
if (Array.isArray(middlewares)) {
middlewares.forEach((middleware) => {
if (typeof middleware === 'function') {
app.use(middleware);
}
});
}
};

const attachEndpoint = (app, config, callback) => {
app.post(config.endpoint, renderBatch(config, callback));
};
Expand Down Expand Up @@ -117,8 +130,8 @@ const initServer = (app, config, callback) => {
};

const worker = (app, config, onServer, workerId) => {
// ===== Middleware =========================================================
attachMiddleware(app, config);
// ===== Middlewares =========================================================
attachMiddlewares(app, config);

if (onServer) {
onServer(app, process);
Expand Down Expand Up @@ -154,6 +167,7 @@ const worker = (app, config, onServer, workerId) => {
};

worker.attachMiddleware = attachMiddleware;
worker.attachMiddlewares = attachMiddlewares;
worker.attachEndpoint = attachEndpoint;
worker.initServer = initServer;
worker.Server = Server;
Expand Down