Repository navigation
Expand file tree
/
Copy pathserver.ts
More file actions
86 lines (73 loc) · 2.34 KB
/
Copy pathserver.ts
File metadata and controls
86 lines (73 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
const EXTENSION = __filename.substr(-2);
if (EXTENSION === "js")
require("source-map-support/register");
import * as fs from "fs";
import * as orm from "typeorm";
import { Singleton } from "tstl/thread/Singleton";
import { randint } from "tstl/algorithm/random";
import { PaymentBackend } from "../PaymentBackend";
import { PaymentConfiguration } from "../PaymentConfiguration";
import { PaymentGlobal } from "../PaymentGlobal";
import { ErrorUtil } from "../utils/ErrorUtil";
import { Scheduler } from "../schedulers/Scheduler";
const directory = new Singleton(async () =>
{
await mkdir(`${__dirname}/../../assets`);
await mkdir(`${__dirname}/../../assets/logs`);
await mkdir(`${__dirname}/../../assets/logs/errors`);
});
function cipher(val: number): string
{
if (val < 10)
return "0" + val;
else
return String(val);
}
async function mkdir(path: string): Promise<void>
{
try
{
await fs.promises.mkdir(path);
}
catch {}
}
async function handle_error(exp: any): Promise<void>
{
try
{
const date: Date = new Date();
const fileName: string = `${date.getFullYear()}${cipher(date.getMonth()+1)}${cipher(date.getDate())}${cipher(date.getHours())}${cipher(date.getMinutes())}${cipher(date.getSeconds())}.${randint(0, Number.MAX_SAFE_INTEGER)}`;
const content: string = JSON.stringify(ErrorUtil.toJSON(exp), null, 4);
await directory.get();
await fs.promises.writeFile(`${__dirname}/../../assets/logs/errors/${fileName}.log`, content, "utf8");
}
catch {}
}
async function main(): Promise<void>
{
//----
// OPEN SERVER
//----
// CONFIGURE MODE
if (process.argv[2])
PaymentGlobal.setMode(process.argv[2].toUpperCase() as "LOCAL");
// CONNECT TO THE DB FIRST
await orm.createConnection(PaymentConfiguration.db_config());
// BACKEND SEVER LATER
const backend: PaymentBackend = new PaymentBackend();
await backend.open();
//----
// POST-PROCESSES
//----
// UNEXPECTED ERRORS
global.process.on("uncaughtException", handle_error);
global.process.on("unhandledRejection", handle_error);
// SCHEDULER ONLY WHEN MASTER
if (PaymentGlobal.mode !== "REAL" || process.argv[3] === "master")
await Scheduler.repeat();
}
main().catch(exp =>
{
console.log(exp);
process.exit(-1);
});