-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
213 lines (181 loc) · 6.17 KB
/
index.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
'use strict';
const _ = require('underscore');
const co = require('co');
const fs = require('fs');
const bodyParser = require('body-parser');
const http = require('http');
const express = require('express');
const path = require('path');
const cors = require('cors');
const fileUpload = require('express-fileupload');
const webminController = require('./server/controller/webmin.js');
// Inject 'webstart' command if no argument was given
if (process.argv.length === 2) {
process.argv.push('direct_webstart');
}
module.exports = {
duniter: {
cliOptions: [
// Webmin options
{ value: '--webmhost <host>', desc: 'Local network interface to connect to (IP)' },
{ value: '--webmport <port>', desc: 'Local network port to connect', parser: parseInt },
],
cli: [{
name : 'webstart',
desc : 'Starts Duniter as a daemon (background task).',
logs : false,
onConfiguredExecute: (server, conf, program, params) => co(function* () {
yield server.checkConfig();
const daemon = server.getDaemon('direct_webstart', 'webstart');
yield startDaemon(program, daemon);
}),
}, {
name : 'webrestart',
desc : 'Stops Duniter daemon and restart it with its web interface.',
logs : false,
onConfiguredExecute: (server, conf, program, params) => co(function* () {
yield server.checkConfig();
const daemon = server.getDaemon('direct_webstart', 'webrestart');
yield stopDaemon(daemon);
yield startDaemon(program, daemon);
}),
}, {
name : 'direct_webstart',
desc : 'Do a webstart',
onDatabaseExecute: (server, conf, program, params, startServices, stopServices, stack) => co(function* () {
try {
/****************************************
* SPECIALISATION
***************************************/
const app = express();
const HOTE = program.webmhost || 'localhost';
const PORT = program.webmport || 9220;
/**
* Sur appel de l'URL /abc
*/
app.use(express.static(path.join(__dirname, '..', 'duniter-ui', 'public')));
app.use(cors());
// File upload for backup API
app.use(fileUpload());
app.use(bodyParser.urlencoded({
extended: true,
}));
app.use(bodyParser.json());
const wbmin = webminController(server, startServices, stopServices, listDuniterPlugins, stack);
const httpServer = http.createServer(app);
httpServer.listen(PORT, HOTE);
server.logger.info('Web administration accessible at following address: http://%s:%s', HOTE, PORT);
require('./server/lib/routes').webmin(wbmin, app);
require('./server/lib/routes').webminWS(wbmin)(httpServer);
const uiDeps = listDuniterUIPlugins();
for (const dep of uiDeps) {
// Eventual HTTP routing
if (dep.required.duniterUI.route) {
const subApp = express();
dep.required.duniterUI.route(subApp, server, conf, program, params);
app.use('/modules/', subApp);
}
}
const currentBlock = yield server.dal.getCurrentBlockOrNull();
if (currentBlock) {
yield wbmin.startAllServices();
}
// Never ending promise
return new Promise((resolve) => {
});
/****************************************/
} catch (e) {
console.error(e);
process.exit(1);
}
}),
}],
},
};
function startDaemon(program, daemon) {
return co(function* () {
const PORT = program.webmport || 9221;
const isPortAlreadyTaken = yield new Promise((resolve) => {
isPortTaken(PORT, (err, taken) => err ? reject(err) : resolve(taken));
});
if (isPortAlreadyTaken) {
console.error('Port ' + PORT + ' already used.');
process.exit(3);
}
return new Promise((resolve, reject) => daemon.start((err) => {
if (err) return reject(err);
resolve();
}));
});
}
/**
* Checks if a port is already taken by another app.
*
* Source: https://gist.github.com/timoxley/1689041
* @param port
* @param fn
*/
function isPortTaken(port, fn) {
const net = require('net');
const tester = net.createServer()
.once('error', function (err) {
if (err.code != 'EADDRINUSE') return fn(err);
fn(null, true);
})
.once('listening', function () {
tester.once('close', function () {
fn(null, false);
})
.close();
})
.listen(port);
}
function stopDaemon(daemon) {
return new Promise((resolve, reject) => daemon.stop((err) => {
err && console.error(err);
if (err) return reject(err);
resolve();
}));
}
function listDuniterPlugins() {
return listPlugins(r => !!r.duniter || !!r.duniterUI);
}
function listDuniterUIPlugins() {
return listPlugins(r => !!r.duniterUI);
}
function listPlugins(conditionTest) {
const uiDependencies = [];
const pathToPackageJSON = path.resolve('./package.json');
const pkgJSON = JSON.parse(fs.readFileSync(pathToPackageJSON, 'utf8'));
const peerDeps = pkgJSON.peerDependencies || {};
const allDeps = _.extend(pkgJSON.dependencies || {}, pkgJSON.devDependencies || {});
const deps = Object.keys(allDeps);
for (const dep of deps) {
try {
const required = require(dep);
if (required && conditionTest(required)) {
uiDependencies.push({
name : dep,
version: allDeps[dep],
locked : !!peerDeps[dep],
required,
});
}
} catch (e) {
}
}
// Special: self dependency (if local package is also a module)
if (pkgJSON.main && pkgJSON.main.match(/\.js/)) { // With NW.js, the main is an index.html file, which causes a bug
const dep = pkgJSON.name;
const required = require(path.resolve('./' + pkgJSON.main));
if (required && conditionTest(required)) {
uiDependencies.push({
name : dep,
version: 'local',
locked : true,
required,
});
}
}
return uiDependencies;
}