-
Notifications
You must be signed in to change notification settings - Fork 0
/
www.js
130 lines (116 loc) · 3.41 KB
/
www.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
'use strict';
// Module dependencies
const debug = require('debug');
const http = require('http');
const app = require('../app');
// Debugging
const debugErr = debug('app:err');
const debugLog = debug('app:log');
// Optimizations
http.globalAgent.maxSockets = Infinity;
// Security
app.proxy = true;
// pg-promise
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/throw#Throw_an_object
// https://nodejs.org/api/process.html#process_process_exit_code
// https://nodejs.org/api/errors.html#errors_error_propagation_and_interception
const db = require('../db/pgp').db;
const pgp = require('../db/pgp').pgp;
function UserException(message) {
this.message = message;
this.name = 'UserException';
}
const query = async (tablename) => {
try {
// Check if table exists
// http://dba.stackexchange.com/a/86098/106579
// http://stackoverflow.com/a/24089729/1442219
//const exist = await db.one(`SELECT to_regclass('${tablename}') AS exist;`, [], a => a.exist);
/*const exist = await db.one(`
SELECT EXISTS (
SELECT 1
FROM information_schema.tables
WHERE table_name = '${tablename}'
) AS exist;
`, [], a => a.exist);*/
const exist = await db.one(`
SELECT EXISTS (
SELECT 1
FROM information_schema.columns
WHERE table_name = '${tablename}'
AND column_name = 'name'
)
AS bool;
`, [], a => a.bool);
//if (exist === tablename) {
if (exist === true) {
debugLog(`Database exists: ${exist}`);
} else {
debugErr(`Database missing: ${tablename} !== ${exist}`);
throw new UserException(`${tablename} table NOT exist`);
}
} catch (err) {
debugErr(`PGP ERROR: ${err.message || err}`); // print error;
process.on('exit', (code) => {
debugErr(`About to exit with code: ${code}`);
});
process.exitCode = 9;
process.exit();
}
};
/*try {
query('foo2');
} catch (e) {
debugErr(e.message, e.name); // pass exception object to err handler
}*/
query('foo');
//query('foo2');
// Create HTTP server
const server = http.createServer(app.callback());
// Normalize a port into a number, string, or false
function normalizePort(val) {
const port = parseInt(val, 10);
if (isNaN(port)) {
return val; // named pipe
}
if (port >= 0) {
return port; // port number
}
return false;
}
// Get port from environment and store in Koa
const port = normalizePort(process.env.PORT || '3000');
// Event listener for HTTP server "error" event
function onError(err) {
if (err.syscall !== 'listen') {
throw err;
}
const bind = typeof port === 'string'
? `Pipe ${port}`
: `Port ${port}`;
// Handle specific listen errors with friendly messages
switch (err.code) {
case 'EACCES':
debugErr('%s requires elevated privileges', bind);
process.exit(1);
break;
case 'EADDRINUSE':
debugErr('%s is already in use', bind);
process.exit(1);
break;
default:
throw err;
}
}
// Event listener for HTTP server "listening" event
function onListening() {
const addr = server.address();
const bind = typeof addr === 'string'
? `pipe ${addr}`
: `port ${addr.port}`;
debugLog('Listening on %s', bind);
}
// Listen on provided port, on all network interfaces
server.listen(port, process.env.PRIVATE_IP_DOCKER || '0.0.0.0');
server.on('error', onError);
server.on('listening', onListening);