Skip to content

Commit

Permalink
serve client viewer on same port as socket server
Browse files Browse the repository at this point in the history
  • Loading branch information
pavlobu committed Aug 30, 2020
1 parent 68a5dd8 commit 114c363
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 269 deletions.
5 changes: 5 additions & 0 deletions .codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
coverage:
status:
patch:
default:
enabled: no
2 changes: 1 addition & 1 deletion .github/workflows/codecov.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: codecov generate
on: [push, pull_request]
on: [pull_request]
jobs:
run:
runs-on: ubuntu-18.04
Expand Down
5 changes: 5 additions & 0 deletions app/client/.codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
coverage:
status:
patch:
default:
enabled: no
2 changes: 1 addition & 1 deletion app/main.dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { autoUpdater } from 'electron-updater';
import log from 'electron-log';
import config from './configs/app.config';
import i18n from './configs/i18next.config';
import signalingServer from './server/signalingServer';
import signalingServer from './server';
import MenuBuilder from './menu';

const globalAny: any = global;
Expand Down
2 changes: 1 addition & 1 deletion app/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {

import config from './configs/app.config';

import signalingServer from './server/signalingServer';
import signalingServer from './server';

interface DarwinMenuItemConstructorOptions extends MenuItemConstructorOptions {
selector?: string;
Expand Down
138 changes: 106 additions & 32 deletions app/server/index.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,128 @@
/* eslint-disable no-console */
import { Server } from 'http';
// import express, { Request, Response, NextFunction } from 'express';
import http, { Server } from 'http';
import express from 'express';
import Koa from 'koa';
import Io from 'socket.io';
import cors from 'kcors';
import Router from 'koa-router';
import crypto from 'crypto';
import koaStatic from 'koa-static';
import koaSend from 'koa-send';
import getPort from 'get-port';
// import * as bodyParser from "body-parser";
// import express from "express";
// import { Routes } from "./routes";
// eslint-disable-next-line import/no-cycle
import Socket from './socket';
import pollForInactiveRooms from './inactive_rooms';
import getStore from './store';

let isDev;
try {
// eslint-disable-next-line global-require
isDev = require('electron-is-dev');
} catch (e) {
isDev = true;
}

require('dotenv').config();

const app = new Koa();

const router = new Router();

const store = getStore();

app.use(cors());
app.use(router.routes());

function setStaticFileHeaders(
ctx: Koa.ParameterizedContext<Koa.DefaultState, Koa.DefaultContext>
) {
ctx.set({
'strict-transport-security': 'max-age=31536000',
'X-Frame-Options': 'deny',
'X-XSS-Protection': '1; mode=block',
'X-Content-Type-Options': 'nosniff',
'Referrer-Policy': 'no-referrer',
'Feature-Policy':
"geolocation 'none'; vr 'none'; payment 'none'; microphone 'none'",
});
}

const clientDistDirectory = isDev
? `${__dirname}/../client/build`
: `${__dirname}/client/build`;

if (clientDistDirectory) {
app.use(async (ctx, next) => {
setStaticFileHeaders(ctx);
await koaStatic(clientDistDirectory)(ctx, next);
});

app.use(async (ctx) => {
setStaticFileHeaders(ctx);
await koaSend(ctx, 'index.html', { root: clientDistDirectory });
});
} else {
app.use(async (ctx) => {
ctx.body = { ready: true };
});
}

const protocol = http;

const server = protocol.createServer(app.callback());
const io = Io(server, {
pingInterval: 20000,
pingTimeout: 5000,
serveClient: false,
});

const getRoomIdHash = (id: string) => {
return crypto.createHash('sha256').update(id).digest('hex');
};

io.on('connection', async (socket) => {
const { roomId } = socket.handshake.query;

const roomIdHash = getRoomIdHash(roomId);

let room = await store.get('rooms', roomIdHash);
room = JSON.parse(room || '{}');

// eslint-disable-next-line no-new
new Socket({
roomIdOriginal: roomId,
roomId: roomIdHash,
socket,
room,
});
});

const init = async (PORT: number) => {
pollForInactiveRooms();

return server.listen(PORT, () => {
console.log(`Signaling server is online at port ${PORT}`);
});
};

class SignalingServer {
private static instance: SignalingServer;

public expressApp: express.Application;

public count: number;

public server: Server;

public port: number;
// public routePrv: Routes = new Routes();

constructor() {
this.expressApp = express();
this.count = 0;
this.config();
this.server = new Server();
this.port = 3131;
// this.routePrv.routes(this.app);
}

public async start(): Promise<Server> {
// this.port = await getRandomPort();
this.port = await getPort({ port: 3131 });
this.server = this.expressApp.listen(this.port);
this.server = await init(this.port);
console.log(`\n\nDeskreen signaling server started at port: ${this.port}`);
return this.server;
}
Expand All @@ -40,25 +131,6 @@ class SignalingServer {
this.server.close();
}

private config(): void {
// support application/json type post data
// this.app.use(bodyParser.json());
// support application/x-www-form-urlencoded post data
// this.app.use(bodyParser.urlencoded({ extended: false }));
// responde with indented JSON string
// this.app.set("json spaces", 2);
this.expressApp.get('/', (_, res) => {
this.count += 1;
res.status(200);
res.write('Deskreen signaling server is running');
res.send();
});
this.expressApp.get('/favicon.ico', (_, res) => {
res.status(204);
res.send();
});
}

public static getInstance(): SignalingServer {
if (!SignalingServer.instance) {
SignalingServer.instance = new SignalingServer();
Expand All @@ -68,4 +140,6 @@ class SignalingServer {
}
}

export const getIO = () => io;

export default SignalingServer.getInstance();
Loading

0 comments on commit 114c363

Please sign in to comment.