@ecopex/ecopex-framework 1.0.6 → 1.0.7
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/config/database.js +12 -10
- package/index.js +14 -1
- package/libraries/fastify.js +23 -40
- package/package.json +1 -1
package/config/database.js
CHANGED
|
@@ -1,24 +1,26 @@
|
|
|
1
1
|
// create knex config for production and development
|
|
2
2
|
const { config: configStore } = require('../stores/config');
|
|
3
|
+
const databaseConfig = configStore?.database || {};
|
|
4
|
+
|
|
3
5
|
module.exports = {
|
|
4
6
|
development: {
|
|
5
7
|
client: 'mysql2',
|
|
6
8
|
connection: {
|
|
7
|
-
host:
|
|
8
|
-
port:
|
|
9
|
-
user:
|
|
10
|
-
password:
|
|
11
|
-
database:
|
|
9
|
+
host: databaseConfig?.host || 'localhost',
|
|
10
|
+
port: databaseConfig?.port || 3306,
|
|
11
|
+
user: databaseConfig?.user || 'root',
|
|
12
|
+
password: databaseConfig?.password || '',
|
|
13
|
+
database: databaseConfig?.database || 'ecopex'
|
|
12
14
|
}
|
|
13
15
|
},
|
|
14
16
|
production: {
|
|
15
17
|
client: 'mysql2',
|
|
16
18
|
connection: {
|
|
17
|
-
host:
|
|
18
|
-
port:
|
|
19
|
-
user:
|
|
20
|
-
password:
|
|
21
|
-
database:
|
|
19
|
+
host: databaseConfig?.host || 'localhost',
|
|
20
|
+
port: databaseConfig?.port || 3306,
|
|
21
|
+
user: databaseConfig?.user || 'root',
|
|
22
|
+
password: databaseConfig?.password || '',
|
|
23
|
+
database: databaseConfig?.database || 'ecopex'
|
|
22
24
|
}
|
|
23
25
|
}
|
|
24
26
|
}
|
package/index.js
CHANGED
|
@@ -1,5 +1,18 @@
|
|
|
1
1
|
const { start: fastifyStart } = require('./libraries/fastify');
|
|
2
|
+
const knex = require('./libraries/knex');
|
|
3
|
+
const JWT = require('./libraries/jwt');
|
|
4
|
+
const BCRYPT = require('./libraries/bcrypt');
|
|
5
|
+
const date = require('./libraries/date');
|
|
6
|
+
const twofactor = require('./libraries/2fa');
|
|
7
|
+
|
|
8
|
+
const jwt = new JWT();
|
|
9
|
+
const bcrypt = new BCRYPT();
|
|
2
10
|
|
|
3
11
|
module.exports = {
|
|
4
|
-
fastifyStart
|
|
12
|
+
fastifyStart,
|
|
13
|
+
db: knex,
|
|
14
|
+
date: date,
|
|
15
|
+
jwt: jwt,
|
|
16
|
+
bcrypt: bcrypt,
|
|
17
|
+
twofactor: twofactor
|
|
5
18
|
}
|
package/libraries/fastify.js
CHANGED
|
@@ -2,21 +2,24 @@ const { loadRoutes } = require('../utils/routeLoader');
|
|
|
2
2
|
const Middleware = require('../utils/middleware');
|
|
3
3
|
const { setConfig } = require('../stores/config');
|
|
4
4
|
|
|
5
|
+
let fastifyInstance = null;
|
|
6
|
+
|
|
5
7
|
// Register plugins
|
|
6
8
|
async function registerPlugins(config) {
|
|
7
9
|
|
|
8
|
-
|
|
10
|
+
fastifyInstance = require('fastify')({
|
|
9
11
|
logger: {
|
|
10
|
-
level: config.log_level || 'info'
|
|
12
|
+
level: config.log_level || 'info',
|
|
13
|
+
file: config.log_file || null
|
|
11
14
|
}
|
|
12
15
|
});
|
|
13
16
|
|
|
14
17
|
// Set error handler first, before any plugins
|
|
15
|
-
|
|
16
|
-
|
|
18
|
+
fastifyInstance.setErrorHandler(Middleware.errorHandler);
|
|
19
|
+
fastifyInstance.setNotFoundHandler(Middleware.errorNotFoundHandler);
|
|
17
20
|
|
|
18
21
|
// CORS plugin
|
|
19
|
-
await
|
|
22
|
+
await fastifyInstance.register(require('@fastify/cors'), {
|
|
20
23
|
origin: config.cors || '*',
|
|
21
24
|
credentials: true
|
|
22
25
|
});
|
|
@@ -24,13 +27,13 @@ async function registerPlugins(config) {
|
|
|
24
27
|
// Swagger documentation
|
|
25
28
|
const swaggerConfig = require('../config/swagger');
|
|
26
29
|
|
|
27
|
-
await
|
|
30
|
+
await fastifyInstance.register(require('@fastify/swagger'), swaggerConfig.swagger);
|
|
28
31
|
|
|
29
32
|
// Swagger UI
|
|
30
|
-
await
|
|
33
|
+
await fastifyInstance.register(require('@fastify/swagger-ui'), swaggerConfig.swaggerUi);
|
|
31
34
|
|
|
32
35
|
// Fastify Multipart plugin
|
|
33
|
-
|
|
36
|
+
fastifyInstance.register(require('@fastify/multipart'), {
|
|
34
37
|
attachFieldsToBody: true, // Optional: attaches fields to the body object
|
|
35
38
|
throwFileSizeLimit: true, // Optional: throws an error if the file size limit is exceeded
|
|
36
39
|
limits: {
|
|
@@ -45,11 +48,11 @@ async function registerPlugins(config) {
|
|
|
45
48
|
});
|
|
46
49
|
|
|
47
50
|
// Register other middleware
|
|
48
|
-
|
|
49
|
-
|
|
51
|
+
fastifyInstance.addHook('preHandler', Middleware.languageDetection());
|
|
52
|
+
fastifyInstance.addHook('preHandler', Middleware.responseFormatter());
|
|
50
53
|
|
|
51
54
|
// Health check endpoint
|
|
52
|
-
|
|
55
|
+
fastifyInstance.get('/health', async (request, reply) => {
|
|
53
56
|
return {
|
|
54
57
|
status: 'ok',
|
|
55
58
|
service: config.name,
|
|
@@ -59,14 +62,16 @@ async function registerPlugins(config) {
|
|
|
59
62
|
};
|
|
60
63
|
});
|
|
61
64
|
|
|
62
|
-
return
|
|
65
|
+
return fastifyInstance;
|
|
63
66
|
}
|
|
64
67
|
|
|
65
68
|
// Load admin routes, common routes, and auto routes
|
|
66
|
-
async function loadAdminRoutes(
|
|
69
|
+
async function loadAdminRoutes(config) {
|
|
67
70
|
try {
|
|
68
71
|
// Load admin routes
|
|
69
|
-
|
|
72
|
+
if(fastifyInstance) {
|
|
73
|
+
await loadRoutes(fastifyInstance, './routes/' + config.name, true, config.name);
|
|
74
|
+
}
|
|
70
75
|
} catch (error) {
|
|
71
76
|
console.error('Error loading admin routes:', error);
|
|
72
77
|
process.exit(1);
|
|
@@ -75,16 +80,15 @@ async function loadAdminRoutes(fastifyInstance, config) {
|
|
|
75
80
|
|
|
76
81
|
// Start admin service
|
|
77
82
|
async function start(config) {
|
|
78
|
-
console.log('config', config);
|
|
79
83
|
try {
|
|
80
84
|
|
|
81
85
|
setConfig(config);
|
|
82
86
|
|
|
83
87
|
// Register plugins
|
|
84
|
-
|
|
85
|
-
|
|
88
|
+
await registerPlugins(config);
|
|
89
|
+
|
|
86
90
|
// Load admin routes
|
|
87
|
-
await loadAdminRoutes(
|
|
91
|
+
await loadAdminRoutes(config);
|
|
88
92
|
|
|
89
93
|
// Start server
|
|
90
94
|
const port = config.port || 3001;
|
|
@@ -93,32 +97,11 @@ async function start(config) {
|
|
|
93
97
|
fastifyInstance.listen({ port, host });
|
|
94
98
|
|
|
95
99
|
} catch (error) {
|
|
96
|
-
console.error('Error starting
|
|
100
|
+
console.error('Error starting ' + config.name + ' server:', error);
|
|
97
101
|
process.exit(1);
|
|
98
102
|
}
|
|
99
103
|
}
|
|
100
104
|
|
|
101
|
-
// Graceful shutdown
|
|
102
|
-
process.on('SIGINT', async () => {
|
|
103
|
-
try {
|
|
104
|
-
await fastify.close();
|
|
105
|
-
process.exit(0);
|
|
106
|
-
} catch (error) {
|
|
107
|
-
console.error('Error during admin service shutdown:', error);
|
|
108
|
-
process.exit(1);
|
|
109
|
-
}
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
process.on('SIGTERM', async () => {
|
|
113
|
-
try {
|
|
114
|
-
await fastify.close();
|
|
115
|
-
process.exit(0);
|
|
116
|
-
} catch (error) {
|
|
117
|
-
console.error('Error during admin service shutdown:', error);
|
|
118
|
-
process.exit(1);
|
|
119
|
-
}
|
|
120
|
-
});
|
|
121
|
-
|
|
122
105
|
// Start the admin service
|
|
123
106
|
module.exports = {
|
|
124
107
|
start
|