forked from sqlpad/sqlpad
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
executable file
·147 lines (128 loc) · 4.37 KB
/
Copy pathserver.js
File metadata and controls
executable file
·147 lines (128 loc) · 4.37 KB
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
#!/usr/bin/env node
const fs = require('fs');
const http = require('http');
const https = require('https');
const detectPort = require('detect-port');
const makeApp = require('./app');
// Parse command line flags to see if anything special needs to happen
require('./lib/cli-flow.js');
const logger = require('./lib/logger');
const config = require('./lib/config');
const { makeDb, getDb } = require('./lib/db');
makeDb(config);
const configValidations = config.getValidations();
configValidations.warnings.map(warning => logger.warn(warning));
if (configValidations.errors.length > 0) {
configValidations.errors.forEach(error => logger.error(error));
process.exit(1);
}
const baseUrl = config.get('baseUrl');
const ip = config.get('ip');
const port = config.get('port');
const httpsPort = config.get('port');
const certPassphrase = config.get('certPassphrase');
const keyPath = config.get('keyPath');
const certPath = config.get('certPath');
const systemdSocket = config.get('systemdSocket');
const timeoutSeconds = config.get('timeoutSeconds');
function isFdObject(ob) {
return ob && typeof ob.fd === 'number';
}
// When --systemd-socket is passed we will try to acquire the bound socket
// directly from Systemd.
//
// More info
//
// https://github.com/rickbergfalk/sqlpad/pull/185
// https://www.freedesktop.org/software/systemd/man/systemd.socket.html
// https://www.freedesktop.org/software/systemd/man/sd_listen_fds.html
function detectPortOrSystemd(port) {
if (systemdSocket) {
const passedSocketCount = parseInt(process.env.LISTEN_FDS, 10) || 0;
// LISTEN_FDS contains number of sockets passed by Systemd. At least one
// must be passed. The sockets are set to file descriptors starting from 3.
// We just crab the first socket from fd 3 since sqlpad binds only one
// port.
if (passedSocketCount > 0) {
logger.info('Using port from Systemd');
return Promise.resolve({ fd: 3 });
} else {
logger.warn(
'Warning: Systemd socket asked but not found. Trying to bind port %d manually',
port
);
}
}
return detectPort(port);
}
/* Start the Server
============================================================================= */
let server;
async function startServer(models) {
const app = makeApp(config, models);
// determine if key pair exists for certs
if (keyPath && certPath) {
// https only
const _port = await detectPortOrSystemd(httpsPort);
if (!isFdObject(_port) && httpsPort !== _port) {
logger.info(
'Port %d already occupied. Using port %d instead.',
httpsPort,
_port
);
// TODO FIXME XXX Persist the new port to the in-memory store.
// config.set('httpsPort', _port)
}
const privateKey = fs.readFileSync(keyPath, 'utf8');
const certificate = fs.readFileSync(certPath, 'utf8');
const httpsOptions = {
key: privateKey,
cert: certificate,
passphrase: certPassphrase
};
server = https
.createServer(httpsOptions, app)
.listen(_port, ip, function() {
const hostIp = ip === '0.0.0.0' ? 'localhost' : ip;
const url = `https://${hostIp}:${_port}${baseUrl}`;
logger.info('Welcome to SQLPad!. Visit %s to get started', url);
});
} else {
// http only
const _port = await detectPortOrSystemd(port);
if (!isFdObject(_port) && port !== _port) {
logger.info(
'Port %d already occupied. Using port %d instead.',
port,
_port
);
// TODO FIXME XXX Persist the new port to the in-memory store.
// config.set('port', _port)
}
server = http.createServer(app).listen(_port, ip, function() {
const hostIp = ip === '0.0.0.0' ? 'localhost' : ip;
const url = `http://${hostIp}:${_port}${baseUrl}`;
logger.info('Welcome to SQLPad! Visit %s to get started', url);
});
}
server.setTimeout(timeoutSeconds * 1000);
}
getDb()
.then(db => startServer(db.models))
.catch(error => {
logger.error(error, 'Error starting SQLPad');
process.exit(1);
});
function handleShutdownSignal(signal) {
if (!server) {
logger.info('Received %s, but no server to shutdown', signal);
process.exit(0);
} else {
logger.info('Received %s, shutting down server...', signal);
server.close(function() {
process.exit(0);
});
}
}
process.on('SIGTERM', handleShutdownSignal);
process.on('SIGINT', handleShutdownSignal);