-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
102 lines (92 loc) · 5.45 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// @ts-check
require('reflect-metadata');
// Patch electron version if missing, see https://github.com/eclipse-theia/theia/pull/7361#pullrequestreview-377065146
if (typeof process.versions.electron === 'undefined' && typeof process.env.THEIA_ELECTRON_VERSION === 'string') {
process.versions.electron = process.env.THEIA_ELECTRON_VERSION;
}
const path = require('path');
const express = require('express');
const cors = require('cors');
const { Container } = require('inversify');
const { BackendApplication, CliManager } = require('@theia/core/lib/node');
const { backendApplicationModule } = require('@theia/core/lib/node/backend-application-module');
const { messagingBackendModule } = require('@theia/core/lib/node/messaging/messaging-backend-module');
const { loggerBackendModule } = require('@theia/core/lib/node/logger-backend-module');
const assetsManager = require('./assets-manager');
const config = require('./configurations');
const gitDownloader = require('./git-downloader');
const governify = require('governify-commons');
const logger = governify.getLogger().tag('server');
const fileUpload = require('express-fileupload');
const fs = require('fs');
const spawnSync = require('child_process').spawnSync;
// Setting the default infrastructure location
if (!process.env.GOV_INFRASTRUCTURE) {
process.env.GOV_INFRASTRUCTURE = '/home/project/public/infrastructure.yaml';
}
const container = new Container();
container.load(backendApplicationModule);
container.load(messagingBackendModule);
container.load(loggerBackendModule);
function load (raw) {
return Promise.resolve(raw.default).then(module =>
container.load(module)
);
}
function start (port, host, argv) {
if (argv === undefined) {
argv = process.argv;
}
const cliManager = container.get(CliManager);
return cliManager.initializeCli(argv).then(async function () {
const application = container.get(BackendApplication);
application.use(cors());
if (process.env.ASSETS_REPOSITORY && !fs.existsSync('/home/project/.git')) { // Download assets from repository if specified in ENV VAR
logger.info('Assets repository URL specified. Downloading assets from: ', process.env.ASSETS_REPOSITORY);
if (process.env.ASSETS_REPOSITORY_BRANCH) {
logger.info('And checking out branch:', process.env.ASSETS_REPOSITORY_BRANCH);
}
await gitDownloader.gitDownload(process.env.ASSETS_REPOSITORY, '/home/project', process.env.ASSETS_REPOSITORY_BRANCH, process.env.ASSETS_REPOSITORY_SSHKEY);
logger.info('Git download process finished');
} else if (process.env.ASSETS_REPOSITORY && !fs.existsSync(process.env.GOV_INFRASTRUCTURE)) {
spawnSync('git', ['reset', '--hard', 'HEAD'], { cwd: '/home/project/' });
}
await governify.init().then(govMiddleware => {
application.use(express.json());
application.use(express.text());
application.use(fileUpload());
application.use(govMiddleware);
application.use(assetsManager.serveMiddleware);
}).catch(err => {
logger.error('Error in governify commons: ', err);
});
application.use(express.static(path.join(__dirname, '../../lib')));
application.use(express.static(path.join(__dirname, '../../lib/index.html')));
return application.start(config.port, host);
});
}
module.exports = (port, host, argv) => Promise.resolve()
// Add governify module load
.then(function () { return Promise.resolve(require('@theia/filesystem/lib/node/filesystem-backend-module')).then(load); })
.then(function () { return Promise.resolve(require('@theia/filesystem/lib/node/download/file-download-backend-module')).then(load); })
.then(function () { return Promise.resolve(require('@theia/workspace/lib/node/workspace-backend-module')).then(load); })
.then(function () { return Promise.resolve(require('@theia/process/lib/common/process-common-module')).then(load); })
.then(function () { return Promise.resolve(require('@theia/process/lib/node/process-backend-module')).then(load); })
.then(function () { return Promise.resolve(require('@theia/terminal/lib/node/terminal-backend-module')).then(load); })
.then(function () { return Promise.resolve(require('@theia/task/lib/node/task-backend-module')).then(load); })
.then(function () { return Promise.resolve(require('@theia/debug/lib/node/debug-backend-module')).then(load); })
.then(function () { return Promise.resolve(require('@theia/file-search/lib/node/file-search-backend-module')).then(load); })
.then(function () { return Promise.resolve(require('@theia/git/lib/node/git-backend-module')).then(load); })
.then(function () { return Promise.resolve(require('@theia/git/lib/node/env/git-env-module')).then(load); })
.then(function () { return Promise.resolve(require('@theia/mini-browser/lib/node/mini-browser-backend-module')).then(load); })
.then(function () { return Promise.resolve(require('@theia/search-in-workspace/lib/node/search-in-workspace-backend-module')).then(load); })
.then(function () { return Promise.resolve(require('@theia/plugin-ext/lib/plugin-ext-backend-module')).then(load); })
.then(function () { return Promise.resolve(require('@theia/plugin-ext-vscode/lib/node/plugin-vscode-backend-module')).then(load); })
.then(function () { return Promise.resolve(require('@theia/vsx-registry/lib/node/vsx-registry-backend-module')).then(load); })
.then(() => start(config.port, host, argv)).catch(reason => {
console.error('Failed to start the backend application.');
if (reason) {
console.error(reason);
}
throw reason;
});