Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Copyright (c) TySoft Limited 2014 - ALL RIGHTS RESERVED.
- */
- 'use strict';
- var Url = require('url'),
- Async = require('async'),
- Express = require('express'),
- Passport = require('passport'),
- ExpressApp = Express(),
- Connect = require('connect'),
- MongoStore = require('connect-mongo')(Express),
- HttpServer = require('http').createServer(ExpressApp),
- Config = require('./config/config'),
- DatabaseConfig = require('./config/config-database');
- var session_store = null;
- Async.waterfall([
- // Environment configuration
- function (done) {
- done(Config.checkConfig());
- },
- // Connect to the database
- function (done) {
- DatabaseConfig.configDatabase(done);
- },
- // Configure express
- function (done) {
- // Store sessions in MongoDB
- session_store = new MongoStore({
- host: DatabaseConfig.database.host,
- port: DatabaseConfig.database.port,
- db: DatabaseConfig.database.name,
- username: DatabaseConfig.database.username,
- password: DatabaseConfig.database.password
- });
- // Configure a public static content server
- ExpressApp.use(Express.static(__dirname + '/client' + (Config.inProduction() ? '-build' : '')));
- // Use compress to automatically gzip requests and responses
- ExpressApp.use(Express.compress());
- // Enable cookie parsing, needed for our sessions
- ExpressApp.use(Express.cookieParser());
- // Enable body parsing so we can access the request body as an object
- ExpressApp.use(Connect.urlencoded());
- ExpressApp.use(Connect.json());
- // Make sure we set the session cookie on subdomains in prod so it can be accessed on the socket domain
- var cookie_domain = null;
- if (Config.inProduction()) {
- var server_url = Url.parse(Config.getConfig(Config.keys.server_url)),
- parts = server_url.hostname.split('.');
- cookie_domain = '.' + parts[parts.length - 2] + '.' + parts[parts.length - 1];
- }
- // Enable sessions
- ExpressApp.use(Express.session({
- cookie: { domain: cookie_domain},
- key: Config.getConfig(Config.keys.session_key),
- secret: Config.getConfig(Config.keys.session_secret),
- store: session_store
- }));
- // Use passport for authentication
- ExpressApp.use(Passport.initialize());
- ExpressApp.use(Passport.session());
- // Development specific configuration
- ExpressApp.configure('development', function () {
- ExpressApp.use(Express.errorHandler({
- dumpExceptions: true,
- showStack: true
- }));
- });
- // Production specific configuration
- ExpressApp.configure('production', function () {
- ExpressApp.use(Express.errorHandler());
- });
- done();
- },
- // Configure passport
- function (done) {
- require('./config/config-authentication').config(ExpressApp, done);
- },
- // Configure socket.io
- function (done) {
- var SocketConfig = require('./config/config-sockets');
- SocketConfig.configureSockets(HttpServer, session_store);
- done();
- },
- // Start express server
- function (done) {
- var server_listening = false;
- HttpServer.once('error', function (err) {
- if (!server_listening) {
- done(err);
- }
- });
- HttpServer.once('listening', function () {
- server_listening = true;
- done();
- });
- HttpServer.listen(Config.getConfig(Config.keys.port));
- },
- // Load http controllers
- function (done) {
- require('./authentication/authentication-controller')(ExpressApp);
- require('./users/user-controller')(ExpressApp);
- require('./games/game-controller')(ExpressApp);
- require('./stories/story-controller')(ExpressApp);
- done();
- }],
- // Handle any errors encountered during startup
- function (err) {
- if (!err) {
- console.log('Poker server startup complete.');
- } else {
- console.log('Something went wrong during server startup. Exiting.');
- throw err;
- }
- }
- );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement