Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.x] HTTP API healthcheck #413

Merged
merged 13 commits into from
Mar 4, 2021
427 changes: 427 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"async": "^3.2.0",
"bluebird": "^3.7.2",
"bluebird-global": "^1.0.1",
"body-parser": "^1.19.0",
"bptf-listings-2": "^1.2.0",
"bptf-login-2": "^1.0.2",
"callback-queue": "^3.0.0",
Expand All @@ -38,6 +39,7 @@
"dot-prop": "^6.0.1",
"dotenv": "^8.2.0",
"eslint-config-prettier": "^8.1.0",
"express": "^4.17.1",
"graceful-fs": "^4.2.6",
"isobject": "^4.0.0",
"js-levenshtein": "^1.1.6",
Expand Down Expand Up @@ -73,6 +75,7 @@
"@types/bluebird-global": "^3.5.12",
"@types/cheerio": "^0.22.24",
"@types/death": "^1.1.1",
"@types/express": "^4.17.11",
"@types/graceful-fs": "^4.1.5",
"@types/jest": "^26.0.20",
"@types/pluralize": "0.0.29",
Expand Down
11 changes: 11 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const { version: BOT_VERSION } = require('../package.json');
import { getPricer } from '@pricer/pricer';
import Pricer, { GetPricerFn } from './classes/Pricer';
import { loadOptions } from './classes/Options';
import HttpManager from './classes/HttpManager';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about importing only if enabled, to reduce memory usage slightly, but this is not that important

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ES6 and TS don't allow importing with import from within classes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know, either use require or let it be like it is now


process.env.BOT_VERSION = BOT_VERSION as string;

Expand Down Expand Up @@ -130,4 +131,14 @@ void botManager.start(options).asCallback(err => {
if (err) {
throw err;
}

if (options.enableHttpApi) {
const httpManager = new HttpManager(options);

void httpManager.start().asCallback(err => {
if (err) {
throw err;
}
});
}
});
54 changes: 54 additions & 0 deletions src/classes/HttpManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/* eslint-disable @typescript-eslint/no-unsafe-return */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/no-unsafe-call */

import bodyParser from 'body-parser';
import express from 'express';
import log from '../lib/logger';
import Options from './Options';

export default class HttpManager {
/**
* The Express.js app.
*/
protected app: express.Application;

/**
* The Express.js server app.
*/
public server;
rennokki marked this conversation as resolved.
Show resolved Hide resolved

/**
* Initialize the HTTP manager.
*
* @param options - The options list.
*/
constructor(protected options: Options) {
this.app = express();
this.app.use(bodyParser.json());
this.app.use(bodyParser.urlencoded({ extended: false }));

this.registerRoutes();
}

/**
* Register the routes.
*/
protected registerRoutes(): void {
this.app.get('/health', (req, res) => res.send('OK'));
this.app.get('/uptime', (req, res) => res.json({ uptime: process.uptime() }));
}

/**
* Start the server.
*/
start(): Promise<void> {
return new Promise(resolve => {
this.server = this.app.listen(this.options.httpApiPort, () => {
resolve();
log.debug(`HTTP Server started: http://127.0.0.1:${this.options.httpApiPort}`);
});
});
}
}
10 changes: 9 additions & 1 deletion src/classes/Options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,7 @@ export const DEFAULTS = {
}
}
},

detailsExtra: {
/**
* Custom string to be shown in listing note if details.highValue.showSpells set to true
Expand Down Expand Up @@ -1814,6 +1815,9 @@ export default interface Options extends JsonOptions {

folderName?: string;
filePrefix?: string;

enableHttpApi?: boolean;
httpApiPort?: number;
}

function getOption<T>(option: string, def: T, parseFn: (target: string) => T, options?: Options): T {
Expand Down Expand Up @@ -1909,6 +1913,7 @@ export function loadOptions(options?: Options): Options {

const jsonParseArray = (jsonString: string): string[] => (JSON.parse(jsonString) as unknown) as string[];
const jsonParseBoolean = (jsonString: string): boolean => (JSON.parse(jsonString) as unknown) as boolean;
const jsonParseNumber = (jsonString: string): number => (JSON.parse(jsonString) as unknown) as number;

const envOptions = {
steamAccountName: steamAccountName,
Expand Down Expand Up @@ -1937,7 +1942,10 @@ export function loadOptions(options?: Options): Options {
timeAdditionalNotes: getOption('timeAdditionalNotes', '', String, incomingOptions),

debug: getOption('debug', true, jsonParseBoolean, incomingOptions),
debugFile: getOption('debugFile', true, jsonParseBoolean, incomingOptions)
debugFile: getOption('debugFile', true, jsonParseBoolean, incomingOptions),

enableHttpApi: getOption('enableHttpApi', false, jsonParseBoolean, incomingOptions),
httpApiPort: getOption('httpApiPort', 80, jsonParseNumber, incomingOptions)
};

if (!envOptions.steamAccountName) {
Expand Down
5 changes: 4 additions & 1 deletion template.ecosystem.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@
"TIME_ADDITIONAL_NOTES": "",

"DEBUG": true,
"DEBUG_FILE": true
"DEBUG_FILE": true,

"ENABLE_HTTP_API": false,
"HTTP_API_PORT": 80
}
}
]
Expand Down
3 changes: 3 additions & 0 deletions template.env
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@ TIME_ADDITIONAL_NOTES=""

DEBUG=true
DEBUG_FILE=true

ENABLE_HTTP_API=false
HTTP_API_PORT=80