ability to cancel construction and have a portion of the funds returned
[browser-rts.git] / src / lib / server.ts
index 249cc069fea921e24d0685dcbf7e1c798b974980..6615487bf31666301eba38268671f82e7af56e11 100644 (file)
@@ -1,10 +1,12 @@
 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';
+import {HttpError} from '../errors';
 
 type AuthInfo = {
        authInfo: { 
@@ -17,13 +19,17 @@ export type HttpHandler<I, O> = (params: I & AuthInfo, rawReq: Request, rawRes:
 
 export class HttpServer {
        server: express.Application;
-       port: number;
+  http: http.Server;
+  ws: Server;
+       port: string | number;
        bullAdapter: ExpressAdapter;
-       constructor(port: string) {
-               this.port = parseInt(port, 10);
+       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 +41,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`);
 
@@ -72,8 +84,9 @@ export class HttpServer {
                        }
                        catch(e) {
                                console.log(e);
-                               res.statusCode = (e as HttpError).statusCode || 500;
-                               res.json(e)
+                               res.send(`
+                               <div class="alert danger autofade">${e.message}</div>
+                               `);
                        }
                        finally {
                                res.end();
@@ -93,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));
        }
 }