Skip to content

Commit

Permalink
feat(API): run API & Socket Server on same single port
Browse files Browse the repository at this point in the history
  • Loading branch information
jkuri committed Nov 6, 2017
1 parent 6da3a24 commit 78fd624
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 41 deletions.
35 changes: 34 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -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",
Expand Down
27 changes: 15 additions & 12 deletions src/api/index.ts
Expand Up @@ -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())
Expand All @@ -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 changes: 3 additions & 11 deletions src/api/server.ts
Expand Up @@ -14,7 +14,7 @@ export interface ServerConfig {

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

export const sessionParser = session({
Expand All @@ -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());
Expand All @@ -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 changes: 14 additions & 12 deletions src/api/socket.ts
Expand Up @@ -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 {
Expand All @@ -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;
Expand Down Expand Up @@ -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) => {
Expand Down
1 change: 0 additions & 1 deletion src/api/utils.ts
Expand Up @@ -25,7 +25,6 @@ const defaultConfig = {
secret: 'thisIsSecret',
jwtSecret: 'abstruseSecret4321!!',
port: 6500,
wsport: 6501,
concurrency: 10,
idleTimeout: 600,
jobTimeout: 3600,
Expand Down
7 changes: 3 additions & 4 deletions src/app/services/config.service.ts
Expand Up @@ -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}`;
}
}

Expand Down

0 comments on commit 78fd624

Please sign in to comment.