-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
109 lines (98 loc) · 2.82 KB
/
Copy pathindex.js
File metadata and controls
109 lines (98 loc) · 2.82 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
* @module application
* @license MIT
* Module class application to make a new instance.
* @author Cris-Mur
*/
const requireUnCached = require('#Utils/requireUnCached');
/**
* @class BasicAPI
* @description This Class encapsulates Express & HTTP server implementation.
* @property {ExpressController} #application - The application instance.
* @property {Function} build - Function to build the application.
* @property {Object} #network - Network configuration for the application.
*/
class BasicAPI {
#application = undefined;
build;
#network;
constructor() {
this.loadLogger();
this.loadEnvironment();
this.build = require('./build');
this.#network = require('./network');
this.startup();
}
/**
* @function loadEnvironment
* @description Loads the environment variables from the .env file.
*/
loadEnvironment() {
try {
process.loadEnvFile();
} catch (errorOnLoadEnv) {
try {
process.loadEnvFile('./env.example');
console.warn('[####][WARNING][####][RUN APPLICATION WITH ENVIRONMENT EXAMPLE]');
} catch (errorOnLoadEnvExample) {
console.warn('[####][WARNING][####][RUN APPLICATION WITHOUT ENVIRONMENT FILE]');
}
}
}
/**
* @function loadLogger
* @description Loads the custom logger.
*/
loadLogger() {
require('#Utils/logger');
}
/**
* @function initNetwork
* @description Initializes the network interface.
*/
initNetwork() {
this.#network.http.initTCPInterface(this.#network.port.getPort());
}
/**
* @function initApplication
* @description Builds a new application instance and sets the request listener.
*/
initApplication() {
this.initNetwork();
this.#application = this.build();
this.#network.http.
setRequestListener(
this.#application.getApplication()
);
console.debug("[BasicAPI][initApplication]",
this.#application.getApplication().name,
"[was mounted]"
);
}
/**
* @function getApplication
* @description Returns the application instance.
* @returns {ExpressController | express} The application instance.
*/
getApplication() {
return this.#application;
}
/**
* @function startup
* @description Loads environment, logger, and initializes the application.
*/
startup() {
this.initApplication();
}
/**
* @function reset
* @description Resets the network configuration by requiring it uncached.
*/
reset() {
this.#network = requireUnCached('./network', __dirname);
}
}
/**
* @exports {@class<BasicAPI>}
*/
module.exports = new BasicAPI;