From 8b33c51a2d4c84a2f0da1d072cb1098557338518 Mon Sep 17 00:00:00 2001 From: Alex Renoki Date: Thu, 4 Mar 2021 16:21:59 +0200 Subject: [PATCH 01/13] Implemented HttpManager for Express.js --- package-lock.json | 427 ++++++++++++++++++++++++++++ package.json | 3 + src/app.ts | 9 + src/classes/HttpManager.ts | 59 ++++ src/classes/Options.ts | 14 + src/schemas/options-json/options.ts | 11 + 6 files changed, 523 insertions(+) create mode 100644 src/classes/HttpManager.ts diff --git a/package-lock.json b/package-lock.json index a0a27ae58..0ffb49de2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1792,6 +1792,16 @@ "@types/bluebird": "*" } }, + "@types/body-parser": { + "version": "1.19.0", + "resolved": "https://registry.npmjs.org/@types/body-parser/-/body-parser-1.19.0.tgz", + "integrity": "sha512-W98JrE0j2K78swW4ukqMleo8R7h/pFETjM2DQ90MF6XK2i4LO4W3gQ71Lt4w3bfm2EvVSyWHplECvB5sK22yFQ==", + "dev": true, + "requires": { + "@types/connect": "*", + "@types/node": "*" + } + }, "@types/caseless": { "version": "0.12.2", "resolved": "https://registry.npmjs.org/@types/caseless/-/caseless-0.12.2.tgz", @@ -1807,12 +1817,44 @@ "@types/node": "*" } }, + "@types/connect": { + "version": "3.4.34", + "resolved": "https://registry.npmjs.org/@types/connect/-/connect-3.4.34.tgz", + "integrity": "sha512-ePPA/JuI+X0vb+gSWlPKOY0NdNAie/rPUqX2GUPpbZwiKTkSPhjXWuee47E4MtE54QVzGCQMQkAL6JhV2E1+cQ==", + "dev": true, + "requires": { + "@types/node": "*" + } + }, "@types/death": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/@types/death/-/death-1.1.1.tgz", "integrity": "sha512-SRZdlsuCMY8iJQQ5CPRHivXR9+mw25peEG9hLEg/styIQ0jiXO90CrNhf7aGFaMFc8wPA+R87QT+AY15u3mO/g==", "dev": true }, + "@types/express": { + "version": "4.17.11", + "resolved": "https://registry.npmjs.org/@types/express/-/express-4.17.11.tgz", + "integrity": "sha512-no+R6rW60JEc59977wIxreQVsIEOAYwgCqldrA/vkpCnbD7MqTefO97lmoBe4WE0F156bC4uLSP1XHDOySnChg==", + "dev": true, + "requires": { + "@types/body-parser": "*", + "@types/express-serve-static-core": "^4.17.18", + "@types/qs": "*", + "@types/serve-static": "*" + } + }, + "@types/express-serve-static-core": { + "version": "4.17.18", + "resolved": "https://registry.npmjs.org/@types/express-serve-static-core/-/express-serve-static-core-4.17.18.tgz", + "integrity": "sha512-m4JTwx5RUBNZvky/JJ8swEJPKFd8si08pPF2PfizYjGZOKr/svUWPcoUmLow6MmPzhasphB7gSTINY67xn3JNA==", + "dev": true, + "requires": { + "@types/node": "*", + "@types/qs": "*", + "@types/range-parser": "*" + } + }, "@types/graceful-fs": { "version": "4.1.5", "resolved": "https://registry.npmjs.org/@types/graceful-fs/-/graceful-fs-4.1.5.tgz", @@ -1867,6 +1909,12 @@ "resolved": "https://registry.npmjs.org/@types/long/-/long-4.0.1.tgz", "integrity": "sha512-5tXH6Bx/kNGd3MgffdmP4dy2Z+G4eaXw0SE81Tq3BNadtnMR5/ySMzX4SLEzHJzSmPNn4HIdpQsBvXMUykr58w==" }, + "@types/mime": { + "version": "1.3.2", + "resolved": "https://registry.npmjs.org/@types/mime/-/mime-1.3.2.tgz", + "integrity": "sha512-YATxVxgRqNH6nHEIsvg6k2Boc1JHI9ZbH5iWFFv/MTkchz3b1ieGDa5T0a9RznNdI0KhVbdbWSN+KWWrQZRxTw==", + "dev": true + }, "@types/node": { "version": "10.14.22", "resolved": "https://registry.npmjs.org/@types/node/-/node-10.14.22.tgz", @@ -1891,6 +1939,18 @@ "integrity": "sha512-UEyp8LwZ4Dg30kVU2Q3amHHyTn1jEdhCIE59ANed76GaT1Vp76DD3ZWSAxgCrw6wJ0TqeoBpqmfUHiUDPs//HQ==", "dev": true }, + "@types/qs": { + "version": "6.9.5", + "resolved": "https://registry.npmjs.org/@types/qs/-/qs-6.9.5.tgz", + "integrity": "sha512-/JHkVHtx/REVG0VVToGRGH2+23hsYLHdyG+GrvoUGlGAd0ErauXDyvHtRI/7H7mzLm+tBCKA7pfcpkQ1lf58iQ==", + "dev": true + }, + "@types/range-parser": { + "version": "1.2.3", + "resolved": "https://registry.npmjs.org/@types/range-parser/-/range-parser-1.2.3.tgz", + "integrity": "sha512-ewFXqrQHlFsgc09MK5jP5iR7vumV/BYayNC6PgJO2LPe8vrnNFyjQjSppfEngITi0qvfKtzFvgKymGheFM9UOA==", + "dev": true + }, "@types/request": { "version": "2.48.5", "resolved": "https://registry.npmjs.org/@types/request/-/request-2.48.5.tgz", @@ -1928,6 +1988,16 @@ "integrity": "sha512-+nVsLKlcUCeMzD2ufHEYuJ9a2ovstb6Dp52A5VsoKxDXgvE051XgHI/33I1EymwkRGQkwnA0LkhnUzituGs4EQ==", "dev": true }, + "@types/serve-static": { + "version": "1.13.9", + "resolved": "https://registry.npmjs.org/@types/serve-static/-/serve-static-1.13.9.tgz", + "integrity": "sha512-ZFqF6qa48XsPdjXV5Gsz0Zqmux2PerNd3a/ktL45mHpa19cuMi/cL8tcxdAx497yRh+QtYPuofjT9oWw9P7nkA==", + "dev": true, + "requires": { + "@types/mime": "^1", + "@types/node": "*" + } + }, "@types/socket.io-client": { "version": "1.4.34", "resolved": "https://registry.npmjs.org/@types/socket.io-client/-/socket.io-client-1.4.34.tgz", @@ -2240,6 +2310,15 @@ "integrity": "sha512-nne9/IiQ/hzIhY6pdDnbBtz7DjPTKrY00P/zvPSm5pOFkl6xuGrGnXn/VtTNNfNtAfZ9/1RtehkszU9qcTii0Q==", "dev": true }, + "accepts": { + "version": "1.3.7", + "resolved": "https://registry.npmjs.org/accepts/-/accepts-1.3.7.tgz", + "integrity": "sha512-Il80Qs2WjYlJIBNzNkK6KYqlVMTbZLXgHx2oT0pU/fjRHyEp+PEfEPY0R3WCwAGVOtauxh1hOxNgIf5bv7dQpA==", + "requires": { + "mime-types": "~2.1.24", + "negotiator": "0.6.2" + } + }, "acorn": { "version": "7.2.0", "resolved": "https://registry.npmjs.org/acorn/-/acorn-7.2.0.tgz", @@ -2441,6 +2520,11 @@ "resolved": "https://registry.npmjs.org/array-filter/-/array-filter-1.0.0.tgz", "integrity": "sha1-uveeYubvTCpMC4MSMtr/7CUfnYM=" }, + "array-flatten": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz", + "integrity": "sha1-ml9pkFGx5wczKPKgCJaLZOopVdI=" + }, "array-union": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/array-union/-/array-union-2.1.0.tgz", @@ -2811,6 +2895,71 @@ "resolved": "https://registry.npmjs.org/bodec/-/bodec-0.1.0.tgz", "integrity": "sha1-vIUVVUMPI8n3ZQp172TGqUw0GMw=" }, + "body-parser": { + "version": "1.19.0", + "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.19.0.tgz", + "integrity": "sha512-dhEPs72UPbDnAQJ9ZKMNTP6ptJaionhP5cBb541nXPlW60Jepo9RV/a4fX4XWW9CuFNK22krhrj1+rgzifNCsw==", + "requires": { + "bytes": "3.1.0", + "content-type": "~1.0.4", + "debug": "2.6.9", + "depd": "~1.1.2", + "http-errors": "1.7.2", + "iconv-lite": "0.4.24", + "on-finished": "~2.3.0", + "qs": "6.7.0", + "raw-body": "2.4.0", + "type-is": "~1.6.17" + }, + "dependencies": { + "debug": { + "version": "2.6.9", + "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", + "integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==", + "requires": { + "ms": "2.0.0" + } + }, + "http-errors": { + "version": "1.7.2", + "resolved": "https://registry.npmjs.org/http-errors/-/http-errors-1.7.2.tgz", + "integrity": "sha512-uUQBt3H/cSIVfch6i1EuPNy/YsRSOUBXTVfZ+yR7Zjez3qjBz6i9+i4zjNaoqcoFVI4lQJ5plg63TvGfRSDCRg==", + "requires": { + "depd": "~1.1.2", + "inherits": "2.0.3", + "setprototypeof": "1.1.1", + "statuses": ">= 1.5.0 < 2", + "toidentifier": "1.0.0" + } + }, + "inherits": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz", + "integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=" + }, + "ms": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", + "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=" + }, + "qs": { + "version": "6.7.0", + "resolved": "https://registry.npmjs.org/qs/-/qs-6.7.0.tgz", + "integrity": "sha512-VCdBRNFTX1fyE7Nb6FYoURo/SPe62QCaAyzJvUjwRaIsc+NePBEniHlvxFmmX56+HZphIGtV0XeCirBtpDrTyQ==" + }, + "raw-body": { + "version": "2.4.0", + "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.4.0.tgz", + "integrity": "sha512-4Oz8DUIwdvoa5qMJelxipzi/iJIi40O5cGV1wNYp5hvZP8ZN0T+jiNkL0QepXs+EsQ9XJ8ipEDoiH70ySUJP3Q==", + "requires": { + "bytes": "3.1.0", + "http-errors": "1.7.2", + "iconv-lite": "0.4.24", + "unpipe": "1.0.0" + } + } + } + }, "boolbase": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/boolbase/-/boolbase-1.0.0.tgz", @@ -3598,6 +3747,19 @@ } } }, + "content-disposition": { + "version": "0.5.3", + "resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-0.5.3.tgz", + "integrity": "sha512-ExO0774ikEObIAEV9kDo50o+79VCUdEB6n6lzKgGwupcVeRlhrj3qGAfwq8G6uBJjkqLrhT0qEYFcWng8z1z0g==", + "requires": { + "safe-buffer": "5.1.2" + } + }, + "content-type": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/content-type/-/content-type-1.0.4.tgz", + "integrity": "sha512-hIP3EEPs8tB9AT1L+NUqtwOAps4mk2Zob89MWXMHjHWg9milF/j4osnnQLXBCBFBk/tvIG/tUc9mOUJiPBhPXA==" + }, "continuation-local-storage": { "version": "3.2.1", "resolved": "https://registry.npmjs.org/continuation-local-storage/-/continuation-local-storage-3.2.1.tgz", @@ -3616,6 +3778,16 @@ "safe-buffer": "~5.1.1" } }, + "cookie": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.4.0.tgz", + "integrity": "sha512-+Hp8fLp57wnUSt0tY0tHEXh4voZRDnoIrZPqlo3DPiI4y9lwg/jqx+1Om94/W6ZaPDOUbnjOt/99w66zk+l1Xg==" + }, + "cookie-signature": { + "version": "1.0.6", + "resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz", + "integrity": "sha1-4wOogrNCzD7oylE6eZmXNNqzriw=" + }, "copy-descriptor": { "version": "0.1.1", "resolved": "https://registry.npmjs.org/copy-descriptor/-/copy-descriptor-0.1.1.tgz", @@ -3893,6 +4065,11 @@ "resolved": "https://registry.npmjs.org/depd/-/depd-1.1.2.tgz", "integrity": "sha1-m81S4UwJd2PnSbJ0xDRu0uVgtak=" }, + "destroy": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/destroy/-/destroy-1.0.4.tgz", + "integrity": "sha1-l4hXRCxEdJ5CBmE+N5RiBYJqvYA=" + }, "detect-newline": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/detect-newline/-/detect-newline-3.1.0.tgz", @@ -4015,6 +4192,11 @@ "safer-buffer": "^2.1.0" } }, + "ee-first": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", + "integrity": "sha1-WQxhFWsK4vTwJVcyoViyZrxWsh0=" + }, "emitter-listener": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/emitter-listener/-/emitter-listener-1.1.2.tgz", @@ -4040,6 +4222,11 @@ "resolved": "https://registry.npmjs.org/enabled/-/enabled-2.0.0.tgz", "integrity": "sha512-AKrN98kuwOzMIdAizXGI86UFBoo26CL21UM763y1h/GMSJ4/OHU9k2YlsmBpyScFo/wbLzWQJBMCW4+IO3/+OQ==" }, + "encodeurl": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/encodeurl/-/encodeurl-1.0.2.tgz", + "integrity": "sha1-rT/0yG7C0CkyL1oCw6mmBslbP1k=" + }, "end-of-stream": { "version": "1.4.4", "resolved": "https://registry.npmjs.org/end-of-stream/-/end-of-stream-1.4.4.tgz", @@ -4178,6 +4365,11 @@ "integrity": "sha512-8/uIhbG12Csjy2JEW7D9pHbreaVaS/OpN3ycnyvElTdwM5n6GY6W6e2IPemfvGZeUMqZ9A/3GqIZMgKnBhAw/Q==", "dev": true }, + "escape-html": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/escape-html/-/escape-html-1.0.3.tgz", + "integrity": "sha1-Aljq5NPQwJdN4cFpGI7wBR0dGYg=" + }, "escape-regexp": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/escape-regexp/-/escape-regexp-0.0.1.tgz", @@ -4599,6 +4791,11 @@ "resolved": "https://registry.npmjs.org/esutils/-/esutils-2.0.3.tgz", "integrity": "sha512-kVscqXk4OCp68SZ0dkgEKVi6/8ij300KBWTJq32P/dYeWTSwK41WyTxalN1eRmA5Z9UU/LX9D7FWSmV9SAYx6g==" }, + "etag": { + "version": "1.8.1", + "resolved": "https://registry.npmjs.org/etag/-/etag-1.8.1.tgz", + "integrity": "sha1-Qa4u62XvpiJorr/qg6x9eSmbCIc=" + }, "eventemitter2": { "version": "5.0.1", "resolved": "https://registry.npmjs.org/eventemitter2/-/eventemitter2-5.0.1.tgz", @@ -4734,6 +4931,63 @@ } } }, + "express": { + "version": "4.17.1", + "resolved": "https://registry.npmjs.org/express/-/express-4.17.1.tgz", + "integrity": "sha512-mHJ9O79RqluphRrcw2X/GTh3k9tVv8YcoyY4Kkh4WDMUYKRZUq0h1o0w2rrrxBqM7VoeUVqgb27xlEMXTnYt4g==", + "requires": { + "accepts": "~1.3.7", + "array-flatten": "1.1.1", + "body-parser": "1.19.0", + "content-disposition": "0.5.3", + "content-type": "~1.0.4", + "cookie": "0.4.0", + "cookie-signature": "1.0.6", + "debug": "2.6.9", + "depd": "~1.1.2", + "encodeurl": "~1.0.2", + "escape-html": "~1.0.3", + "etag": "~1.8.1", + "finalhandler": "~1.1.2", + "fresh": "0.5.2", + "merge-descriptors": "1.0.1", + "methods": "~1.1.2", + "on-finished": "~2.3.0", + "parseurl": "~1.3.3", + "path-to-regexp": "0.1.7", + "proxy-addr": "~2.0.5", + "qs": "6.7.0", + "range-parser": "~1.2.1", + "safe-buffer": "5.1.2", + "send": "0.17.1", + "serve-static": "1.14.1", + "setprototypeof": "1.1.1", + "statuses": "~1.5.0", + "type-is": "~1.6.18", + "utils-merge": "1.0.1", + "vary": "~1.1.2" + }, + "dependencies": { + "debug": { + "version": "2.6.9", + "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", + "integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==", + "requires": { + "ms": "2.0.0" + } + }, + "ms": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", + "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=" + }, + "qs": { + "version": "6.7.0", + "resolved": "https://registry.npmjs.org/qs/-/qs-6.7.0.tgz", + "integrity": "sha512-VCdBRNFTX1fyE7Nb6FYoURo/SPe62QCaAyzJvUjwRaIsc+NePBEniHlvxFmmX56+HZphIGtV0XeCirBtpDrTyQ==" + } + } + }, "extend": { "version": "3.0.2", "resolved": "https://registry.npmjs.org/extend/-/extend-3.0.2.tgz", @@ -4943,6 +5197,35 @@ "to-regex-range": "^5.0.1" } }, + "finalhandler": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/finalhandler/-/finalhandler-1.1.2.tgz", + "integrity": "sha512-aAWcW57uxVNrQZqFXjITpW3sIUQmHGG3qSb9mUah9MgMC4NeWhNOlNjXEYq3HjRAvL6arUviZGGJsBg6z0zsWA==", + "requires": { + "debug": "2.6.9", + "encodeurl": "~1.0.2", + "escape-html": "~1.0.3", + "on-finished": "~2.3.0", + "parseurl": "~1.3.3", + "statuses": "~1.5.0", + "unpipe": "~1.0.0" + }, + "dependencies": { + "debug": { + "version": "2.6.9", + "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", + "integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==", + "requires": { + "ms": "2.0.0" + } + }, + "ms": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", + "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=" + } + } + }, "find-up": { "version": "4.1.0", "resolved": "https://registry.npmjs.org/find-up/-/find-up-4.1.0.tgz", @@ -5005,6 +5288,11 @@ "mime-types": "^2.1.12" } }, + "forwarded": { + "version": "0.1.2", + "resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.1.2.tgz", + "integrity": "sha1-mMI9qxF1ZXuMBXPozszZGw/xjIQ=" + }, "fragment-cache": { "version": "0.2.1", "resolved": "https://registry.npmjs.org/fragment-cache/-/fragment-cache-0.2.1.tgz", @@ -5014,6 +5302,11 @@ "map-cache": "^0.2.2" } }, + "fresh": { + "version": "0.5.2", + "resolved": "https://registry.npmjs.org/fresh/-/fresh-0.5.2.tgz", + "integrity": "sha1-PYyt2Q2XZWn6g1qx+OSyOhBWBac=" + }, "fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", @@ -5617,6 +5910,11 @@ "integrity": "sha1-+ni/XS5pE8kRzp+BnuUUa7bYROk=", "dev": true }, + "ipaddr.js": { + "version": "1.9.1", + "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.1.tgz", + "integrity": "sha512-0KI/607xoxSToH7GjN1FfSbLoU0+btTicjsQSWQlh/hZykN8KpmMf7uYwPW3R+akZ6R/w18ZlXSHBYXiYUPO3g==" + }, "is-accessor-descriptor": { "version": "0.1.6", "resolved": "https://registry.npmjs.org/is-accessor-descriptor/-/is-accessor-descriptor-0.1.6.tgz", @@ -7742,6 +8040,16 @@ "object-visit": "^1.0.0" } }, + "media-typer": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz", + "integrity": "sha1-hxDXrwqmJvj/+hzgAWhUUmMlV0g=" + }, + "merge-descriptors": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-1.0.1.tgz", + "integrity": "sha1-sAqqVW3YtEVoFQ7J0blT8/kMu2E=" + }, "merge-stream": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/merge-stream/-/merge-stream-2.0.0.tgz", @@ -7754,6 +8062,11 @@ "integrity": "sha512-8q7VEgMJW4J8tcfVPy8g09NcQwZdbwFEqhe/WZkoIzjn/3TGDwtOCYtXGxA3O8tPzpczCCDgv+P2P5y00ZJOOg==", "dev": true }, + "methods": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/methods/-/methods-1.1.2.tgz", + "integrity": "sha1-VSmk1nZUE07cxSZmVoNbD4Ua/O4=" + }, "micromatch": { "version": "4.0.2", "resolved": "https://registry.npmjs.org/micromatch/-/micromatch-4.0.2.tgz", @@ -7764,6 +8077,11 @@ "picomatch": "^2.0.5" } }, + "mime": { + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/mime/-/mime-1.6.0.tgz", + "integrity": "sha512-x0Vn8spI+wuJ1O6S7gnbaQg8Pxh4NNHb7KSINmEWKiPE4RKOplvijn+NkmYmmRgP68mc70j2EbeTFRsrswaQeg==" + }, "mime-db": { "version": "1.40.0", "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.40.0.tgz", @@ -7907,6 +8225,11 @@ } } }, + "negotiator": { + "version": "0.6.2", + "resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.2.tgz", + "integrity": "sha512-hZXc7K2e+PgeI1eDBe/10Ard4ekbfrrqG8Ep+8Jmf4JID2bNg7NvCPOZN+kfF574pFQI7mum2AUqDidoKqcTOw==" + }, "netmask": { "version": "1.0.6", "resolved": "https://registry.npmjs.org/netmask/-/netmask-1.0.6.tgz", @@ -8256,6 +8579,14 @@ } } }, + "on-finished": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/on-finished/-/on-finished-2.3.0.tgz", + "integrity": "sha1-IPEzZIGwg811M3mSoWlxqi2QaUc=", + "requires": { + "ee-first": "1.1.1" + } + }, "once": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", @@ -8440,6 +8771,11 @@ "resolved": "https://registry.npmjs.org/parseuri/-/parseuri-0.0.6.tgz", "integrity": "sha512-AUjen8sAkGgao7UyCX6Ahv0gIK2fABKmYjvP4xmy5JaKvcbTRueIqIPHLAfq30xJddqSE033IOMUSOMCcK3Sow==" }, + "parseurl": { + "version": "1.3.3", + "resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.3.tgz", + "integrity": "sha512-CiyeOxFT/JZyN5m0z9PfXw4SCBJ6Sygz1Dpl0wqjlhDEGGBP1GnsUVEL0p63hoG1fcj3fHynXi9NYO4nWOL+qQ==" + }, "pascal-case": { "version": "3.1.2", "resolved": "https://registry.npmjs.org/pascal-case/-/pascal-case-3.1.2.tgz", @@ -8500,6 +8836,11 @@ "resolved": "https://registry.npmjs.org/path-parse/-/path-parse-1.0.6.tgz", "integrity": "sha512-GSmOT2EbHrINBf9SR7CDELwlJ8AENk3Qn7OikK4nFYAu3Ote2+JYNVvkpAEQm3/TLNEJFD/xZJjzyxg3KBWOzw==" }, + "path-to-regexp": { + "version": "0.1.7", + "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.7.tgz", + "integrity": "sha1-32BBeABfUi8V60SQ5yR6G/qmf4w=" + }, "path-type": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/path-type/-/path-type-4.0.0.tgz", @@ -8888,6 +9229,15 @@ } } }, + "proxy-addr": { + "version": "2.0.6", + "resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.6.tgz", + "integrity": "sha512-dh/frvCBVmSsDYzw6n926jv974gddhkFPfiN8hPOi30Wax25QZyZEGveluCgliBnqmuM+UJmBErbAUFIoDbjOw==", + "requires": { + "forwarded": "~0.1.2", + "ipaddr.js": "1.9.1" + } + }, "proxy-agent": { "version": "3.1.1", "resolved": "https://registry.npmjs.org/proxy-agent/-/proxy-agent-3.1.1.tgz", @@ -8986,6 +9336,11 @@ "resolved": "https://registry.npmjs.org/rand-token/-/rand-token-1.0.1.tgz", "integrity": "sha512-Zri5SfJmEzBJ3IexFdigvPSCamslJ7UjLkUn0tlgH7COJvaUr5V7FyUYgKifEMTw7gFO8ZLcWjcU+kq8akipzg==" }, + "range-parser": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/range-parser/-/range-parser-1.2.1.tgz", + "integrity": "sha512-Hrgsx+orqoygnmhFbKaHE6c296J+HTAQXoxEF6gNupROmmGJRoyzfG3ccAveqCBrwr/2yxQ5BVd/GTl5agOwSg==" + }, "raw-body": { "version": "2.4.1", "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.4.1.tgz", @@ -9553,6 +9908,48 @@ "semver": "^6.3.0" } }, + "send": { + "version": "0.17.1", + "resolved": "https://registry.npmjs.org/send/-/send-0.17.1.tgz", + "integrity": "sha512-BsVKsiGcQMFwT8UxypobUKyv7irCNRHk1T0G680vk88yf6LBByGcZJOTJCrTP2xVN6yI+XjPJcNuE3V4fT9sAg==", + "requires": { + "debug": "2.6.9", + "depd": "~1.1.2", + "destroy": "~1.0.4", + "encodeurl": "~1.0.2", + "escape-html": "~1.0.3", + "etag": "~1.8.1", + "fresh": "0.5.2", + "http-errors": "~1.7.2", + "mime": "1.6.0", + "ms": "2.1.1", + "on-finished": "~2.3.0", + "range-parser": "~1.2.1", + "statuses": "~1.5.0" + }, + "dependencies": { + "debug": { + "version": "2.6.9", + "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", + "integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==", + "requires": { + "ms": "2.0.0" + }, + "dependencies": { + "ms": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", + "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=" + } + } + }, + "ms": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.1.tgz", + "integrity": "sha512-tgp+dl5cGk28utYktBsrFqA7HKgrhgPsg6Z/EfhWI4gl1Hwq8B/GmY/0oXZ6nF8hDVesS/FpnYaD/kOWhYQvyg==" + } + } + }, "sentence-case": { "version": "3.0.4", "resolved": "https://registry.npmjs.org/sentence-case/-/sentence-case-3.0.4.tgz", @@ -9570,6 +9967,17 @@ } } }, + "serve-static": { + "version": "1.14.1", + "resolved": "https://registry.npmjs.org/serve-static/-/serve-static-1.14.1.tgz", + "integrity": "sha512-JMrvUwE54emCYWlTI+hGrGv5I8dEwmco/00EvkzIIsR7MqrHonbD9pO2MOfFnpFntl7ecpZs+3mW+XbQZu9QCg==", + "requires": { + "encodeurl": "~1.0.2", + "escape-html": "~1.0.3", + "parseurl": "~1.3.3", + "send": "0.17.1" + } + }, "set-blocking": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/set-blocking/-/set-blocking-2.0.0.tgz", @@ -10747,6 +11155,15 @@ "integrity": "sha512-4dbzIzqvjtgiM5rw1k5rEHtBANKmdudhGyBEajN01fEyhaAIhsoKNy6y7+IN93IfpFtwY9iqi7kD+xwKhQsNJA==", "dev": true }, + "type-is": { + "version": "1.6.18", + "resolved": "https://registry.npmjs.org/type-is/-/type-is-1.6.18.tgz", + "integrity": "sha512-TkRKr9sUTxEH8MdfuCSP7VizJyzRNMjj2J2do2Jr3Kym598JVdEksuzPQCnlFPW4ky9Q+iA+ma9BGm06XQBy8g==", + "requires": { + "media-typer": "0.3.0", + "mime-types": "~2.1.24" + } + }, "typedarray-to-buffer": { "version": "3.1.5", "resolved": "https://registry.npmjs.org/typedarray-to-buffer/-/typedarray-to-buffer-3.1.5.tgz", @@ -11029,6 +11446,11 @@ "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", "integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=" }, + "utils-merge": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz", + "integrity": "sha1-n5VxD1CiZ5R7LMwSR0HBAoQn5xM=" + }, "uuid": { "version": "3.3.3", "resolved": "https://registry.npmjs.org/uuid/-/uuid-3.3.3.tgz", @@ -11074,6 +11496,11 @@ "spdx-expression-parse": "^3.0.0" } }, + "vary": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz", + "integrity": "sha1-IpnwLG3tMNSllhsLn3RSShj2NPw=" + }, "vdf": { "version": "0.0.2", "resolved": "https://registry.npmjs.org/vdf/-/vdf-0.0.2.tgz", diff --git a/package.json b/package.json index 47865273c..293e6860c 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", @@ -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", diff --git a/src/app.ts b/src/app.ts index 5a0f4b693..fbde4ff2f 100644 --- a/src/app.ts +++ b/src/app.ts @@ -52,6 +52,9 @@ function _getPricer(): Pricer { } const botManager = new BotManager(_getPricer()); +import HttpManager from './classes/HttpManager'; +const httpManager = new HttpManager(options); + import ON_DEATH from 'death'; import * as inspect from 'util'; import { Webhook } from './lib/DiscordWebhook/interfaces'; @@ -130,4 +133,10 @@ void botManager.start(options).asCallback(err => { if (err) { throw err; } + + void httpManager.start().asCallback(err => { + if (err) { + throw err; + } + }); }); diff --git a/src/classes/HttpManager.ts b/src/classes/HttpManager.ts new file mode 100644 index 000000000..f80282ad0 --- /dev/null +++ b/src/classes/HttpManager.ts @@ -0,0 +1,59 @@ +/* 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 { uptimeAsUnix } from '../lib/tools/time'; +import Options from './Options'; + +export default class HttpManager { + /** + * The Express.js app. + */ + protected app: express.Application; + + /** + * The Express.js server app. + */ + public server; + + /** + * 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')); + } + + /** + * Start the server. + */ + start(): Promise { + return new Promise(resolve => { + if (this.options.http.enable) { + this.server = this.app.listen(this.options.http.port, () => { + resolve(); + log.debug(`HTTP Server started: http://127.0.0.1:${this.options.http.port}`); + }); + } else { + log.debug('HTTP Server is not enabled. Skipping initialization...'); + resolve(); + } + }); + } +} diff --git a/src/classes/Options.ts b/src/classes/Options.ts index a86c3776f..97e638514 100644 --- a/src/classes/Options.ts +++ b/src/classes/Options.ts @@ -654,6 +654,7 @@ export const DEFAULTS = { } } }, + detailsExtra: { /** * Custom string to be shown in listing note if details.highValue.showSpells set to true @@ -966,6 +967,11 @@ export const DEFAULTS = { 'Kills Under A Full Moon': '', 'Robots Killed During Halloween': '' } + }, + + http: { + enable: false, + port: 80 } }; @@ -1760,6 +1766,13 @@ interface StrangeParts { 'Robots Killed During Halloween'?: string; } +// ------------ HTTP ------------ + +interface Http { + enable: boolean; + port: number; +} + // ------------ JsonOptions ------------ export interface JsonOptions { @@ -1781,6 +1794,7 @@ export interface JsonOptions { customMessage?: CustomMessage; commands?: Commands; detailsExtra?: DetailsExtra; + http?: Http; } export default interface Options extends JsonOptions { diff --git a/src/schemas/options-json/options.ts b/src/schemas/options-json/options.ts index 006d8983f..938bfcf2e 100644 --- a/src/schemas/options-json/options.ts +++ b/src/schemas/options-json/options.ts @@ -2202,6 +2202,17 @@ export const optionsSchema: jsonschema.Schema = { }, required: ['spells', 'sheens', 'killstreakers', 'painted', 'strangeParts'], additionalProperties: false + }, + http: { + type: 'object', + properties: { + enable: { + type: 'boolean' + }, + port: { + type: 'number' + } + } } }, required: [ From 600d500c8a98c8f63ec16ede69918d2d1e7b272e Mon Sep 17 00:00:00 2001 From: Alex Renoki Date: Thu, 4 Mar 2021 16:27:31 +0200 Subject: [PATCH 02/13] =?UTF-8?q?=E2=8F=B2=20Uptime=20endpoint?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/classes/HttpManager.ts | 1 + src/lib/tools/time.ts | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/classes/HttpManager.ts b/src/classes/HttpManager.ts index f80282ad0..5f2fcfb09 100644 --- a/src/classes/HttpManager.ts +++ b/src/classes/HttpManager.ts @@ -38,6 +38,7 @@ export default class HttpManager { */ protected registerRoutes(): void { this.app.get('/health', (req, res) => res.send('OK')); + this.app.get('/uptime', (req, res) => res.json({ uptime: uptimeAsUnix() })); } /** diff --git a/src/lib/tools/time.ts b/src/lib/tools/time.ts index 23076e306..b48b30dfe 100644 --- a/src/lib/tools/time.ts +++ b/src/lib/tools/time.ts @@ -100,9 +100,13 @@ export function convertTime(time: number, showInMS: boolean): string { return `${dayjs.unix(Math.round((now.valueOf() - time) / 1000)).fromNow(true)}${showInMS ? ` (${time} ms)` : ''}`; } +export function uptimeAsUnix(): dayjs.Dayjs { + return dayjs.unix(dayjs().unix() - process.uptime()); +} + export function uptime(): string { const currentTime = dayjs(); - const uptimeAsMoment = dayjs.unix(currentTime.unix() - process.uptime()); + const uptimeAsMoment = uptimeAsUnix(); const hoursDiff = currentTime.diff(uptimeAsMoment, 'hour'); const daysDiff = currentTime.diff(uptimeAsMoment, 'day'); From a4098b4ba058b16553221bd38d67c307db930b5b Mon Sep 17 00:00:00 2001 From: Alex Renoki Date: Thu, 4 Mar 2021 19:15:41 +0200 Subject: [PATCH 03/13] Added ENABLE_HTTP_API and HTTP_API_PORT for the API --- src/classes/HttpManager.ts | 6 +++--- src/classes/Options.ts | 17 ++++------------- src/schemas/options-json/options.ts | 11 ----------- template.ecosystem.json | 5 ++++- 4 files changed, 11 insertions(+), 28 deletions(-) diff --git a/src/classes/HttpManager.ts b/src/classes/HttpManager.ts index 5f2fcfb09..1d3777814 100644 --- a/src/classes/HttpManager.ts +++ b/src/classes/HttpManager.ts @@ -46,10 +46,10 @@ export default class HttpManager { */ start(): Promise { return new Promise(resolve => { - if (this.options.http.enable) { - this.server = this.app.listen(this.options.http.port, () => { + if (this.options.enableHttpApi) { + this.server = this.app.listen(this.options.httpApiPort, () => { resolve(); - log.debug(`HTTP Server started: http://127.0.0.1:${this.options.http.port}`); + log.debug(`HTTP Server started: http://127.0.0.1:${this.options.httpApiPort}`); }); } else { log.debug('HTTP Server is not enabled. Skipping initialization...'); diff --git a/src/classes/Options.ts b/src/classes/Options.ts index 97e638514..3e7c713ef 100644 --- a/src/classes/Options.ts +++ b/src/classes/Options.ts @@ -967,11 +967,6 @@ export const DEFAULTS = { 'Kills Under A Full Moon': '', 'Robots Killed During Halloween': '' } - }, - - http: { - enable: false, - port: 80 } }; @@ -1766,13 +1761,6 @@ interface StrangeParts { 'Robots Killed During Halloween'?: string; } -// ------------ HTTP ------------ - -interface Http { - enable: boolean; - port: number; -} - // ------------ JsonOptions ------------ export interface JsonOptions { @@ -1794,7 +1782,6 @@ export interface JsonOptions { customMessage?: CustomMessage; commands?: Commands; detailsExtra?: DetailsExtra; - http?: Http; } export default interface Options extends JsonOptions { @@ -1813,6 +1800,7 @@ export default interface Options extends JsonOptions { alerts?: string[]; enableSocket?: boolean; + enableHttpApi?: boolean; customPricerApiToken?: string; customPricerUrl?: string; @@ -1828,6 +1816,8 @@ export default interface Options extends JsonOptions { folderName?: string; filePrefix?: string; + + httpApiPort?: number; } function getOption(option: string, def: T, parseFn: (target: string) => T, options?: Options): T { @@ -1940,6 +1930,7 @@ export function loadOptions(options?: Options): Options { alerts: getOption('alerts', ['trade'], jsonParseArray, incomingOptions), enableSocket: getOption('enableSocket', true, jsonParseBoolean, incomingOptions), + enableHttpApi: getOption('enableHttpApi', true, jsonParseBoolean, incomingOptions), customPricerApiToken: getOption('customPricerApiToken', '', String, incomingOptions), customPricerUrl: getOption('customPricerUrl', 'https://api.prices.tf', String, incomingOptions), diff --git a/src/schemas/options-json/options.ts b/src/schemas/options-json/options.ts index 938bfcf2e..006d8983f 100644 --- a/src/schemas/options-json/options.ts +++ b/src/schemas/options-json/options.ts @@ -2202,17 +2202,6 @@ export const optionsSchema: jsonschema.Schema = { }, required: ['spells', 'sheens', 'killstreakers', 'painted', 'strangeParts'], additionalProperties: false - }, - http: { - type: 'object', - properties: { - enable: { - type: 'boolean' - }, - port: { - type: 'number' - } - } } }, required: [ diff --git a/template.ecosystem.json b/template.ecosystem.json index b3304d04e..c20547bc0 100644 --- a/template.ecosystem.json +++ b/template.ecosystem.json @@ -28,6 +28,7 @@ "ALERTS": ["trade", "version"], "ENABLE_SOCKET": true, + "ENABLE_HTTP_API": false, "CUSTOM_PRICER_URL": "", "CUSTOM_PRICER_API_TOKEN": "", @@ -39,7 +40,9 @@ "TIME_ADDITIONAL_NOTES": "", "DEBUG": true, - "DEBUG_FILE": true + "DEBUG_FILE": true, + + "HTTP_API_PORT": 80 } } ] From 6e8a307e41d03b30138fd8bee7f005fdc94e79be Mon Sep 17 00:00:00 2001 From: Alex Renoki Date: Thu, 4 Mar 2021 19:19:29 +0200 Subject: [PATCH 04/13] Fixed default port number Added parser for numbers --- src/classes/Options.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/classes/Options.ts b/src/classes/Options.ts index 3e7c713ef..8ddbb2787 100644 --- a/src/classes/Options.ts +++ b/src/classes/Options.ts @@ -1913,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, @@ -1930,7 +1931,7 @@ export function loadOptions(options?: Options): Options { alerts: getOption('alerts', ['trade'], jsonParseArray, incomingOptions), enableSocket: getOption('enableSocket', true, jsonParseBoolean, incomingOptions), - enableHttpApi: getOption('enableHttpApi', true, jsonParseBoolean, incomingOptions), + enableHttpApi: getOption('enableHttpApi', false, jsonParseBoolean, incomingOptions), customPricerApiToken: getOption('customPricerApiToken', '', String, incomingOptions), customPricerUrl: getOption('customPricerUrl', 'https://api.prices.tf', String, incomingOptions), @@ -1942,7 +1943,9 @@ 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), + + httpApiPort: getOption('httpApiPort', 80, jsonParseNumber, incomingOptions) }; if (!envOptions.steamAccountName) { From 53e96ecd3c7ec785881270103f077605c445227d Mon Sep 17 00:00:00 2001 From: idinium96 <47635037+idinium96@users.noreply.github.com> Date: Fri, 5 Mar 2021 01:22:07 +0800 Subject: [PATCH 05/13] =?UTF-8?q?=F0=9F=94=84=20reposition=20env=20variabl?= =?UTF-8?q?es?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/classes/Options.ts | 4 ++-- template.ecosystem.json | 2 +- template.env | 3 +++ 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/classes/Options.ts b/src/classes/Options.ts index 8ddbb2787..f6d050d6d 100644 --- a/src/classes/Options.ts +++ b/src/classes/Options.ts @@ -1800,7 +1800,6 @@ export default interface Options extends JsonOptions { alerts?: string[]; enableSocket?: boolean; - enableHttpApi?: boolean; customPricerApiToken?: string; customPricerUrl?: string; @@ -1817,6 +1816,7 @@ export default interface Options extends JsonOptions { folderName?: string; filePrefix?: string; + enableHttpApi?: boolean; httpApiPort?: number; } @@ -1931,7 +1931,6 @@ export function loadOptions(options?: Options): Options { alerts: getOption('alerts', ['trade'], jsonParseArray, incomingOptions), enableSocket: getOption('enableSocket', true, jsonParseBoolean, incomingOptions), - enableHttpApi: getOption('enableHttpApi', false, jsonParseBoolean, incomingOptions), customPricerApiToken: getOption('customPricerApiToken', '', String, incomingOptions), customPricerUrl: getOption('customPricerUrl', 'https://api.prices.tf', String, incomingOptions), @@ -1945,6 +1944,7 @@ export function loadOptions(options?: Options): Options { debug: getOption('debug', true, jsonParseBoolean, incomingOptions), debugFile: getOption('debugFile', true, jsonParseBoolean, incomingOptions), + enableHttpApi: getOption('enableHttpApi', false, jsonParseBoolean, incomingOptions), httpApiPort: getOption('httpApiPort', 80, jsonParseNumber, incomingOptions) }; diff --git a/template.ecosystem.json b/template.ecosystem.json index c20547bc0..e32be5467 100644 --- a/template.ecosystem.json +++ b/template.ecosystem.json @@ -28,7 +28,6 @@ "ALERTS": ["trade", "version"], "ENABLE_SOCKET": true, - "ENABLE_HTTP_API": false, "CUSTOM_PRICER_URL": "", "CUSTOM_PRICER_API_TOKEN": "", @@ -42,6 +41,7 @@ "DEBUG": true, "DEBUG_FILE": true, + "ENABLE_HTTP_API": false, "HTTP_API_PORT": 80 } } diff --git a/template.env b/template.env index 38bbdacda..904ea7d52 100644 --- a/template.env +++ b/template.env @@ -27,3 +27,6 @@ TIME_ADDITIONAL_NOTES="" DEBUG=true DEBUG_FILE=true + +ENABLE_HTTP_API=false +HTTP_API_PORT=80 \ No newline at end of file From 74c1f5b7991fd81fd3ddd77e4ff5439c3cad1fe8 Mon Sep 17 00:00:00 2001 From: Alex Renoki Date: Thu, 4 Mar 2021 19:26:50 +0200 Subject: [PATCH 06/13] end of file --- template.env | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/template.env b/template.env index 904ea7d52..30b084356 100644 --- a/template.env +++ b/template.env @@ -29,4 +29,4 @@ DEBUG=true DEBUG_FILE=true ENABLE_HTTP_API=false -HTTP_API_PORT=80 \ No newline at end of file +HTTP_API_PORT=80 From 5ce6234ab84693fceddd3a5b00fd16600ce39c66 Mon Sep 17 00:00:00 2001 From: Alex Renoki Date: Thu, 4 Mar 2021 19:35:35 +0200 Subject: [PATCH 07/13] Reverted uptime changes --- src/classes/HttpManager.ts | 3 +-- src/lib/tools/time.ts | 6 +----- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/src/classes/HttpManager.ts b/src/classes/HttpManager.ts index 1d3777814..61d3bf980 100644 --- a/src/classes/HttpManager.ts +++ b/src/classes/HttpManager.ts @@ -6,7 +6,6 @@ import bodyParser from 'body-parser'; import express from 'express'; import log from '../lib/logger'; -import { uptimeAsUnix } from '../lib/tools/time'; import Options from './Options'; export default class HttpManager { @@ -38,7 +37,7 @@ export default class HttpManager { */ protected registerRoutes(): void { this.app.get('/health', (req, res) => res.send('OK')); - this.app.get('/uptime', (req, res) => res.json({ uptime: uptimeAsUnix() })); + this.app.get('/uptime', (req, res) => res.json({ uptime: process.uptime() })); } /** diff --git a/src/lib/tools/time.ts b/src/lib/tools/time.ts index b48b30dfe..23076e306 100644 --- a/src/lib/tools/time.ts +++ b/src/lib/tools/time.ts @@ -100,13 +100,9 @@ export function convertTime(time: number, showInMS: boolean): string { return `${dayjs.unix(Math.round((now.valueOf() - time) / 1000)).fromNow(true)}${showInMS ? ` (${time} ms)` : ''}`; } -export function uptimeAsUnix(): dayjs.Dayjs { - return dayjs.unix(dayjs().unix() - process.uptime()); -} - export function uptime(): string { const currentTime = dayjs(); - const uptimeAsMoment = uptimeAsUnix(); + const uptimeAsMoment = dayjs.unix(currentTime.unix() - process.uptime()); const hoursDiff = currentTime.diff(uptimeAsMoment, 'hour'); const daysDiff = currentTime.diff(uptimeAsMoment, 'day'); From 6a67423fe3df5358d4ef0000fce7723e60699248 Mon Sep 17 00:00:00 2001 From: Alex Renoki Date: Thu, 4 Mar 2021 19:38:30 +0200 Subject: [PATCH 08/13] Conditionally initialize & run the API --- src/app.ts | 18 ++++++++++-------- src/classes/HttpManager.ts | 11 +++-------- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/src/app.ts b/src/app.ts index fbde4ff2f..959a42697 100644 --- a/src/app.ts +++ b/src/app.ts @@ -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'; process.env.BOT_VERSION = BOT_VERSION as string; @@ -52,9 +53,6 @@ function _getPricer(): Pricer { } const botManager = new BotManager(_getPricer()); -import HttpManager from './classes/HttpManager'; -const httpManager = new HttpManager(options); - import ON_DEATH from 'death'; import * as inspect from 'util'; import { Webhook } from './lib/DiscordWebhook/interfaces'; @@ -134,9 +132,13 @@ void botManager.start(options).asCallback(err => { throw err; } - void httpManager.start().asCallback(err => { - if (err) { - throw err; - } - }); + if (options.enableHttpApi) { + const httpManager = new HttpManager(options); + + void httpManager.start().asCallback(err => { + if (err) { + throw err; + } + }); + } }); diff --git a/src/classes/HttpManager.ts b/src/classes/HttpManager.ts index 61d3bf980..3a825f6bf 100644 --- a/src/classes/HttpManager.ts +++ b/src/classes/HttpManager.ts @@ -45,15 +45,10 @@ export default class HttpManager { */ start(): Promise { return new Promise(resolve => { - if (this.options.enableHttpApi) { - this.server = this.app.listen(this.options.httpApiPort, () => { - resolve(); - log.debug(`HTTP Server started: http://127.0.0.1:${this.options.httpApiPort}`); - }); - } else { - log.debug('HTTP Server is not enabled. Skipping initialization...'); + this.server = this.app.listen(this.options.httpApiPort, () => { resolve(); - } + log.debug(`HTTP Server started: http://127.0.0.1:${this.options.httpApiPort}`); + }); }); } } From 81504bd6adfc765a85768f722d69f5191d1fec52 Mon Sep 17 00:00:00 2001 From: Alex Renoki Date: Thu, 4 Mar 2021 19:45:54 +0200 Subject: [PATCH 09/13] Removed server member. --- src/classes/HttpManager.ts | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/classes/HttpManager.ts b/src/classes/HttpManager.ts index 3a825f6bf..673787ba6 100644 --- a/src/classes/HttpManager.ts +++ b/src/classes/HttpManager.ts @@ -14,11 +14,6 @@ export default class HttpManager { */ protected app: express.Application; - /** - * The Express.js server app. - */ - public server; - /** * Initialize the HTTP manager. * @@ -45,7 +40,7 @@ export default class HttpManager { */ start(): Promise { return new Promise(resolve => { - this.server = this.app.listen(this.options.httpApiPort, () => { + this.app.listen(this.options.httpApiPort, () => { resolve(); log.debug(`HTTP Server started: http://127.0.0.1:${this.options.httpApiPort}`); }); From 1251430ca679fd7a3f2719563a36355a0c443b81 Mon Sep 17 00:00:00 2001 From: Alex Renoki Date: Thu, 4 Mar 2021 19:46:31 +0200 Subject: [PATCH 10/13] Setting default port to 3001 --- template.ecosystem.json | 2 +- template.env | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/template.ecosystem.json b/template.ecosystem.json index e32be5467..0576cee14 100644 --- a/template.ecosystem.json +++ b/template.ecosystem.json @@ -42,7 +42,7 @@ "DEBUG_FILE": true, "ENABLE_HTTP_API": false, - "HTTP_API_PORT": 80 + "HTTP_API_PORT": 3001 } } ] diff --git a/template.env b/template.env index 30b084356..446adf021 100644 --- a/template.env +++ b/template.env @@ -29,4 +29,4 @@ DEBUG=true DEBUG_FILE=true ENABLE_HTTP_API=false -HTTP_API_PORT=80 +HTTP_API_PORT=3001 From d2e09a39bb7a0fc8b89fc64ccadf541e49e08f23 Mon Sep 17 00:00:00 2001 From: Alex Renoki Date: Thu, 4 Mar 2021 19:53:41 +0200 Subject: [PATCH 11/13] Setting default 3001 on the options --- src/classes/Options.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/classes/Options.ts b/src/classes/Options.ts index f6d050d6d..f2807b601 100644 --- a/src/classes/Options.ts +++ b/src/classes/Options.ts @@ -1945,7 +1945,7 @@ export function loadOptions(options?: Options): Options { debugFile: getOption('debugFile', true, jsonParseBoolean, incomingOptions), enableHttpApi: getOption('enableHttpApi', false, jsonParseBoolean, incomingOptions), - httpApiPort: getOption('httpApiPort', 80, jsonParseNumber, incomingOptions) + httpApiPort: getOption('httpApiPort', 3001, jsonParseNumber, incomingOptions) }; if (!envOptions.steamAccountName) { From 94d80486cc292e02d605c35b5482248e90863e59 Mon Sep 17 00:00:00 2001 From: Alex Renoki Date: Thu, 4 Mar 2021 19:53:52 +0200 Subject: [PATCH 12/13] Added info when starting the HTTP server. --- src/classes/HttpManager.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/classes/HttpManager.ts b/src/classes/HttpManager.ts index 673787ba6..e7f306d89 100644 --- a/src/classes/HttpManager.ts +++ b/src/classes/HttpManager.ts @@ -43,6 +43,10 @@ export default class HttpManager { this.app.listen(this.options.httpApiPort, () => { resolve(); log.debug(`HTTP Server started: http://127.0.0.1:${this.options.httpApiPort}`); + log.info(`This is NOT a HTTP API used to handle data within the bot. + It is solely for managing the bot programatically by providing healthchecks & uptime details.`); + log.info(`Please use the TF2Bot GUI v3+ as the main API source.`); + log.info(`https://github.com/TF2Autobot/tf2autobot-gui`); }); }); } From 8b84c9825950f1b1f0850cc3a2795fb11071ea42 Mon Sep 17 00:00:00 2001 From: Alex Renoki Date: Thu, 4 Mar 2021 19:54:25 +0200 Subject: [PATCH 13/13] Resolve after log. --- src/classes/HttpManager.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/classes/HttpManager.ts b/src/classes/HttpManager.ts index e7f306d89..7cb2d3f8d 100644 --- a/src/classes/HttpManager.ts +++ b/src/classes/HttpManager.ts @@ -41,12 +41,12 @@ export default class HttpManager { start(): Promise { return new Promise(resolve => { this.app.listen(this.options.httpApiPort, () => { - resolve(); log.debug(`HTTP Server started: http://127.0.0.1:${this.options.httpApiPort}`); log.info(`This is NOT a HTTP API used to handle data within the bot. - It is solely for managing the bot programatically by providing healthchecks & uptime details.`); + It is solely for managing the bot programatically by providing healthchecks & uptime details.`); log.info(`Please use the TF2Bot GUI v3+ as the main API source.`); log.info(`https://github.com/TF2Autobot/tf2autobot-gui`); + resolve(); }); }); }