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: {
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() {
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`);
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));
}
}