forked from lisong/code-push-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwww.ts
More file actions
executable file
·94 lines (83 loc) · 2.85 KB
/
Copy pathwww.ts
File metadata and controls
executable file
·94 lines (83 loc) · 2.85 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
#!/usr/bin/env node
import 'dotenv/config';
import http from 'http';
import { logger } from 'kv-logger';
import _ from 'lodash';
import validator from 'validator';
import { app } from './app';
import { CURRENT_DB_VERSION } from './core/const';
import { Versions } from './models/versions';
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort(val): number | string | false {
const port = parseInt(val, 10);
if (Number.isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
// check if the db is initialized
Versions.findOne({ where: { type: 1 } })
.then((v) => {
if (!v || v.version !== CURRENT_DB_VERSION) {
throw new Error(
'Please upgrade your database. use `npm run upgrade` or `code-push-server-db upgrade`',
);
}
// create server and listen
const server = http.createServer(app);
const port = normalizePort(process.env.PORT || '3000');
let host = null;
if (process.env.HOST) {
logger.debug(`process.env.HOST ${process.env.HOST}`);
if (validator.isIP(process.env.HOST)) {
logger.debug(`${process.env.HOST} valid`);
host = process.env.HOST;
} else {
logger.warn(`process.env.HOST ${process.env.HOST} is invalid, use 0.0.0.0 instead`);
}
}
server.listen(port, host);
server.on('error', (error: Error & { syscall: string; code: string }) => {
if (error.syscall === 'listen') {
const bind = typeof port === 'string' ? `Pipe ${port}` : `Port ${port}`;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
logger.error(`${bind} requires elevated privileges`);
process.exit(1);
break;
case 'EADDRINUSE':
logger.error(`${bind} is already in use`);
process.exit(1);
break;
default:
break;
}
}
logger.error(error);
throw error;
});
server.on('listening', () => {
const addr = server.address();
logger.info(`server is listening on ${JSON.stringify(addr)}`);
});
})
.catch((e) => {
if (_.startsWith(e.message, 'ER_NO_SUCH_TABLE')) {
logger.error(
new Error(
'Please upgrade your database. use `npm run upgrade` or `code-push-server-db upgrade`',
),
);
} else {
logger.error(e);
}
process.exit(1);
});