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
·256 lines (213 loc) · 7.82 KB
/
server.js
File metadata and controls
executable file
·256 lines (213 loc) · 7.82 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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#!/usr/bin/env node
const fs = require('fs');
const http = require('http');
const https = require('https');
const minimist = require('minimist');
const detectPort = require('detect-port');
const dotenv = require('dotenv');
const makeApp = require('./app');
const appLog = require('./lib/app-log');
const Config = require('./lib/config');
const { makeDb, getDb } = require('./lib/db');
const makeMigrator = require('./lib/make-migrator');
const loadSeedData = require('./lib/load-seed-data');
const ensureAdmin = require('./lib/ensure-admin');
const ensureConnectionAccess = require('./lib/ensure-connection-access');
const packageJson = require('./package.json');
const argv = minimist(process.argv.slice(2));
const helpText = `
SQLPad version ${packageJson.version}
# Print this help text
node server.js --help
# Apply database migrations then exit:
node server.js --migrate --config path/to/file.format
# Start SQLPad using config file:
node server.js --config path/to/file.format
`;
function cliHas(value) {
const lowered = argv._.map((v) => v.toLowerCase().trim());
return lowered.includes(value);
}
if (argv.version || cliHas('version')) {
// eslint-disable-next-line no-console
console.log('SQLPad version %s', packageJson.version);
process.exit(0);
}
if (argv.help || cliHas('help')) {
// eslint-disable-next-line no-console
console.log(helpText);
process.exit(0);
}
// If an .env file was passed for config, call dotenv to apply it to process.env
const configFilePath = argv.config || process.env.SQLPAD_CONFIG;
if (configFilePath && configFilePath.includes('.env')) {
const result = dotenv.config({ path: configFilePath });
if (result.error) {
appLog.error(result.error, 'Error loading .env file');
process.exit(1);
}
}
const config = new Config(argv, process.env);
const migrateOnly = config.get('migrate') || cliHas('migrate');
appLog.setLevel(config.get('appLogLevel'));
appLog.debug(config.get(), 'Final config values');
appLog.debug(config.getConnections(), 'Connections from config');
// Validate configuration and warn/error as appropriate
const configValidations = config.getValidations();
configValidations.warnings.map((warning) => appLog.warn(warning));
if (configValidations.errors.length > 0) {
configValidations.errors.forEach((error) => appLog.error(error));
process.exit(1);
}
makeDb(config);
const baseUrl = config.get('baseUrl');
const ip = config.get('ip');
const port = parseInt(config.get('port'), 10);
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');
const HALF_HOUR = 1000 * 60 * 30;
const CLEANING_MS = HALF_HOUR + parseInt(Math.random() * HALF_HOUR * 2);
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/sqlpad/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) {
appLog.info('Using port from Systemd');
return Promise.resolve({ fd: 3 });
} else {
appLog.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() {
const { models, sequelizeDb } = await getDb();
// Before application starts up apply any backend database migrations needed
// If --migrate / migrate was specified, the process exits afterwards
// Automatically running migrations may be disabled via config.
const migrator = makeMigrator(config, appLog, sequelizeDb.sequelize);
// Check to ensure SQLPad is either v0 (not yet initialized) or v5 or later
// As of v6, the embedded db migrations needed to move off of v3/v4 are no longer included.
const dbMajorVersion = await migrator.getDbMajorVersion();
const incompatibleDbVersion = dbMajorVersion >= 1 && dbMajorVersion <= 4;
if (incompatibleDbVersion) {
appLog.error(
'SQLPad database not compatible with this version of SQLPad. Migrate to version 5 prior to running version 6 or later.'
);
process.exit(1);
}
const isUpToDate = await migrator.schemaUpToDate();
const runMigrations = migrateOnly || config.get('dbAutomigrate');
if (!isUpToDate && !runMigrations) {
appLog.error(
'SQLPad schema not up to date. Turn on automigration or use --migrate'
);
process.exit(1);
}
if (runMigrations) {
appLog.info('Running migrations');
await migrator.migrate();
appLog.info('Migration finished');
}
if (migrateOnly) {
process.exit(0);
}
// Ensure admin is set if configured
await ensureAdmin(models, config);
// Load seed data after migrations
await loadSeedData(appLog, config, models);
// Create a connection accesses entry for everyone if set
await ensureConnectionAccess(sequelizeDb, config);
// Schedule cleanups
setInterval(async () => {
await models.statements.removeOldEntries();
}, CLEANING_MS);
// Create expressjs app
const app = await makeApp(config, models);
// determine if key pair exists for certs
if (keyPath && certPath) {
// https only
const _port = await detectPortOrSystemd(port);
if (!isFdObject(_port) && parseInt(port, 10) !== parseInt(_port, 10)) {
appLog.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)
}
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}`;
appLog.info('Welcome to SQLPad!. Visit %s to get started', url);
});
} else {
// http only
const _port = await detectPortOrSystemd(port);
if (!isFdObject(_port) && parseInt(port, 10) !== parseInt(_port, 10)) {
appLog.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}`;
appLog.info('Welcome to SQLPad! Visit %s to get started', url);
});
}
server.setTimeout(timeoutSeconds * 1000);
}
startServer().catch((error) => {
appLog.error(error, 'Error starting SQLPad');
process.exit(1);
});
function handleShutdownSignal(signal) {
if (!server) {
appLog.info('Received %s, but no server to shutdown', signal);
process.exit(0);
} else {
appLog.info('Received %s, shutting down server...', signal);
server.close(function () {
process.exit(0);
});
}
}
process.on('SIGTERM', handleShutdownSignal);
process.on('SIGINT', handleShutdownSignal);