@ecopex/ecopex-framework 1.0.4 → 1.0.5
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 +24 -0
- package/config/swagger.js +45 -0
- package/knexfile.js +0 -2
- package/libraries/2fa.js +2 -1
- package/libraries/aws.js +5 -4
- package/libraries/bcrypt.js +2 -1
- package/libraries/fastify.js +28 -24
- package/libraries/jwt.js +3 -2
- package/libraries/knex.js +2 -2
- package/libraries/upload.js +2 -1
- package/package.json +1 -1
- package/routes/auto/handler.js +2 -2
- package/stores/config.js +10 -0
- package/utils/middleware.js +2 -1
- package/config/swagger/admin.js +0 -44
- package/config/swagger/api.js +0 -19
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// create knex config for production and development
|
|
2
|
+
const { config: configStore } = require('../stores/config');
|
|
3
|
+
module.exports = {
|
|
4
|
+
development: {
|
|
5
|
+
client: 'mysql2',
|
|
6
|
+
connection: {
|
|
7
|
+
host: configStore.database.host || 'localhost',
|
|
8
|
+
port: configStore.database.port || 3306,
|
|
9
|
+
user: configStore.database.user || 'root',
|
|
10
|
+
password: configStore.database.password || '',
|
|
11
|
+
database: configStore.database.database || 'ecopex'
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
production: {
|
|
15
|
+
client: 'mysql2',
|
|
16
|
+
connection: {
|
|
17
|
+
host: configStore.database.host || 'localhost',
|
|
18
|
+
port: configStore.database.port || 3306,
|
|
19
|
+
user: configStore.database.user || 'root',
|
|
20
|
+
password: configStore.database.password || '',
|
|
21
|
+
database: configStore.database.database || 'ecopex'
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Swagger configuration for Admin service
|
|
3
|
+
*/
|
|
4
|
+
const { config } = require('../stores/config');
|
|
5
|
+
module.exports = {
|
|
6
|
+
swagger: {
|
|
7
|
+
openapi: {
|
|
8
|
+
info: {
|
|
9
|
+
title: config.name + ' API',
|
|
10
|
+
description: config.name + ' API documentation for ' + config.name,
|
|
11
|
+
version: '1.0.0'
|
|
12
|
+
},
|
|
13
|
+
host: `${config.host || 'localhost'}:${config.port || 3001}`,
|
|
14
|
+
schemes: ['http', 'https'],
|
|
15
|
+
consumes: ['application/json'],
|
|
16
|
+
produces: ['application/json'],
|
|
17
|
+
components: {
|
|
18
|
+
securitySchemes: {
|
|
19
|
+
bearer: {
|
|
20
|
+
type: 'http',
|
|
21
|
+
scheme: 'bearer',
|
|
22
|
+
bearerFormat: 'JWT',
|
|
23
|
+
description: 'Enter the token with the `Bearer: ` prefix, e.g. "Bearer abcde12345".'
|
|
24
|
+
},
|
|
25
|
+
apiKey: {
|
|
26
|
+
type: 'apiKey',
|
|
27
|
+
in: 'header',
|
|
28
|
+
name: 'apiKey',
|
|
29
|
+
description: 'Enter token e.g. "abcde1-234533-333312-2222".'
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
headers: {
|
|
33
|
+
description: 'Headers',
|
|
34
|
+
scheme: {
|
|
35
|
+
type: 'object',
|
|
36
|
+
properties: {}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
swaggerUi: {
|
|
43
|
+
routePrefix: '/docs'
|
|
44
|
+
}
|
|
45
|
+
};
|
package/knexfile.js
CHANGED
package/libraries/2fa.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
const twofactor = require("node-2fa");
|
|
2
|
+
const { config } = require('../stores/config');
|
|
2
3
|
|
|
3
4
|
const generate_2fa_secret = (account = '') => {
|
|
4
|
-
const newSecret = twofactor.generateSecret({ name:
|
|
5
|
+
const newSecret = twofactor.generateSecret({ name: config.name || 'System', account });
|
|
5
6
|
return newSecret
|
|
6
7
|
}
|
|
7
8
|
|
package/libraries/aws.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
const { S3Client } = require("@aws-sdk/client-s3");
|
|
2
2
|
const { Upload } = require("@aws-sdk/lib-storage");
|
|
3
3
|
const fs = require('fs');
|
|
4
|
+
const { config } = require('../stores/config');
|
|
4
5
|
|
|
5
6
|
// Config
|
|
6
7
|
|
|
@@ -8,17 +9,17 @@ const fs = require('fs');
|
|
|
8
9
|
const uploadBase = __dirname + '../uploads/'
|
|
9
10
|
|
|
10
11
|
// The name of the bucket that you have created
|
|
11
|
-
const BUCKET_NAME =
|
|
12
|
+
const BUCKET_NAME = config.aws_bucket_name;
|
|
12
13
|
|
|
13
14
|
|
|
14
15
|
const uploadAws = async (filePath, contentType) => {
|
|
15
16
|
|
|
16
17
|
const client = new S3Client({
|
|
17
18
|
bucketEndpoint: BUCKET_NAME,
|
|
18
|
-
region:
|
|
19
|
+
region: config.aws_region,
|
|
19
20
|
credentials: {
|
|
20
|
-
secretAccessKey:
|
|
21
|
-
accessKeyId:
|
|
21
|
+
secretAccessKey: config.aws_secret_access_key,
|
|
22
|
+
accessKeyId: config.aws_access_key_id
|
|
22
23
|
}
|
|
23
24
|
});
|
|
24
25
|
|
package/libraries/bcrypt.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
const bcrypt = require('bcryptjs');
|
|
2
|
+
const { config } = require('../stores/config');
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* Bcrypt Library Class
|
|
5
6
|
* Provides methods for password hashing, comparison, and salt generation
|
|
6
7
|
*/
|
|
7
8
|
class Bcrypt {
|
|
8
|
-
constructor(saltRounds = 12) {
|
|
9
|
+
constructor(saltRounds = config.bcrypt_salt_rounds || 12) {
|
|
9
10
|
this.saltRounds = saltRounds;
|
|
10
11
|
}
|
|
11
12
|
|
package/libraries/fastify.js
CHANGED
|
@@ -1,28 +1,28 @@
|
|
|
1
|
-
require('dotenv').config();
|
|
2
|
-
|
|
3
|
-
const fastify = require('fastify')({
|
|
4
|
-
logger: {
|
|
5
|
-
level: process.env.LOG_LEVEL || 'info'
|
|
6
|
-
}
|
|
7
|
-
});
|
|
8
|
-
|
|
9
1
|
const { loadRoutes } = require('../utils/routeLoader');
|
|
10
2
|
const Middleware = require('../utils/middleware');
|
|
3
|
+
const { setConfig } = require('../stores/config');
|
|
11
4
|
|
|
12
5
|
// Register plugins
|
|
13
|
-
async function registerPlugins() {
|
|
6
|
+
async function registerPlugins(config) {
|
|
7
|
+
|
|
8
|
+
const fastify = require('fastify')({
|
|
9
|
+
logger: {
|
|
10
|
+
level: config.log_level || 'info'
|
|
11
|
+
}
|
|
12
|
+
});
|
|
13
|
+
|
|
14
14
|
// Set error handler first, before any plugins
|
|
15
15
|
fastify.setErrorHandler(Middleware.errorHandler);
|
|
16
16
|
fastify.setNotFoundHandler(Middleware.errorNotFoundHandler);
|
|
17
17
|
|
|
18
18
|
// CORS plugin
|
|
19
19
|
await fastify.register(require('@fastify/cors'), {
|
|
20
|
-
origin:
|
|
20
|
+
origin: config.cors || '*',
|
|
21
21
|
credentials: true
|
|
22
22
|
});
|
|
23
23
|
|
|
24
24
|
// Swagger documentation
|
|
25
|
-
const swaggerConfig = require('../config/swagger
|
|
25
|
+
const swaggerConfig = require('../config/swagger');
|
|
26
26
|
|
|
27
27
|
await fastify.register(require('@fastify/swagger'), swaggerConfig.swagger);
|
|
28
28
|
|
|
@@ -51,20 +51,22 @@ async function registerPlugins() {
|
|
|
51
51
|
// Health check endpoint
|
|
52
52
|
fastify.get('/health', async (request, reply) => {
|
|
53
53
|
return {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
54
|
+
status: 'ok',
|
|
55
|
+
service: config.name,
|
|
56
|
+
timestamp: new Date().toISOString(),
|
|
57
|
+
uptime: process.uptime(),
|
|
58
|
+
pid: process.pid
|
|
59
59
|
};
|
|
60
60
|
});
|
|
61
|
+
|
|
62
|
+
return fastify;
|
|
61
63
|
}
|
|
62
64
|
|
|
63
65
|
// Load admin routes, common routes, and auto routes
|
|
64
|
-
async function loadAdminRoutes() {
|
|
66
|
+
async function loadAdminRoutes(fastifyInstance, config) {
|
|
65
67
|
try {
|
|
66
68
|
// Load admin routes
|
|
67
|
-
await loadRoutes(
|
|
69
|
+
await loadRoutes(fastifyInstance, __dirname + '/../routes/' + config.name, true, config.name);
|
|
68
70
|
} catch (error) {
|
|
69
71
|
console.error('Error loading admin routes:', error);
|
|
70
72
|
process.exit(1);
|
|
@@ -72,20 +74,22 @@ async function loadAdminRoutes() {
|
|
|
72
74
|
}
|
|
73
75
|
|
|
74
76
|
// Start admin service
|
|
75
|
-
async function start() {
|
|
77
|
+
async function start(config) {
|
|
76
78
|
try {
|
|
77
79
|
|
|
80
|
+
setConfig(config);
|
|
81
|
+
|
|
78
82
|
// Register plugins
|
|
79
|
-
await registerPlugins();
|
|
83
|
+
const fastifyInstance = await registerPlugins(config);
|
|
80
84
|
|
|
81
85
|
// Load admin routes
|
|
82
|
-
await loadAdminRoutes();
|
|
86
|
+
await loadAdminRoutes(fastifyInstance, config);
|
|
83
87
|
|
|
84
88
|
// Start server
|
|
85
|
-
const port =
|
|
86
|
-
const host =
|
|
89
|
+
const port = config.port || 3001;
|
|
90
|
+
const host = config.host || '127.0.0.1';
|
|
87
91
|
|
|
88
|
-
|
|
92
|
+
fastifyInstance.listen({ port, host });
|
|
89
93
|
|
|
90
94
|
} catch (error) {
|
|
91
95
|
console.error('Error starting admin service:', error);
|
package/libraries/jwt.js
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
const jwt = require('jsonwebtoken');
|
|
2
|
+
const { config } = require('../stores/config');
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* JWT Library Class
|
|
5
6
|
* Provides methods for JWT token operations including sign, verify, and decode
|
|
6
7
|
*/
|
|
7
8
|
class JWT {
|
|
8
|
-
constructor(secretKey =
|
|
9
|
+
constructor(secretKey = config.jwt_secret || 'default-secret-key') {
|
|
9
10
|
this.secretKey = secretKey;
|
|
10
|
-
this.defaultExpiresIn = '1d';
|
|
11
|
+
this.defaultExpiresIn = config.jwt_expires_in || '1d';
|
|
11
12
|
}
|
|
12
13
|
|
|
13
14
|
/**
|
package/libraries/knex.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
const knex = require('knex');
|
|
2
|
-
|
|
2
|
+
const { config: configStore } = require('../stores/config');
|
|
3
3
|
const config = require('../config/database');
|
|
4
4
|
|
|
5
|
-
const db = knex(config[
|
|
5
|
+
const db = knex(config[configStore.development ? 'development' : 'production']);
|
|
6
6
|
|
|
7
7
|
module.exports = db;
|
package/libraries/upload.js
CHANGED
|
@@ -6,6 +6,7 @@ const { resize: resizeImage } = require('./image')
|
|
|
6
6
|
const sizeOf = require('image-size');
|
|
7
7
|
const returnSlug = require('./slug')
|
|
8
8
|
const { uploadAws } = require('./aws');
|
|
9
|
+
const { config } = require('../stores/config');
|
|
9
10
|
|
|
10
11
|
const uploadFile = async (fileObject = {}, prefix = '', directory = '', resizeOpts = false) => {
|
|
11
12
|
|
|
@@ -70,7 +71,7 @@ const createReturnData = async (fileName = '', directory = '', resizeOpts = fals
|
|
|
70
71
|
}
|
|
71
72
|
}
|
|
72
73
|
|
|
73
|
-
if(
|
|
74
|
+
if(config.aws_status == 'true') {
|
|
74
75
|
|
|
75
76
|
await sleep(1000);
|
|
76
77
|
|
package/package.json
CHANGED
package/routes/auto/handler.js
CHANGED
|
@@ -5,7 +5,7 @@ const i18n = require('../../utils/i18n');
|
|
|
5
5
|
const { uploadFile } = require('../../libraries/upload');
|
|
6
6
|
const { randomChars } = require('../../libraries/controls');
|
|
7
7
|
const { get_all, get_by_primary_key, create_item, update_item, delete_item } = require('../../stores');
|
|
8
|
-
|
|
8
|
+
const { config } = require('../../stores/config');
|
|
9
9
|
/**
|
|
10
10
|
* Get all records with pagination, search, and join tables
|
|
11
11
|
* @param {Object} request - The request object
|
|
@@ -277,7 +277,7 @@ async function create(request, reply, routeOptions = {}) {
|
|
|
277
277
|
data: item
|
|
278
278
|
});
|
|
279
279
|
} catch (error) {
|
|
280
|
-
if(
|
|
280
|
+
if(config.development) {
|
|
281
281
|
console.log(error.message);
|
|
282
282
|
}
|
|
283
283
|
if(error.sqlMessage) {
|
package/stores/config.js
ADDED
package/utils/middleware.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
const i18n = require('./i18n');
|
|
2
2
|
const db = require('../libraries/knex');
|
|
3
|
+
const { config } = require('../stores/config');
|
|
3
4
|
/**
|
|
4
5
|
* Middleware utilities for Fastify
|
|
5
6
|
*/
|
|
@@ -31,7 +32,7 @@ class Middleware {
|
|
|
31
32
|
*/
|
|
32
33
|
static errorHandler(error, request, reply) {
|
|
33
34
|
|
|
34
|
-
if(
|
|
35
|
+
if(config.development) {
|
|
35
36
|
console.log(error);
|
|
36
37
|
}
|
|
37
38
|
|
package/config/swagger/admin.js
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Swagger configuration for Admin service
|
|
3
|
-
*/
|
|
4
|
-
module.exports = {
|
|
5
|
-
swagger: {
|
|
6
|
-
openapi: {
|
|
7
|
-
info: {
|
|
8
|
-
title: 'Ecommerce Admin API',
|
|
9
|
-
description: 'Admin API documentation for Ecommerce',
|
|
10
|
-
version: '1.0.0'
|
|
11
|
-
},
|
|
12
|
-
host: `${process.env.HOST || 'localhost'}:${process.env.ADMIN_PORT || 3001}`,
|
|
13
|
-
schemes: ['http', 'https'],
|
|
14
|
-
consumes: ['application/json'],
|
|
15
|
-
produces: ['application/json'],
|
|
16
|
-
components: {
|
|
17
|
-
securitySchemes: {
|
|
18
|
-
bearer: {
|
|
19
|
-
type: 'http',
|
|
20
|
-
scheme: 'bearer',
|
|
21
|
-
bearerFormat: 'JWT',
|
|
22
|
-
description: 'Enter the token with the `Bearer: ` prefix, e.g. "Bearer abcde12345".'
|
|
23
|
-
},
|
|
24
|
-
apiKey: {
|
|
25
|
-
type: 'apiKey',
|
|
26
|
-
in: 'header',
|
|
27
|
-
name: 'apiKey',
|
|
28
|
-
description: 'Enter token e.g. "abcde1-234533-333312-2222".'
|
|
29
|
-
}
|
|
30
|
-
},
|
|
31
|
-
headers: {
|
|
32
|
-
description: 'Headers',
|
|
33
|
-
scheme: {
|
|
34
|
-
type: 'object',
|
|
35
|
-
properties: {}
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
},
|
|
41
|
-
swaggerUi: {
|
|
42
|
-
routePrefix: '/docs'
|
|
43
|
-
}
|
|
44
|
-
};
|
package/config/swagger/api.js
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Swagger configuration for API service
|
|
3
|
-
*/
|
|
4
|
-
module.exports = {
|
|
5
|
-
swagger: {
|
|
6
|
-
info: {
|
|
7
|
-
title: 'SB System API',
|
|
8
|
-
description: 'API documentation for SB System',
|
|
9
|
-
version: '1.0.0'
|
|
10
|
-
},
|
|
11
|
-
host: `${process.env.HOST || 'localhost'}:${process.env.API_PORT || 3000}`,
|
|
12
|
-
schemes: ['http', 'https'],
|
|
13
|
-
consumes: ['application/json'],
|
|
14
|
-
produces: ['application/json']
|
|
15
|
-
},
|
|
16
|
-
swaggerUi: {
|
|
17
|
-
routePrefix: '/docs'
|
|
18
|
-
}
|
|
19
|
-
};
|