global chat via socket.io
[browser-rts.git] / src / lib / server.ts
index 00f41fa2b36da892f6e8b55bcaba1339d6a6d00a..e27981298d64701ae4a7fcff617c09fede624595 100644 (file)
@@ -1,10 +1,11 @@
 import express, { Request, Response } from 'express';
 import { join } from 'path';
 import { isString } from 'lodash';
-import { HttpError } from '../errors';
 import { merge } from 'lodash';
 import bodyParser from 'body-parser';
 import { ExpressAdapter } from '@bull-board/express';
+import http from 'http';
+import { Server } from 'socket.io';
 
 type AuthInfo = {
        authInfo: { 
@@ -17,13 +18,17 @@ export type HttpHandler<I, O> = (params: I & AuthInfo, rawReq: Request, rawRes:
 
 export class HttpServer {
        server: express.Application;
+  http: http.Server;
+  ws: Server;
        port: string | number;
        bullAdapter: ExpressAdapter;
        constructor(port: string | number) {
                this.port = port;
+    this.server = express();
                this.bullAdapter = new ExpressAdapter()
-               this.server = express();
                this.configureMiddleWare();
+    this.http = http.createServer(this.server);
+    this.ws = new Server(this.http);
        }
 
        configureMiddleWare() {
@@ -35,27 +40,33 @@ export class HttpServer {
                this.server.use('/admin/queues', this.bullAdapter.getRouter());
        }
 
+  authFromUrl(raw: string): {authInfo: {token: string, accountId: string}} {
+    let url = new URL('http://localhost.com?id=null&token=null');
+    try {
+      url = new URL(raw);
+      const authInfo = {
+        authInfo: {
+          token: url.searchParams.get('token'),
+          accountId: url.searchParams.get('id')
+        }
+      };
+
+      return authInfo;
+    }
+    catch(e) {
+      console.log(e);
+    }
+  }
+
        wrap<I, O>(handler: HttpHandler<I, O>, hxEvents: string) {
+    const self = this;
                return async function (req: Request, res: Response) {
                        try {
                                const start = Date.now();
                                console.log(`Req: ${req.method.toUpperCase()} ${req.path}`);
 
                                // extract hx game vars (token, id);
-                               let url = new URL('http://localhost.com?id=null&token=null');
-                               try {
-                                       url = new URL(req.headers['hx-current-url'].toString());
-                               }
-                               catch(e) {
-                                       console.log(e);
-                               }
-                               const headerData = {
-                                       authInfo: {
-                                               token: url.searchParams.get('token'),
-                                               accountId: url.searchParams.get('id')
-                                       }
-                               };
-
+        const headerData = self.authFromUrl(req.headers['hx-current-url'].toString());
                                const output: O = await handler(merge(req, headerData) as unknown as (I & AuthInfo), req, res);
                                console.log(`Runtime: ${Date.now() - start}ms`);
 
@@ -95,6 +106,6 @@ export class HttpServer {
 
        start(fn?: any): void {
                console.log(`Listening on port ${this.port}`);
-               this.server.listen(this.port, fn?.bind(this));
+               this.http.listen(this.port, fn?.bind(this));
        }
 }