This repository has been archived by the owner on Apr 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
136 lines (126 loc) · 3.87 KB
/
server.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
/**
* caligarum
* an ORM-free REST API with OAuth2 support
*
* created by François Masclef
*/
global.config = require('./config/' + (process.env.NODE_ENV || 'production') + '.json')
global.cluster = require('cluster')
global.db = require('./db')
global.Helpers = require('./services/helpers')
global.pjson = require('./package.json')
global.redis = require('redis').createClient(config.redis || {}),
global.retcode = require('./enums/httpStatusCode')
global.winston = require('winston')
global.basePath = __dirname
var
mysql = require('mysql'),
patcher = require('mysql-patcher'),
path = require('path')
/**
* Patch String class
*/
String.prototype.ucfirst = function()
{
return this.charAt(0).toUpperCase() + this.substr(1).toLowerCase()
}
/**
* Configure Winston
*/
winston.configure({
level: config.log.level || 'info',
format: winston.format.combine(
winston.format.splat(),
winston.format.colorize(),
winston.format.timestamp(),
winston.format.align(),
winston.format.printf((info) => `${info.timestamp} ${info.level} ${info.message}`)
),
transports: []
});
if (config.log.console.enabled) winston.add(new winston.transports.Console())
if (config.log.file.enabled) winston.add(winston.transports.File,{
filename : config.log.file.filename || '/var/log/caligarum/caligarum.log',
maxsize : config.log.file.size || 10485760,
maxFiles : config.log.file.files || 10,
zippedArchive : 'zip' in config.log.file ? config.log.file.zip : true
})
/**
* Start server in a clustered world
*/
if (cluster.isMaster) {
// deal with `npm stop`
process.title = process.argv[2]
// Hello caligarum
let figlet = require('figlet')
console.log(
figlet.textSync(pjson.name, {
font: 'Small',
horizontalLayout: 'default',
verticalLayout: 'default'
})
)
winston.info("%s %s running on port %d from %s", pjson.name, pjson.version, config.server.port, basePath)
// Check caligarum database, patch it if required
winston.info("checking caligarum schema, applying patch if required")
patcher.patch({
host : config.database.host,
port : config.database.port,
user : config.database.user,
database : config.database.db,
password : config.database.password,
dir : path.join(__dirname, 'sql'),
patchKey : 'schema-patch-level',
patchLevel : 1,
filePrefix : 'caligarum',
metaTable : 'metadata',
mysql : mysql
}, function(err, res) {
if (err) {
winston.error(err)
process.exit(1)
}
// Check custom database, patch it if required
winston.info("checking custom schema, applying patch if required")
patcher.patch({
host : config.database.host,
port : config.database.port,
user : config.database.user,
database : config.database.db,
password : config.database.password,
dir : path.join(__dirname, 'sql'),
patchKey : (config.database.custom_sql_prefix || 'custom') + '-patch-level',
patchLevel : 1,
filePrefix : config.database.custom_sql_prefix || 'custom',
metaTable : 'metadata',
mysql : mysql
}, function(err, res) {
if (err) {
winston.error(err)
process.exit(1)
}
// Let's fork
var cpuCount = require('os').cpus().length;
winston.info('spawning %d workers', cpuCount)
for (let i=0; i<cpuCount; i+=1) {
cluster.fork()
}
// handle dying workers
cluster.on('exit', function(worker) {
winston.warn('worker %d died, respawning', worker.id)
cluster.fork()
})
})
})
} else {
db.connect(function(err) {
if (err) {
winston.error(err)
process.exit(2)
} else {
let app = require('./app')
app.set('port', config.server.port)
let server = app.listen(app.get('port'), function() { })
}
})
}