badmfck-api-server 1.8.3 → 1.8.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -36,6 +36,7 @@ export interface APIServiceOptions {
36
36
  export declare function getDefaultOptions(): APIServiceOptions;
37
37
  export declare const REQ_CREATE_NET_LOG: Req<void, APIServiceNetworkLogItem>;
38
38
  export declare const REQ_HTTP_LOG: Req<void, APIServiceNetworkLogItem[]>;
39
+ export declare const REQ_HTTP_REQUESTS_COUNT: Req<void, number>;
39
40
  export declare function Initializer(services: IBaseService[]): Promise<void>;
40
41
  export declare class APIService extends BaseService {
41
42
  private static nextLogID;
@@ -43,6 +44,8 @@ export declare class APIService extends BaseService {
43
44
  private options;
44
45
  private monitor?;
45
46
  private monitorIndexFile?;
47
+ private started;
48
+ private requestsCount;
46
49
  netLog: APIServiceNetworkLogItem[];
47
50
  constructor(options?: APIServiceOptions | null);
48
51
  init(): Promise<void>;
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.APIService = exports.Initializer = exports.REQ_HTTP_LOG = exports.REQ_CREATE_NET_LOG = exports.getDefaultOptions = void 0;
6
+ exports.APIService = exports.Initializer = exports.REQ_HTTP_REQUESTS_COUNT = exports.REQ_HTTP_LOG = exports.REQ_CREATE_NET_LOG = exports.getDefaultOptions = void 0;
7
7
  const express_1 = __importDefault(require("express"));
8
8
  const BaseService_1 = require("./BaseService");
9
9
  const cors_1 = __importDefault(require("cors"));
@@ -17,6 +17,8 @@ const crypto_1 = __importDefault(require("crypto"));
17
17
  const fs_1 = __importDefault(require("fs"));
18
18
  const express_fileupload_1 = __importDefault(require("express-fileupload"));
19
19
  const os_1 = __importDefault(require("os"));
20
+ const Liveness_1 = require("./routes/Liveness");
21
+ const Readiness_1 = require("./routes/Readiness");
20
22
  function getDefaultOptions() {
21
23
  return {
22
24
  port: 8091,
@@ -34,15 +36,19 @@ function getDefaultOptions() {
34
36
  },
35
37
  isProductionEnvironment: false,
36
38
  fileLimit: 50 * 1024 * 1024,
37
- fileTempDir: path_1.default.resolve(os_1.default.tmpdir(), "fileTempDir"),
39
+ fileTempDir: path_1.default.resolve(os_1.default.tmpdir(), "fileTempDir")
38
40
  };
39
41
  }
40
42
  exports.getDefaultOptions = getDefaultOptions;
41
43
  exports.REQ_CREATE_NET_LOG = new badmfck_signal_1.Req(undefined, "REQ_CREATE_NET_LOG");
42
44
  exports.REQ_HTTP_LOG = new badmfck_signal_1.Req(undefined, "REQ_HTTP_LOG");
45
+ exports.REQ_HTTP_REQUESTS_COUNT = new badmfck_signal_1.Req(undefined, "REQ_HTTP_REQUESTS_COUNT");
46
+ const activeServices = [];
47
+ const entryPoints = [];
43
48
  async function Initializer(services) {
44
49
  for (let i of services) {
45
50
  await i.init();
51
+ activeServices.push(i.getName());
46
52
  }
47
53
  for (let i of services) {
48
54
  i.applicationReady();
@@ -51,10 +57,12 @@ async function Initializer(services) {
51
57
  exports.Initializer = Initializer;
52
58
  class APIService extends BaseService_1.BaseService {
53
59
  static nextLogID = 0;
54
- version = "1.8.3";
60
+ version = "1.8.5";
55
61
  options;
56
62
  monitor;
57
63
  monitorIndexFile;
64
+ started = new Date();
65
+ requestsCount = 0;
58
66
  netLog = [];
59
67
  constructor(options) {
60
68
  super('HTTP Service');
@@ -74,6 +82,8 @@ class APIService extends BaseService_1.BaseService {
74
82
  console.warn(i.login + " -> /sm-" + hash);
75
83
  }
76
84
  }
85
+ this.options.endpoints.push(new Liveness_1.Liveness(this.started), new Readiness_1.Readiness(this.started));
86
+ exports.REQ_HTTP_REQUESTS_COUNT.listener = async () => this.requestsCount;
77
87
  }
78
88
  async init() {
79
89
  exports.REQ_HTTP_LOG.listener = async (ignore) => this.netLog;
@@ -108,7 +118,6 @@ class APIService extends BaseService_1.BaseService {
108
118
  useTempFiles: true,
109
119
  uriDecodeFileNames: true,
110
120
  tempFileDir: this.options.fileTempDir,
111
- safeFileNames: true,
112
121
  abortOnLimit: true
113
122
  }));
114
123
  app.use(async (err, req, resp, next) => {
@@ -150,6 +159,7 @@ class APIService extends BaseService_1.BaseService {
150
159
  let ep = BaseEndpoint_1.BaseEndpoint.getEntryPoint() + j.endpoint;
151
160
  ep = ep.replaceAll("//", "/");
152
161
  app.all(ep, async (req, res) => {
162
+ this.requestsCount++;
153
163
  const tme = +new Date();
154
164
  let log = null;
155
165
  if (!i.ignoreHttpLogging) {
@@ -14,6 +14,7 @@ export interface IEndpointHandler {
14
14
  allowInterceptorError?: boolean;
15
15
  }
16
16
  export declare class BaseEndpoint implements IBaseEndpoint {
17
+ private inializedEndpointNames;
17
18
  endpoints?: IEndpointHandler[];
18
19
  ignoreHttpLogging: boolean;
19
20
  private static entrypoint;
@@ -23,6 +24,7 @@ export declare class BaseEndpoint implements IBaseEndpoint {
23
24
  constructor(endpoint: string);
24
25
  registerEndpoints(endpoints: IEndpointHandler[]): void;
25
26
  init(): Promise<void>;
27
+ getInitalizedEndpointNames(): String[];
26
28
  precheck(req: HTTPRequestVO): Promise<TransferPacketVO<any> | null>;
27
29
  execute(req: HTTPRequestVO): Promise<TransferPacketVO<any>>;
28
30
  }
@@ -7,6 +7,7 @@ exports.BaseEndpoint = void 0;
7
7
  const LogService_1 = require("./LogService");
8
8
  const DefaultErrors_1 = __importDefault(require("./structures/DefaultErrors"));
9
9
  class BaseEndpoint {
10
+ inializedEndpointNames = [];
10
11
  endpoints;
11
12
  ignoreHttpLogging = false;
12
13
  static entrypoint = "/";
@@ -52,10 +53,15 @@ class BaseEndpoint {
52
53
  (0, LogService_1.logCrit)("${BaseEndpoint.js}", "No endpoints registered for " + this.endpoint);
53
54
  return;
54
55
  }
55
- for (let i of this.endpoints)
56
+ for (let i of this.endpoints) {
56
57
  (0, LogService_1.logInfo)("${BaseEndpoint.js}", "endpoint: " + i.endpoint + " initalized, ignoreInterceptor: " + (i.ignoreInterceptor ?? "false"));
58
+ this.inializedEndpointNames.push(i.endpoint + " ignore interceptor: " + (i.ignoreInterceptor ?? "false"));
59
+ }
57
60
  }
58
61
  ;
62
+ getInitalizedEndpointNames() {
63
+ return this.inializedEndpointNames;
64
+ }
59
65
  async precheck(req) { return null; }
60
66
  async execute(req) {
61
67
  if (this.endpoints && this.endpoints.length > 0) {
@@ -1,10 +1,12 @@
1
1
  export interface IBaseService {
2
2
  init: () => Promise<void>;
3
3
  applicationReady: () => void;
4
+ getName: () => string;
4
5
  }
5
6
  export declare class BaseService implements IBaseService {
6
7
  protected name: string;
7
8
  constructor(name: string);
8
9
  init(): Promise<void>;
10
+ getName(): string;
9
11
  applicationReady(): void;
10
12
  }
@@ -9,6 +9,9 @@ class BaseService {
9
9
  async init() {
10
10
  console.log("Service: " + this.name + " initialized");
11
11
  }
12
+ getName() {
13
+ return this.name;
14
+ }
12
15
  applicationReady() { }
13
16
  }
14
17
  exports.BaseService = BaseService;
@@ -11,6 +11,10 @@ export interface MysqlServiceOptions {
11
11
  password: string;
12
12
  port: number;
13
13
  database: string;
14
+ migrations: {
15
+ dir: string;
16
+ callback: () => void;
17
+ } | null;
14
18
  }
15
19
  export interface MysqlResult {
16
20
  error?: MysqlError | null;
@@ -4,6 +4,12 @@ export interface IValidatorOptions {
4
4
  type?: "string" | "boolean" | "number";
5
5
  regex?: RegExp;
6
6
  }
7
+ export interface IValidaotrType {
8
+ name: string;
9
+ type: "string" | "number" | "boolean" | "date" | "array" | "object" | "email" | "phone";
10
+ optional?: boolean;
11
+ arrayType?: IValidaotrType;
12
+ }
7
13
  export declare enum ValidationReport {
8
14
  OK = 1,
9
15
  VALUE_IS_NULL = 2,
@@ -0,0 +1,7 @@
1
+ import { BaseEndpoint } from "../BaseEndpoint";
2
+ import { HTTPRequestVO, TransferPacketVO } from "../structures/Interfaces";
3
+ export declare class Liveness extends BaseEndpoint {
4
+ started: Date;
5
+ constructor(stated: Date);
6
+ checkLiveness(req: HTTPRequestVO): Promise<TransferPacketVO<any>>;
7
+ }
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Liveness = void 0;
4
+ const APIService_1 = require("../APIService");
5
+ const BaseEndpoint_1 = require("../BaseEndpoint");
6
+ class Liveness extends BaseEndpoint_1.BaseEndpoint {
7
+ started;
8
+ constructor(stated) {
9
+ super("liveness");
10
+ this.started = stated;
11
+ this.registerEndpoints([{ endpoint: "", handler: this.checkLiveness, ignoreInterceptor: true }]);
12
+ }
13
+ async checkLiveness(req) {
14
+ const request = APIService_1.REQ_HTTP_REQUESTS_COUNT.request();
15
+ const data = { data: { liveness: true, wakeTime: (+new Date()) - (+this.started), request: request } };
16
+ let cb = null;
17
+ const p = new Promise((resolve, reject) => { cb = resolve; });
18
+ setTimeout(() => {
19
+ if (cb)
20
+ cb(data);
21
+ }, 1000 * 10);
22
+ return p;
23
+ }
24
+ }
25
+ exports.Liveness = Liveness;
@@ -0,0 +1,13 @@
1
+ import { BaseEndpoint } from "../BaseEndpoint";
2
+ import { HTTPRequestVO } from "../structures/Interfaces";
3
+ export declare class Readiness extends BaseEndpoint {
4
+ started: Date;
5
+ constructor(stated: Date);
6
+ checkReadiness(req: HTTPRequestVO): Promise<{
7
+ data: {
8
+ liveness: boolean;
9
+ wakeTime: number;
10
+ request: Promise<number>;
11
+ };
12
+ }>;
13
+ }
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Readiness = void 0;
4
+ const APIService_1 = require("../APIService");
5
+ const BaseEndpoint_1 = require("../BaseEndpoint");
6
+ class Readiness extends BaseEndpoint_1.BaseEndpoint {
7
+ started;
8
+ constructor(stated) {
9
+ super("readiness");
10
+ this.started = stated;
11
+ this.registerEndpoints([{ endpoint: "", handler: this.checkReadiness, ignoreInterceptor: true }]);
12
+ }
13
+ async checkReadiness(req) {
14
+ const request = APIService_1.REQ_HTTP_REQUESTS_COUNT.request();
15
+ return { data: { liveness: true, wakeTime: (+new Date()) - (+this.started), request: request } };
16
+ }
17
+ }
18
+ exports.Readiness = Readiness;
@@ -1,4 +1,4 @@
1
- import { FileArray, UploadedFile } from "express-fileupload";
1
+ import { FileArray } from "express-fileupload";
2
2
  export interface TransferPacketVO<T> {
3
3
  data?: T | null;
4
4
  error?: IError | null;
@@ -26,7 +26,7 @@ export interface HTTPRequestVO<T = any> {
26
26
  endpoint: string;
27
27
  interceptorResult?: TransferPacketVO<any>;
28
28
  precheck?: TransferPacketVO<any> | null;
29
- files: FileArray | UploadedFile | null | undefined;
29
+ files: FileArray | null | undefined;
30
30
  }
31
31
  export interface IError {
32
32
  code: number;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "badmfck-api-server",
3
- "version": "1.8.3",
3
+ "version": "1.8.5",
4
4
  "description": "Simple API http server based on express",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",