UNPKG

ozserver

Version:

API for OZ

157 lines (136 loc) 5.08 kB
var Server, Storage, Streak, async, cluster, colors, exports, express, http, mkpath, mongoose, path, readline, _; express = require('express'); mongoose = require('mongoose'); http = require('http'); path = require('path'); mkpath = require('mkpath'); colors = require('colors'); readline = require('readline'); async = require('async'); cluster = require('cluster'); _ = require('lodash'); Storage = require(global.home + '/script/controllers/storage').Storage; Streak = require(global.home + '/script/controllers/streak').Streak; Server = (function() { function Server() {} Server.prototype.configure = function(special, handler) { var _this = this; if (special == null) { special = {}; } this.options = { mail: {} }; if (global.store == null) { throw 'global.store is not exists'; } if (global.mail == null) { throw 'global.mail is not exists'; } if (special.profile != null) { mkpath.sync("/usr/lib/ozserver/" + special.profile); global.store = "/usr/lib/ozserver/" + special.profile + "/store.json"; global.mail = "/usr/lib/ozserver/" + special.profile + "/mail.json"; } this.options = _.extend({}, this.options, special); this.store = new Storage(global.store); this.mail = new Storage(global.mail); if (cluster.isMaster) { this.rl = readline.createInterface({ input: process.stdin, output: process.stdout }); } return async.series([ function(callback) { return _this.answer(_this.store, 'port', callback); }, function(callback) { return _this.answer(_this.store, 'mongodb_connection', callback); }, function(callback) { return _this.answer(_this.store, 'uploads_directory', callback); }, function(callback) { return _this.answer(_this.store, 'uploads_url', callback); }, function(callback) { return _this.answer(_this.store, 'max_user_file_size_mb', callback); }, function(callback) { return _this.answer(_this.mail, 'email', callback); }, function(callback) { return _this.answer(_this.mail, 'host', callback); }, function(callback) { return _this.answer(_this.mail, 'user', callback); }, function(callback) { return _this.answer(_this.mail, 'password', callback); } ], function(err, results) { if (cluster.isMaster) { _this.rl.close(); } if (typeof handler === 'function') { return handler(results); } }); }; Server.prototype.answer = function(conf, key, callback) { if ((this.options[key] == null) && !conf.get(key)) { if (this.rl == null) { throw 'rl not defined'; } return conf.question(this.rl, key, function(value) { return callback(null, value); }); } else { if (!this.options[key]) { return callback(null, conf.get(key)); } else { return callback(null, this.options[key]); } } }; Server.prototype.run = function(special, handler) { var _this = this; if (special == null) { special = {}; } return this.configure(special, function(results) { var app, server, streak; _this.options.port = results[0]; _this.options.mongodb_connection = results[1]; _this.options.uploads_directory = results[2]; _this.options.uploads_url = results[3]; _this.options.max_user_file_size_mb = results[4]; _this.options.mail.host = results[5]; _this.options.mail.user = results[6]; _this.options.mail.password = results[7]; app = express(); if ((_this.options.mongodb_connection == null) || _this.options.mongodb_connection === '') { throw 'undefined mongodb_connection'; } mongoose.connect(_this.options.mongodb_connection); mongoose.connection.on('error', function(err) { if (err) { throw err; } }); streak = new Streak(); require(global.home + '/script/config/express/server')(app, _this.options, streak); require(global.home + '/script/routes/registration')(app, _this.options, streak); require(global.home + '/script/routes/remember')(app, _this.options, streak); require(global.home + '/script/routes/action')(app, _this.options, streak); require(global.home + '/script/routes/signin')(app, _this.options, streak); require(global.home + '/script/routes/edit')(app, _this.options, streak); require(global.home + '/script/routes/feed')(app, _this.options, streak); if ((app.get('port') == null) || app.get('port') === '') { throw 'undefined port'; } server = http.createServer(app); require(global.home + '/script/controllers/sockets')(server, streak); require(global.home + '/script/controllers/movement')(streak); if (typeof handler === 'function') { return handler(server, _this.options); } }); }; return Server; })(); exports = module.exports = new Server(); exports.Server = Server;