Permalink
Browse files

feat(API): run API & Socket Server on same single port

  • Loading branch information...
jkuri committed Nov 6, 2017
1 parent 6da3a24 commit 78fd6243985bf0a25625179bbe6bd5a31b3c361c
Showing with 70 additions and 41 deletions.
  1. +34 −1 package-lock.json
  2. +1 −0 package.json
  3. +15 −12 src/api/index.ts
  4. +3 −11 src/api/server.ts
  5. +14 −12 src/api/socket.ts
  6. +0 −1 src/api/utils.ts
  7. +3 −4 src/app/services/config.service.ts

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -126,6 +126,7 @@
"enhanced-resolve": "^3.4.1",
"express": "^4.16.2",
"express-session": "^1.15.6",
"express-ws": "^3.0.0",
"extract-text-webpack-plugin": "^3.0.2",
"file-loader": "^1.1.5",
"fs-extra": "^4.0.2",
@@ -17,9 +17,9 @@ import { initSetup, getAbstruseVersion } from './utils';
import { generateKeys } from './security';
import * as db from './db/migrations';
import chalk from 'chalk';
import * as expressWs from 'express-ws';

const server = new ExpressServer({ port: 6500 });
const socket = new SocketServer({ port: 6501 });

initSetup()
.then(() => db.create())
@@ -32,14 +32,17 @@ initSetup()
};
logger.next(msg);
})
.then(() => {
Observable
.merge(...[server.start(), socket.start(), generateKeys()])
.subscribe(data => {
const msg: LogMessageType = { message: data, type: 'info', notify: false };
logger.next(msg);
}, err => {
const msg: LogMessageType = { message: err, type: 'error', notify: false };
logger.next(msg);
});
});
.then(() =>
server.start().subscribe(app => {
const socket = new SocketServer({ app: expressWs(app) });

Observable
.merge(...[socket.start(), generateKeys()])
.subscribe(data => {
const msg: LogMessageType = { message: data, type: 'info', notify: false };
logger.next(msg);
}, err => {
const msg: LogMessageType = { message: err, type: 'error', notify: false };
logger.next(msg);
});
}));
@@ -14,7 +14,7 @@ export interface ServerConfig {

export interface IExpressServer {
config: ServerConfig;
start(): Observable<string>;
start(): Observable<express.Application>;
}

export const sessionParser = session({
@@ -30,7 +30,7 @@ export class ExpressServer implements IExpressServer {
this.config = config;
}

start(): Observable<string> {
start(): Observable<express.Application> {
return new Observable(observer => {
const app: express.Application = express();
app.use(cors());
@@ -53,15 +53,7 @@ export class ExpressServer implements IExpressServer {
app.use('/badge', routes.badgeRoutes());
app.use(routes.webRoutes());


app.listen(this.config.port, () => {
const msg: LogMessageType = {
message: `[http]: server running on port ${this.config.port}`,
type: 'info',
notify: false
};
logger.next(msg);
});
observer.next(app);
});
}
}
@@ -26,7 +26,7 @@ import { ICpuData, cpu } from './stats/cpu';
import { decodeJwt } from './security';

export interface ISocketServerOptions {
port: number;
app: express.Application;
}

export interface IOutput {
@@ -50,7 +50,7 @@ export class SocketServer {

start(): Observable<string> {
return new Observable(observer => {
this.createRxServer(this.options)
this.createRxServer(this.options.app)
.map(data => {
this.token = data.conn.protocol;
this.connectingClient = data.session;
@@ -287,27 +287,29 @@ export class SocketServer {
});
}

private createRxServer = (options: ws.ServerOptions) => {
private createRxServer = (appplication: any) => {
return new Observable((observer: Observer<any>) => {
let config: any = getConfig();
const expressApp = appplication.app;
let server;

if (config.ssl) {
server = https.createServer({
cert: readFileSync(config.sslcert),
key: readFileSync(config.sslkey)
}, express());
}, expressApp);
} else {
server = http.createServer();
server = http.createServer(expressApp);
}

server.listen(options.port);
const msg: LogMessageType = {
message: `[socket]: server running at port ${options.port}`,
type: 'info',
notify: false
};
logger.next(msg);
server.listen(config.port, () => {
const msg: LogMessageType = {
message: `[server]: API and Socket Server running at port ${config.port}`,
type: 'info',
notify: false
};
logger.next(msg);
});

let wss: ws.Server = new ws.Server({
verifyClient: (info: any, done) => {
@@ -25,7 +25,6 @@ const defaultConfig = {
secret: 'thisIsSecret',
jwtSecret: 'abstruseSecret4321!!',
port: 6500,
wsport: 6501,
concurrency: 10,
idleTimeout: 600,
jobTimeout: 3600,
@@ -8,12 +8,11 @@ export class ConfigService {
wsurl: string;

constructor() {
let proto = location.protocol === 'https:' ? 'wss' : 'ws';
this.wsurl = `${proto}://${location.hostname}:6501`;

proto = location.protocol;
let wssProto = location.protocol === 'https:' ? 'wss' : 'ws';
let proto = location.protocol;
let port = location.port === '8000' ? 6500 : location.port;
this.url = `${proto}//${location.hostname}:${port}`;
this.wsurl = `${wssProto}://${location.hostname}:${port}`;
}
}

0 comments on commit 78fd624

Please sign in to comment.