forked from samchon/backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
77 lines (65 loc) · 2.06 KB
/
server.ts
File metadata and controls
77 lines (65 loc) · 2.06 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
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 { Backend } from "../Backend";
import { Configuration } from "../Configuration";
import { SGlobal } from "../SGlobal";
import { ErrorUtil } from "../utils/ErrorUtil";
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])
SGlobal.setMode(process.argv[2].toUpperCase() as typeof SGlobal.mode);
// CONNECT TO THE DB FIRST
await orm.createConnection(Configuration.DB_CONFIG);
// BACKEND SEVER LATER
const backend: Backend = new Backend();
await backend.open(Configuration.API_PORT);
//----
// POST-PROCESSES
//----
// UNEXPECTED ERRORS
global.process.on("uncaughtException", handle_error);
global.process.on("unhandledRejection", handle_error);
}
main();