import * as express from 'express' ; import * as http from 'http' ; import * as serveStatic from 'serve-static' ; import * as path from 'path' ; import * as socket from 'socket.io' ; import * as mongoose from 'mongoose' ; import * as redis from 'socket.io-redis' ; import { RoomSocket } from './socket' ; declare var process , __dirname ; export class Backend { private app : express . Application ; private server : any ; private io : any ; private mongo : mongoose . MongooseThenable ; private port : number ; constructor ( ) { this . app = express ( ) ; this . routes ( ) ; this . server = http . createServer ( this . app ) ; this . databases ( ) ; this . sockets ( ) ; } private routes ( ) { let router ; router = express . Router ( ) ; const root = path . join ( path . resolve ( __dirname , '../../target' ) ) ; this . app . use ( '/assets' , serveStatic ( path . resolve ( root , 'assets' ) ) ) ; router . get ( '/' , ( request , result ) => { result . sendFile ( path . join ( root , '/index.html' ) ) ; } ) ; this . app . use ( '*' , router ) ; } private databases ( ) { const MONGODB_URI = process . env . MONGODB_URI || 'mongodb://localhost/chat' ; this . mongo = mongoose . connect ( MONGODB_URI , { useMongoClient : true } ) ; ( < any > mongoose ) . Promise = global . Promise ; } private sockets ( ) { this . io = socket ( this . server ) ; const REDIS_URL = process . env . REDIS_URL || 'redis://localhost:6379' ; this . io . adapter ( redis ( REDIS_URL ) ) ; let roomSocket = new RoomSocket ( this . io ) ; } public listen ( ) { const port = process . env . PORT || 5000 ; this . server . listen ( port ) ; this . server . on ( 'error' , error => { console . log ( 'ERROR' , error ) ; } ) ; this . server . on ( 'listening' , ( ) => { console . log ( `==> Listening on port ${ port }. Open up http://localhost:${ port }/ in your browser.` ) ; } ) ; } }