fxa-auth-server/bin/key_server.js

112 строки
2.6 KiB
JavaScript
Исходник Обычный вид История

#!/usr/bin/env node
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/. */
var fs = require('fs')
var config = require('../config').root()
function main() {
var log = require('../log')(config)
// memory monitor
var MemoryMonitor = require('../memory_monitor')
var memoryMonitor = new MemoryMonitor()
memoryMonitor.on(
'mem',
function (usage) {
log.trace(
{
op: 'stat',
stat: 'mem',
rss: usage.rss,
heapTotal: usage.heapTotal,
heapUsed: usage.heapUsed
}
)
}
)
memoryMonitor.start()
2013-10-27 08:11:02 +04:00
var Token = require('../tokens')(log)
2013-10-27 06:49:33 +04:00
2013-10-30 00:46:09 +04:00
//signer compute-cluster
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
var Server = require('../server')
var server = null
// TODO: send to the SMTP server directly. In the future this may change
// to another process that we send an http request to.
var mailer = require('../mailer')(config.smtp, log)
// server public key
var serverPublicKey = JSON.parse(fs.readFileSync(config.publicKeyFile))
2013-07-27 04:00:09 +04:00
// databases
2013-10-30 00:46:09 +04:00
var DB = require('../db')(
config,
2013-10-27 06:49:33 +04:00
log,
2013-10-27 08:11:02 +04:00
Token.error,
Token.AuthToken,
Token.SessionToken,
Token.KeyFetchToken,
Token.AccountResetToken,
Token.SrpToken,
Token.ForgotPasswordToken
2013-10-27 06:49:33 +04:00
)
var noncedb = require('../noncedb')(
config,
log
)
2013-10-30 00:46:09 +04:00
DB.connect()
.then(
function (db) {
return noncedb.connect()
.then(function(ndb) { return [db, ndb] })
},
function (err) {
log.error({ op: 'noncedb.connect', err: err })
process.exit(1)
}
)
.then(
function (backends) {
var db = backends[0]
var noncedb = backends[1]
2013-10-30 00:46:09 +04:00
var routes = require('../routes')(log, serverPublicKey, signer, db, mailer, Token, config)
server = Server.create(log, config, routes, db, noncedb)
2013-10-30 00:46:09 +04:00
server.start(
function () {
log.info('running on ' + server.info.uri)
}
)
},
function (err) {
log.error({ op: 'DB.connect', err: err.message })
process.exit(1)
}
)
process.on(
'SIGINT',
function () {
2013-07-26 23:34:15 +04:00
memoryMonitor.stop()
server.stop(
function () {
process.exit()
}
)
}
)
}
if (!fs.existsSync(config.publicKeyFile)) {
require('../scripts/gen_keys')(main)
}
else {
main()
}