Skip to content

Commit 15c5a6a

Browse files
authored
Merge pull request #875 from 06kellyjac/make_more_extensible
refactor: allow for extending express
2 parents 5b3aba9 + 12054fb commit 15c5a6a

File tree

2 files changed

+36
-18
lines changed

2 files changed

+36
-18
lines changed

src/proxy/index.js

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
const proxyApp = require('express')();
1+
const express = require('express');
22
const bodyParser = require('body-parser');
3-
const http = require("http");
4-
const https = require("https");
3+
const http = require('http');
4+
const https = require('https');
55
const fs = require('fs');
6-
const path = require("path");
6+
const path = require('path');
77
const router = require('./routes').router;
88
const config = require('../config');
99
const db = require('../db');
@@ -17,18 +17,14 @@ const options = {
1717
limit: '100000kb',
1818
type: '*/*',
1919
key: fs.readFileSync(path.join(__dirname, config.getSSLKeyPath())),
20-
cert: fs.readFileSync(path.join(__dirname, config.getSSLCertPath()))
20+
cert: fs.readFileSync(path.join(__dirname, config.getSSLCertPath())),
2121
};
2222

23-
// Setup the proxy middleware
24-
proxyApp.use(bodyParser.raw(options));
25-
proxyApp.use('/', router);
26-
27-
const start = async () => {
28-
const plugins = config.getPlugins();
29-
const pluginLoader = new PluginLoader(plugins);
30-
await pluginLoader.load();
31-
chain.chainPluginLoader = pluginLoader;
23+
const proxyPreparations = async () => {
24+
const plugins = config.getPlugins();
25+
const pluginLoader = new PluginLoader(plugins);
26+
await pluginLoader.load();
27+
chain.chainPluginLoader = pluginLoader;
3228
// Check to see if the default repos are in the repo list
3329
const defaultAuthorisedRepoList = config.getAuthorisedList();
3430
const allowedList = await db.getRepos();
@@ -41,15 +37,30 @@ const start = async () => {
4137
await db.addUserCanAuthorise(x.name, 'admin');
4238
}
4339
});
40+
};
4441

45-
http.createServer(options, proxyApp).listen(proxyHttpPort, () => {
42+
// just keep this async incase it needs async stuff in the future
43+
const createApp = async () => {
44+
const app = express();
45+
// Setup the proxy middleware
46+
app.use(bodyParser.raw(options));
47+
app.use('/', router);
48+
return app;
49+
};
50+
51+
const start = async () => {
52+
const app = await createApp();
53+
await proxyPreparations();
54+
http.createServer(options, app).listen(proxyHttpPort, () => {
4655
console.log(`HTTP Proxy Listening on ${proxyHttpPort}`);
4756
});
48-
https.createServer(options, proxyApp).listen(proxyHttpsPort, () => {
57+
https.createServer(options, app).listen(proxyHttpsPort, () => {
4958
console.log(`HTTPS Proxy Listening on ${proxyHttpsPort}`);
5059
});
5160

52-
return proxyApp;
61+
return app;
5362
};
5463

64+
module.exports.proxyPreparations = proxyPreparations;
65+
module.exports.createApp = createApp;
5566
module.exports.start = start;

src/service/index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const corsOptions = {
2323
origin: true,
2424
};
2525

26-
const start = async () => {
26+
const createApp = async () => {
2727
// configuration of passport is async
2828
// Before we can bind the routes - we need the passport strategy
2929
const passport = await require('./passport').configure();
@@ -69,6 +69,12 @@ const start = async () => {
6969
res.sendFile(path.join(`${absBuildPath}/index.html`));
7070
});
7171

72+
return app;
73+
};
74+
75+
const start = async () => {
76+
const app = await createApp();
77+
7278
_httpServer.listen(uiPort);
7379

7480
console.log(`Service Listening on ${uiPort}`);
@@ -77,5 +83,6 @@ const start = async () => {
7783
return app;
7884
};
7985

86+
module.exports.createApp = createApp;
8087
module.exports.start = start;
8188
module.exports.httpServer = _httpServer;

0 commit comments

Comments
 (0)