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/. */
|
|
|
|
|
2015-08-17 19:48:34 +03:00
|
|
|
var config = require('../config').getProperties()
|
2015-03-23 22:24:08 +03:00
|
|
|
var jwtool = require('fxa-jwtool')
|
2013-07-26 00:18:19 +04:00
|
|
|
|
|
|
|
function main() {
|
2015-05-10 22:11:59 +03:00
|
|
|
var log = require('../lib/log')(config.log.level)
|
2016-06-27 13:02:10 +03:00
|
|
|
var metricsContext = require('../lib/metrics/context')(log, config)
|
|
|
|
log.setMetricsContext(metricsContext)
|
2013-11-26 05:27:08 +04:00
|
|
|
|
2016-05-19 07:57:58 +03:00
|
|
|
process.stdout.write(JSON.stringify({
|
|
|
|
event: 'config',
|
|
|
|
data: config
|
|
|
|
}) + '\n')
|
|
|
|
|
2013-12-17 08:36:34 +04:00
|
|
|
if (config.env !== 'prod') {
|
2015-06-19 02:12:42 +03:00
|
|
|
log.info(config, 'starting config')
|
2013-11-26 05:27:08 +04:00
|
|
|
}
|
2013-07-26 00:18:19 +04:00
|
|
|
|
2015-05-10 22:11:59 +03:00
|
|
|
var error = require('../lib/error')
|
|
|
|
var Token = require('../lib/tokens')(log, config.tokenLifetimes)
|
|
|
|
var Password = require('../lib/crypto/password')(log, config)
|
2013-10-27 06:49:33 +04:00
|
|
|
|
2015-05-10 22:11:59 +03:00
|
|
|
var signer = require('../lib/signer')(config.secretKeyFile, config.domain)
|
2015-09-22 03:14:23 +03:00
|
|
|
var serverPublicKeys = {
|
|
|
|
primary: jwtool.JWK.fromFile(
|
|
|
|
config.publicKeyFile,
|
|
|
|
{
|
|
|
|
algorithm: 'RS',
|
|
|
|
use: 'sig',
|
|
|
|
kty: 'RSA'
|
|
|
|
}
|
|
|
|
),
|
|
|
|
secondary: config.oldPublicKeyFile ?
|
|
|
|
jwtool.JWK.fromFile(
|
|
|
|
config.oldPublicKeyFile,
|
|
|
|
{
|
|
|
|
algorithm: 'RS',
|
|
|
|
use: 'sig',
|
|
|
|
kty: 'RSA'
|
|
|
|
}
|
|
|
|
)
|
|
|
|
: null
|
|
|
|
}
|
2013-10-30 00:46:09 +04:00
|
|
|
|
2015-05-10 22:11:59 +03:00
|
|
|
var Customs = require('../lib/customs')(log, error)
|
2014-04-19 06:05:47 +04:00
|
|
|
|
2015-05-10 22:11:59 +03:00
|
|
|
var Server = require('../lib/server')
|
2013-10-30 00:46:09 +04:00
|
|
|
var server = null
|
2014-02-27 03:57:31 +04:00
|
|
|
var mailer = null
|
2014-09-29 08:33:38 +04:00
|
|
|
var statsInterval = null
|
2014-06-09 04:19:34 +04:00
|
|
|
var database = null
|
|
|
|
var customs = null
|
2014-02-13 02:54:05 +04:00
|
|
|
|
2015-06-19 02:12:42 +03:00
|
|
|
function logStatInfo() {
|
|
|
|
log.stat(server.stat())
|
|
|
|
log.stat(Password.stat())
|
|
|
|
}
|
|
|
|
|
2015-05-10 22:11:59 +03:00
|
|
|
require('../lib/mailer')(config, log)
|
2014-03-10 04:33:52 +04:00
|
|
|
.done(
|
2014-02-27 03:57:31 +04:00
|
|
|
function(m) {
|
|
|
|
mailer = m
|
2013-10-30 00:46:09 +04:00
|
|
|
|
2015-05-10 22:11:59 +03:00
|
|
|
var DB = require('../lib/db')(
|
2014-02-13 02:54:05 +04:00
|
|
|
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-06-09 04:19:34 +04:00
|
|
|
database = db
|
|
|
|
customs = new Customs(config.customsUrl)
|
2015-05-10 22:11:59 +03:00
|
|
|
var routes = require('../lib/routes')(
|
2014-03-24 02:57:01 +04:00
|
|
|
log,
|
|
|
|
error,
|
2015-09-22 03:14:23 +03:00
|
|
|
serverPublicKeys,
|
2014-03-24 02:57:01 +04:00
|
|
|
signer,
|
|
|
|
db,
|
|
|
|
mailer,
|
2014-09-29 08:06:36 +04:00
|
|
|
Password,
|
2014-03-24 02:57:01 +04:00
|
|
|
config,
|
2016-06-27 13:02:10 +03:00
|
|
|
customs,
|
|
|
|
metricsContext
|
2014-03-24 02:57:01 +04:00
|
|
|
)
|
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(
|
2015-10-29 23:59:52 +03:00
|
|
|
function (err) {
|
|
|
|
if (err) {
|
|
|
|
log.error({ op: 'server.start.1', msg: 'failed startup with error',
|
|
|
|
err: { message: err.message } })
|
|
|
|
process.exit(1)
|
|
|
|
} else {
|
|
|
|
log.info({ op: 'server.start.1', msg: 'running on ' + server.info.uri })
|
|
|
|
}
|
2014-02-13 02:54:05 +04:00
|
|
|
}
|
|
|
|
)
|
2014-10-01 07:45:36 +04:00
|
|
|
statsInterval = setInterval(logStatInfo, 15000)
|
2014-02-13 02:54:05 +04:00
|
|
|
},
|
|
|
|
function (err) {
|
2014-09-04 05:32:12 +04:00
|
|
|
log.error({ op: 'DB.connect', err: { message: err.message } })
|
2014-02-13 02:54:05 +04:00
|
|
|
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() {
|
2014-06-09 04:19:34 +04:00
|
|
|
log.info({ op: 'shutdown' })
|
2014-09-29 08:33:38 +04:00
|
|
|
clearInterval(statsInterval)
|
2014-03-24 06:01:50 +04:00
|
|
|
server.stop(
|
|
|
|
function () {
|
2014-06-09 04:19:34 +04:00
|
|
|
customs.close()
|
2016-07-15 16:18:20 +03:00
|
|
|
try {
|
|
|
|
mailer.stop()
|
|
|
|
} catch (e) {
|
|
|
|
// XXX: simplesmtp module may quit early and set socket to `false`, stopping it may fail
|
|
|
|
log.warn({ op: 'shutdown', message: 'Mailer client already disconnected' })
|
|
|
|
}
|
2014-06-09 04:19:34 +04:00
|
|
|
database.close()
|
2015-07-30 01:05:29 +03:00
|
|
|
process.exit() //XXX: because of openid dep ಠ_ಠ
|
2014-03-24 06:01:50 +04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
2013-07-26 00:18:19 +04:00
|
|
|
}
|
|
|
|
|
2014-06-09 04:19:34 +04:00
|
|
|
main()
|