2013-07-26 05:46:16 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2013-07-26 00:18:19 +04:00
|
|
|
var fs = require('fs')
|
|
|
|
var config = require('../config').root()
|
|
|
|
|
|
|
|
function main() {
|
2013-12-12 22:02:33 +04:00
|
|
|
var log = require('../log')(config.log.level)
|
2013-11-26 05:27:08 +04:00
|
|
|
|
2014-03-15 08:50:49 +04:00
|
|
|
log.event('config', config)
|
2013-12-17 08:36:34 +04:00
|
|
|
if (config.env !== 'prod') {
|
2013-11-26 05:27:08 +04:00
|
|
|
log.info(config, "starting config")
|
|
|
|
}
|
2013-07-26 00:18:19 +04:00
|
|
|
|
|
|
|
// memory monitor
|
|
|
|
var MemoryMonitor = require('../memory_monitor')
|
|
|
|
var memoryMonitor = new MemoryMonitor()
|
2013-09-17 22:07:39 +04:00
|
|
|
memoryMonitor.on(
|
|
|
|
'mem',
|
|
|
|
function (usage) {
|
2014-02-24 06:37:03 +04:00
|
|
|
log.stat(
|
2013-09-17 22:07:39 +04:00
|
|
|
{
|
|
|
|
stat: 'mem',
|
|
|
|
rss: usage.rss,
|
|
|
|
heapTotal: usage.heapTotal,
|
|
|
|
heapUsed: usage.heapUsed
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
2013-07-26 00:18:19 +04:00
|
|
|
memoryMonitor.start()
|
|
|
|
|
2014-02-01 03:42:49 +04:00
|
|
|
var error = require('../error')
|
2014-01-16 01:23:29 +04:00
|
|
|
var Token = require('../tokens')(log, config.tokenLifetimes)
|
2013-10-27 06:49:33 +04:00
|
|
|
|
2013-12-05 09:21:42 +04:00
|
|
|
// signer compute-cluster
|
2013-10-30 00:46:09 +04:00
|
|
|
var CC = require('compute-cluster')
|
|
|
|
var signer = new CC({ module: __dirname + '/signer.js' })
|
2013-11-08 05:31:44 +04:00
|
|
|
signer.on('error', function () {}) // don't die
|
2013-10-30 00:46:09 +04:00
|
|
|
|
2014-04-19 06:05:47 +04:00
|
|
|
var Customs = require('../customs')(log, error)
|
|
|
|
|
2013-10-30 00:46:09 +04:00
|
|
|
var Server = require('../server')
|
|
|
|
var server = null
|
2014-02-27 03:57:31 +04:00
|
|
|
var mailer = null
|
2014-02-13 02:54:05 +04:00
|
|
|
|
2013-10-30 00:46:09 +04:00
|
|
|
// TODO: send to the SMTP server directly. In the future this may change
|
|
|
|
// to another process that we send an http request to.
|
2014-06-02 02:04:39 +04:00
|
|
|
require('../mailer')(config, log)
|
2014-03-10 04:33:52 +04:00
|
|
|
.done(
|
2014-02-27 03:57:31 +04:00
|
|
|
function(m) {
|
|
|
|
mailer = m
|
2014-02-13 02:54:05 +04:00
|
|
|
// server public key
|
|
|
|
var serverPublicKey = JSON.parse(fs.readFileSync(config.publicKeyFile))
|
2013-10-30 00:46:09 +04:00
|
|
|
|
2014-02-13 02:54:05 +04:00
|
|
|
// databases
|
|
|
|
var DB = require('../db')(
|
|
|
|
config.db.backend,
|
|
|
|
log,
|
|
|
|
error,
|
|
|
|
Token.SessionToken,
|
|
|
|
Token.KeyFetchToken,
|
|
|
|
Token.AccountResetToken,
|
|
|
|
Token.PasswordForgotToken,
|
|
|
|
Token.PasswordChangeToken
|
|
|
|
)
|
2013-10-30 00:46:09 +04:00
|
|
|
|
2014-02-13 02:54:05 +04:00
|
|
|
DB.connect(config[config.db.backend])
|
|
|
|
.done(
|
|
|
|
function (db) {
|
2014-04-19 06:05:47 +04:00
|
|
|
var customs = new Customs(config.customsUrl)
|
2014-03-24 02:57:01 +04:00
|
|
|
var routes = require('../routes')(
|
|
|
|
log,
|
|
|
|
error,
|
|
|
|
serverPublicKey,
|
|
|
|
signer,
|
|
|
|
db,
|
|
|
|
mailer,
|
|
|
|
config,
|
|
|
|
customs
|
|
|
|
)
|
2014-03-10 04:33:52 +04:00
|
|
|
server = Server.create(log, error, config, routes, db)
|
2013-12-06 02:52:55 +04:00
|
|
|
|
2014-02-13 02:54:05 +04:00
|
|
|
server.start(
|
|
|
|
function () {
|
|
|
|
log.info({ op: 'server.start.1', msg: 'running on ' + server.info.uri })
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
function (err) {
|
|
|
|
log.error({ op: 'DB.connect', err: err.message })
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
)
|
2013-07-26 00:18:19 +04:00
|
|
|
|
2013-10-30 00:46:09 +04:00
|
|
|
}
|
|
|
|
)
|
2013-07-26 00:18:19 +04:00
|
|
|
|
2014-01-15 10:29:34 +04:00
|
|
|
process.on(
|
|
|
|
'uncaughtException',
|
|
|
|
function (err) {
|
|
|
|
log.fatal(err)
|
|
|
|
process.exit(8)
|
|
|
|
}
|
|
|
|
)
|
2014-03-24 06:01:50 +04:00
|
|
|
process.on('SIGINT', shutdown)
|
|
|
|
log.on('error', shutdown)
|
|
|
|
|
|
|
|
function shutdown() {
|
|
|
|
memoryMonitor.stop()
|
|
|
|
mailer.stop()
|
|
|
|
server.stop(
|
|
|
|
function () {
|
|
|
|
process.exit()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
2013-07-26 00:18:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!fs.existsSync(config.publicKeyFile)) {
|
|
|
|
require('../scripts/gen_keys')(main)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
main()
|
|
|
|
}
|