@seip/blue-bird 0.4.5 → 0.4.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.env_example +26 -25
- package/AGENTS.md +199 -199
- package/README.md +79 -79
- package/backend/index.js +13 -13
- package/backend/routes/frontend.js +41 -41
- package/backend/routes/seo.js +39 -39
- package/core/app.js +328 -325
- package/core/auth.js +114 -114
- package/core/cache.js +44 -44
- package/core/cli/component.js +42 -42
- package/core/cli/init.js +119 -118
- package/core/cli/react.js +435 -435
- package/core/cli/route.js +42 -42
- package/core/config.js +48 -47
- package/core/debug.js +248 -248
- package/core/logger.js +100 -100
- package/core/middleware.js +27 -27
- package/core/router.js +333 -333
- package/core/seo.js +95 -100
- package/core/template.js +472 -462
- package/core/upload.js +76 -76
- package/core/validate.js +380 -380
- package/frontend/index.html +26 -26
- package/frontend/landing.html +69 -69
- package/frontend/resources/css/tailwind.css +17 -17
- package/frontend/resources/js/App.jsx +70 -70
- package/frontend/resources/js/Main.jsx +18 -18
- package/frontend/resources/js/blue-bird/components/Button.jsx +67 -67
- package/frontend/resources/js/blue-bird/components/Card.jsx +18 -18
- package/frontend/resources/js/blue-bird/components/DataTable.jsx +126 -126
- package/frontend/resources/js/blue-bird/components/Input.jsx +21 -21
- package/frontend/resources/js/blue-bird/components/Label.jsx +12 -12
- package/frontend/resources/js/blue-bird/components/LanguageButton.jsx +23 -23
- package/frontend/resources/js/blue-bird/components/Link.jsx +15 -15
- package/frontend/resources/js/blue-bird/components/Modal.jsx +27 -27
- package/frontend/resources/js/blue-bird/components/Skeleton.jsx +44 -44
- package/frontend/resources/js/blue-bird/components/Translate.jsx +12 -12
- package/frontend/resources/js/blue-bird/components/Typography.jsx +69 -69
- package/frontend/resources/js/blue-bird/contexts/LanguageContext.jsx +41 -41
- package/frontend/resources/js/blue-bird/contexts/SPAContext.jsx +239 -237
- package/frontend/resources/js/blue-bird/contexts/SnackbarContext.jsx +38 -38
- package/frontend/resources/js/blue-bird/contexts/ThemeContext.jsx +49 -49
- package/frontend/resources/js/blue-bird/locales/en.json +47 -47
- package/frontend/resources/js/blue-bird/locales/es.json +47 -47
- package/frontend/resources/js/components/Header.jsx +55 -55
- package/frontend/resources/js/pages/About.jsx +31 -31
- package/frontend/resources/js/pages/Home.jsx +82 -82
- package/package.json +57 -57
- package/vite.config.js +22 -22
- package/frontend/public/robots.txt +0 -0
- package/frontend/public/sitemap.xml +0 -0
package/core/app.js
CHANGED
|
@@ -1,325 +1,328 @@
|
|
|
1
|
-
import express from "express";
|
|
2
|
-
import cors from "cors";
|
|
3
|
-
import path from "path";
|
|
4
|
-
import chalk from "chalk";
|
|
5
|
-
import helmet from "helmet";
|
|
6
|
-
import cookieParser from "cookie-parser";
|
|
7
|
-
import rateLimit from "express-rate-limit";
|
|
8
|
-
import compression from "compression";
|
|
9
|
-
import Config from "./config.js";
|
|
10
|
-
import Logger from "./logger.js";
|
|
11
|
-
import Debug from "./debug.js";
|
|
12
|
-
|
|
13
|
-
const __dirname = Config.dirname();
|
|
14
|
-
const props = Config.props();
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Main Application class to manage Express server, routes, and middlewares.
|
|
18
|
-
*/
|
|
19
|
-
class App {
|
|
20
|
-
/**
|
|
21
|
-
* Initializes the App instance with the provided options.
|
|
22
|
-
* @param {Object} [options] - Configuration options for the application.
|
|
23
|
-
* @param {Array<{path: string, router: import('express').Router}>} [options.routes=[]] - Array of route objects containing path and router components.
|
|
24
|
-
* @param {Object} [options.cors={}] - CORS configuration options.
|
|
25
|
-
* @param {Array<Function>} [options.middlewares=[]] - Array of middleware functions to be applied.
|
|
26
|
-
* @param {number|string} [options.port=3000] - Server port.
|
|
27
|
-
* @param {string} [options.host="http://localhost"] - Server host URL.
|
|
28
|
-
* @param {boolean} [options.logger=true] - Whether to enable the request logger.
|
|
29
|
-
* @param {boolean} [options.notFound=true] - Whether to enable the default 404 handler.
|
|
30
|
-
* @param {boolean} [options.json=true] - Whether to enable JSON body parsing.
|
|
31
|
-
* @param {boolean} [options.urlencoded=true] - Whether to enable URL-encoded body parsing.
|
|
32
|
-
* @param {Object} [options.static={path: null, options: {}}] - Static file configuration.
|
|
33
|
-
* @param {boolean} [options.cookieParser=true] - Whether to enable cookie parsing.
|
|
34
|
-
* @param {boolean|Object} [options.rateLimit=false] - Enable global rate limiting.
|
|
35
|
-
* @param {boolean|Object} [options.swagger=false] - Enable swagger
|
|
36
|
-
* @param {boolean} [options.compression=true] - Enable Gzip compression.
|
|
37
|
-
* @example
|
|
38
|
-
* const app = new App({
|
|
39
|
-
* routes: [],
|
|
40
|
-
* cors: {}, // { origin: "https://domain:port" }
|
|
41
|
-
* middlewares: [],
|
|
42
|
-
* port: 3000,
|
|
43
|
-
* host: "http://localhost",
|
|
44
|
-
* logger: true,
|
|
45
|
-
* notFound: true,
|
|
46
|
-
* json: true,
|
|
47
|
-
* urlencoded: true,
|
|
48
|
-
* static: {
|
|
49
|
-
* path: "public",
|
|
50
|
-
* options: {}
|
|
51
|
-
* },
|
|
52
|
-
* cookieParser: true,
|
|
53
|
-
* rateLimit: {
|
|
54
|
-
* windowMs: 10 * 60 * 1000,
|
|
55
|
-
* max: 50
|
|
56
|
-
* },
|
|
57
|
-
* swagger:{
|
|
58
|
-
* info: {
|
|
59
|
-
* title: "Blue Bird API",
|
|
60
|
-
* version: "1.0.0",
|
|
61
|
-
* description: "Blue Bird Framework API Documentation"
|
|
62
|
-
* },
|
|
63
|
-
* url : "http://localhost:8000"
|
|
64
|
-
* },
|
|
65
|
-
* compression: true
|
|
66
|
-
* });
|
|
67
|
-
*/
|
|
68
|
-
constructor(options = {}) {
|
|
69
|
-
this.app = express();
|
|
70
|
-
this.routes = options.routes || [];
|
|
71
|
-
this.cors = options.cors || {};
|
|
72
|
-
this.middlewares = options.middlewares || [];
|
|
73
|
-
this.port = options.port || props.port;
|
|
74
|
-
this.host = options.host || props.host;
|
|
75
|
-
this.
|
|
76
|
-
this.
|
|
77
|
-
this.
|
|
78
|
-
this.
|
|
79
|
-
this.
|
|
80
|
-
this.
|
|
81
|
-
this.
|
|
82
|
-
this.
|
|
83
|
-
this.
|
|
84
|
-
this.
|
|
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
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
*
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
if (this.
|
|
119
|
-
if (this.
|
|
120
|
-
if (this.
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
this.
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
this.
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
const
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
const
|
|
217
|
-
const
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
*
|
|
232
|
-
*
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
const status = err.status || 500;
|
|
238
|
-
const message = err.message || "Internal Server Error";
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
if (props.debug) {
|
|
243
|
-
return res.status(status).json({
|
|
244
|
-
success: false,
|
|
245
|
-
error: true,
|
|
246
|
-
message: message,
|
|
247
|
-
stack: err.stack,
|
|
248
|
-
});
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
return res.status(status).json({
|
|
252
|
-
success: false,
|
|
253
|
-
error: true,
|
|
254
|
-
message: status === 500 ? "Internal Server Error" : message,
|
|
255
|
-
});
|
|
256
|
-
});
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
/**
|
|
260
|
-
* Iterates through the stored routes and attaches them to the Express application instance.
|
|
261
|
-
* @private
|
|
262
|
-
*/
|
|
263
|
-
_dispatchRoutes() {
|
|
264
|
-
if (props.debug) {
|
|
265
|
-
const debug = new Debug();
|
|
266
|
-
const debugRouter = debug.getRouter();
|
|
267
|
-
this.app.use(debugRouter.path, debugRouter.router);
|
|
268
|
-
}
|
|
269
|
-
this.routes.forEach((route) => {
|
|
270
|
-
this.app.use(route.path, route.router);
|
|
271
|
-
});
|
|
272
|
-
}
|
|
273
|
-
/**
|
|
274
|
-
* Default 404 handler for unmatched routes.
|
|
275
|
-
* Returns a JSON response with a "Not Found" message.
|
|
276
|
-
* @private
|
|
277
|
-
*/
|
|
278
|
-
_notFoundDefault() {
|
|
279
|
-
this.app.use((req, res) => {
|
|
280
|
-
return res.status(404).json({ message: "Not Found" });
|
|
281
|
-
});
|
|
282
|
-
}
|
|
283
|
-
/**
|
|
284
|
-
* Starts the HTTP server and begins listening for incoming connections.
|
|
285
|
-
* Waits for dispatch to complete before starting.
|
|
286
|
-
*/
|
|
287
|
-
run() {
|
|
288
|
-
this._ready
|
|
289
|
-
.then(() => {
|
|
290
|
-
this.app.listen(this.port, () => {
|
|
291
|
-
console.log(
|
|
292
|
-
chalk.bold.blue("Blue Bird Server Online\n") +
|
|
293
|
-
chalk.bold.cyan("
|
|
294
|
-
chalk.green(`${this.
|
|
295
|
-
"\n" +
|
|
296
|
-
chalk.
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
*
|
|
314
|
-
*
|
|
315
|
-
*
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
|
|
1
|
+
import express from "express";
|
|
2
|
+
import cors from "cors";
|
|
3
|
+
import path from "path";
|
|
4
|
+
import chalk from "chalk";
|
|
5
|
+
import helmet from "helmet";
|
|
6
|
+
import cookieParser from "cookie-parser";
|
|
7
|
+
import rateLimit from "express-rate-limit";
|
|
8
|
+
import compression from "compression";
|
|
9
|
+
import Config from "./config.js";
|
|
10
|
+
import Logger from "./logger.js";
|
|
11
|
+
import Debug from "./debug.js";
|
|
12
|
+
|
|
13
|
+
const __dirname = Config.dirname();
|
|
14
|
+
const props = Config.props();
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Main Application class to manage Express server, routes, and middlewares.
|
|
18
|
+
*/
|
|
19
|
+
class App {
|
|
20
|
+
/**
|
|
21
|
+
* Initializes the App instance with the provided options.
|
|
22
|
+
* @param {Object} [options] - Configuration options for the application.
|
|
23
|
+
* @param {Array<{path: string, router: import('express').Router}>} [options.routes=[]] - Array of route objects containing path and router components.
|
|
24
|
+
* @param {Object} [options.cors={}] - CORS configuration options.
|
|
25
|
+
* @param {Array<Function>} [options.middlewares=[]] - Array of middleware functions to be applied.
|
|
26
|
+
* @param {number|string} [options.port=3000] - Server port.
|
|
27
|
+
* @param {string} [options.host="http://localhost"] - Server host URL.
|
|
28
|
+
* @param {boolean} [options.logger=true] - Whether to enable the request logger.
|
|
29
|
+
* @param {boolean} [options.notFound=true] - Whether to enable the default 404 handler.
|
|
30
|
+
* @param {boolean} [options.json=true] - Whether to enable JSON body parsing.
|
|
31
|
+
* @param {boolean} [options.urlencoded=true] - Whether to enable URL-encoded body parsing.
|
|
32
|
+
* @param {Object} [options.static={path: null, options: {}}] - Static file configuration.
|
|
33
|
+
* @param {boolean} [options.cookieParser=true] - Whether to enable cookie parsing.
|
|
34
|
+
* @param {boolean|Object} [options.rateLimit=false] - Enable global rate limiting.
|
|
35
|
+
* @param {boolean|Object} [options.swagger=false] - Enable swagger
|
|
36
|
+
* @param {boolean} [options.compression=true] - Enable Gzip compression.
|
|
37
|
+
* @example
|
|
38
|
+
* const app = new App({
|
|
39
|
+
* routes: [],
|
|
40
|
+
* cors: {}, // { origin: "https://domain:port" }
|
|
41
|
+
* middlewares: [],
|
|
42
|
+
* port: 3000,
|
|
43
|
+
* host: "http://localhost",
|
|
44
|
+
* logger: true,
|
|
45
|
+
* notFound: true,
|
|
46
|
+
* json: true,
|
|
47
|
+
* urlencoded: true,
|
|
48
|
+
* static: {
|
|
49
|
+
* path: "public",
|
|
50
|
+
* options: {}
|
|
51
|
+
* },
|
|
52
|
+
* cookieParser: true,
|
|
53
|
+
* rateLimit: {
|
|
54
|
+
* windowMs: 10 * 60 * 1000,
|
|
55
|
+
* max: 50
|
|
56
|
+
* },
|
|
57
|
+
* swagger:{
|
|
58
|
+
* info: {
|
|
59
|
+
* title: "Blue Bird API",
|
|
60
|
+
* version: "1.0.0",
|
|
61
|
+
* description: "Blue Bird Framework API Documentation"
|
|
62
|
+
* },
|
|
63
|
+
* url : "http://localhost:8000"
|
|
64
|
+
* },
|
|
65
|
+
* compression: true
|
|
66
|
+
* });
|
|
67
|
+
*/
|
|
68
|
+
constructor(options = {}) {
|
|
69
|
+
this.app = express();
|
|
70
|
+
this.routes = options.routes || [];
|
|
71
|
+
this.cors = options.cors || {};
|
|
72
|
+
this.middlewares = options.middlewares || [];
|
|
73
|
+
this.port = options.port || props.port;
|
|
74
|
+
this.host = options.host || props.host;
|
|
75
|
+
this.appUrl = options.appUrl || props.appUrl;
|
|
76
|
+
this.logger = options.logger ?? false;
|
|
77
|
+
this.notFound = options.notFound ?? true;
|
|
78
|
+
this.json = options.json ?? true;
|
|
79
|
+
this.urlencoded = options.urlencoded ?? true;
|
|
80
|
+
this.static = options.static || props.static;
|
|
81
|
+
this.cookieParser = options.cookieParser ?? true;
|
|
82
|
+
this.rateLimit = options.rateLimit ?? false;
|
|
83
|
+
this.swagger = options.swagger ?? false;
|
|
84
|
+
this.compression = options.compression ?? true;
|
|
85
|
+
this.loggerInstance = new Logger();
|
|
86
|
+
this._ready = this._dispatch();
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Registers a custom middleware or module in the Express application.
|
|
91
|
+
* @param {Function|import('express').Router} record - The middleware function or Express router to register.
|
|
92
|
+
* @example
|
|
93
|
+
* app.use((req, res, next) => {
|
|
94
|
+
* console.log("Middleware");
|
|
95
|
+
* next();
|
|
96
|
+
* });
|
|
97
|
+
*/
|
|
98
|
+
use(record) {
|
|
99
|
+
this.app.use(record);
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Sets a configuration value in the Express application.
|
|
103
|
+
* @param {string} key - The configuration key.
|
|
104
|
+
* @param {*} value - The value to set for the configuration key.
|
|
105
|
+
* @example
|
|
106
|
+
* app.set("port", 3000);
|
|
107
|
+
*/
|
|
108
|
+
set(key, value) {
|
|
109
|
+
this.app.set(key, value);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Bootstraps the application by configuring global middlewares and routes.
|
|
114
|
+
* Sets up JSON parsing, URL encoding, CORS, and custom middlewares.
|
|
115
|
+
* @private
|
|
116
|
+
*/
|
|
117
|
+
async _dispatch() {
|
|
118
|
+
if (this.compression) this.app.use(compression());
|
|
119
|
+
if (this.json) this.app.use(express.json());
|
|
120
|
+
if (this.urlencoded) this.app.use(express.urlencoded({ extended: true }));
|
|
121
|
+
if (this.cookieParser) this.app.use(cookieParser());
|
|
122
|
+
if (this.static.path)
|
|
123
|
+
this.app.use(
|
|
124
|
+
express.static(
|
|
125
|
+
path.join(__dirname, this.static.path),
|
|
126
|
+
this.static.options,
|
|
127
|
+
),
|
|
128
|
+
);
|
|
129
|
+
|
|
130
|
+
this.app.use(cors(this.cors));
|
|
131
|
+
if (this.rateLimit) {
|
|
132
|
+
if (!this.app.get("trust proxy")) {
|
|
133
|
+
this.app.set("trust proxy", 1);
|
|
134
|
+
}
|
|
135
|
+
const defaultRateLimit = {
|
|
136
|
+
windowMs: 15 * 60 * 1000,
|
|
137
|
+
max: 500,
|
|
138
|
+
standardHeaders: true,
|
|
139
|
+
legacyHeaders: false,
|
|
140
|
+
message: {
|
|
141
|
+
success: false,
|
|
142
|
+
message: "Too many requests, please try again later.",
|
|
143
|
+
},
|
|
144
|
+
};
|
|
145
|
+
const optionsRateLimiter = {
|
|
146
|
+
...defaultRateLimit,
|
|
147
|
+
...(typeof this.rateLimit === "object" ? this.rateLimit : {}),
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
if (props.debug) {
|
|
151
|
+
optionsRateLimiter.skip = (req) => req.path.startsWith("/debug");
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const limiter = rateLimit(optionsRateLimiter);
|
|
155
|
+
|
|
156
|
+
this.app.use(limiter);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
this.middlewares.forEach((middleware) => {
|
|
160
|
+
this.app.use(middleware);
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
if (this.logger) this._middlewareLogger();
|
|
164
|
+
|
|
165
|
+
this.app.use((req, res, next) => {
|
|
166
|
+
res.setHeader("X-Powered-By", "Blue Bird");
|
|
167
|
+
next();
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
if (props.debug) {
|
|
171
|
+
Debug.middlewareMetrics(this.app);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
if (this.swagger) {
|
|
175
|
+
const { default: Swagger } = await import("./swagger.js");
|
|
176
|
+
const defaultSwaggerOptions = {
|
|
177
|
+
info: {
|
|
178
|
+
title: "Blue Bird API",
|
|
179
|
+
version: "1.0.0",
|
|
180
|
+
description: "Blue Bird Framework API Documentation",
|
|
181
|
+
},
|
|
182
|
+
url: this.appUrl ? this.appUrl : `${this.host}:${this.port}`,
|
|
183
|
+
route: "/docs",
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
const swaggerOptions = {
|
|
187
|
+
...defaultSwaggerOptions,
|
|
188
|
+
...(typeof this.swagger === "object" ? this.swagger : {}),
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
Swagger.init(this.app, swaggerOptions);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
this._dispatchRoutes();
|
|
195
|
+
|
|
196
|
+
if (this.notFound) this._notFoundDefault();
|
|
197
|
+
|
|
198
|
+
this._errorHandler();
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Middleware that logs incoming HTTP requests to the console and to a log file.
|
|
203
|
+
* @private
|
|
204
|
+
*/
|
|
205
|
+
_middlewareLogger() {
|
|
206
|
+
this.app.use((req, res, next) => {
|
|
207
|
+
const method = req.method;
|
|
208
|
+
const url = req.url.replace(
|
|
209
|
+
/(password|token|authorization)=([^&]+)/gi,
|
|
210
|
+
"$1=***",
|
|
211
|
+
);
|
|
212
|
+
const params =
|
|
213
|
+
Object.keys(req.params).length > 0
|
|
214
|
+
? ` ${JSON.stringify(req.params)}`
|
|
215
|
+
: "";
|
|
216
|
+
const ip = req.ip;
|
|
217
|
+
const now = new Date().toISOString();
|
|
218
|
+
const time = `${now.split("T")[0]} ${now.split("T")[1].split(".")[0]}`;
|
|
219
|
+
let message = ` ${time} -${ip} -[${method}] ${url} ${params}`;
|
|
220
|
+
|
|
221
|
+
this.loggerInstance.info(message);
|
|
222
|
+
if (props.debug) {
|
|
223
|
+
message = `${chalk.bold.green(time)} - ${chalk.bold.cyan(ip)} -[${chalk.bold.red(method)}] ${chalk.bold.blue(url)} ${chalk.bold.yellow(params)}`;
|
|
224
|
+
console.log(message);
|
|
225
|
+
}
|
|
226
|
+
next();
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Global error handler for the application.
|
|
232
|
+
* Catches all errors and responds with a standardized JSON structure.
|
|
233
|
+
* @private
|
|
234
|
+
*/
|
|
235
|
+
_errorHandler() {
|
|
236
|
+
this.app.use((err, req, res, next) => {
|
|
237
|
+
const status = err.status || 500;
|
|
238
|
+
const message = err.message || "Internal Server Error";
|
|
239
|
+
|
|
240
|
+
this.loggerInstance.error(`[${status}] ${message} - ${err.stack}`);
|
|
241
|
+
|
|
242
|
+
if (props.debug) {
|
|
243
|
+
return res.status(status).json({
|
|
244
|
+
success: false,
|
|
245
|
+
error: true,
|
|
246
|
+
message: message,
|
|
247
|
+
stack: err.stack,
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
return res.status(status).json({
|
|
252
|
+
success: false,
|
|
253
|
+
error: true,
|
|
254
|
+
message: status === 500 ? "Internal Server Error" : message,
|
|
255
|
+
});
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* Iterates through the stored routes and attaches them to the Express application instance.
|
|
261
|
+
* @private
|
|
262
|
+
*/
|
|
263
|
+
_dispatchRoutes() {
|
|
264
|
+
if (props.debug) {
|
|
265
|
+
const debug = new Debug();
|
|
266
|
+
const debugRouter = debug.getRouter();
|
|
267
|
+
this.app.use(debugRouter.path, debugRouter.router);
|
|
268
|
+
}
|
|
269
|
+
this.routes.forEach((route) => {
|
|
270
|
+
this.app.use(route.path, route.router);
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* Default 404 handler for unmatched routes.
|
|
275
|
+
* Returns a JSON response with a "Not Found" message.
|
|
276
|
+
* @private
|
|
277
|
+
*/
|
|
278
|
+
_notFoundDefault() {
|
|
279
|
+
this.app.use((req, res) => {
|
|
280
|
+
return res.status(404).json({ message: "Not Found" });
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* Starts the HTTP server and begins listening for incoming connections.
|
|
285
|
+
* Waits for dispatch to complete before starting.
|
|
286
|
+
*/
|
|
287
|
+
run() {
|
|
288
|
+
this._ready
|
|
289
|
+
.then(() => {
|
|
290
|
+
this.app.listen(this.port, () => {
|
|
291
|
+
console.log(
|
|
292
|
+
chalk.bold.blue("Blue Bird Server Online\n") +
|
|
293
|
+
chalk.bold.cyan("App URL: ") +
|
|
294
|
+
chalk.green(`${this.appUrl}`) +
|
|
295
|
+
"\n" +
|
|
296
|
+
chalk.bold.cyan("Internal: ") +
|
|
297
|
+
chalk.green(`${this.host}:${this.port}`) +
|
|
298
|
+
"\n" +
|
|
299
|
+
chalk.gray("────────────────────────────────"),
|
|
300
|
+
);
|
|
301
|
+
});
|
|
302
|
+
})
|
|
303
|
+
.catch((err) => {
|
|
304
|
+
console.error(
|
|
305
|
+
chalk.bold.red("Failed to start Blue Bird:"),
|
|
306
|
+
err.message,
|
|
307
|
+
);
|
|
308
|
+
process.exit(1);
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* Returns a pre-configured Helmet middleware for use on specific routers.
|
|
314
|
+
* @param {Object} [options={}] - Helmet options to override defaults.
|
|
315
|
+
* @returns {Function} Helmet middleware function.
|
|
316
|
+
* @example
|
|
317
|
+
* const router = new Router("/web");
|
|
318
|
+
* router.use(App.helmet({ contentSecurityPolicy: false }));
|
|
319
|
+
*/
|
|
320
|
+
static helmet(options = {}) {
|
|
321
|
+
const defaultOptions = {
|
|
322
|
+
contentSecurityPolicy: props.debug ? false : undefined,
|
|
323
|
+
};
|
|
324
|
+
return helmet({ ...defaultOptions, ...options });
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
export default App;
|