@ecopex/ecopex-framework 1.0.5 → 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.
@@ -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: 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'
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: 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'
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
  }
@@ -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
- const fastify = require('fastify')({
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
- fastify.setErrorHandler(Middleware.errorHandler);
16
- fastify.setNotFoundHandler(Middleware.errorNotFoundHandler);
18
+ fastifyInstance.setErrorHandler(Middleware.errorHandler);
19
+ fastifyInstance.setNotFoundHandler(Middleware.errorNotFoundHandler);
17
20
 
18
21
  // CORS plugin
19
- await fastify.register(require('@fastify/cors'), {
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 fastify.register(require('@fastify/swagger'), swaggerConfig.swagger);
30
+ await fastifyInstance.register(require('@fastify/swagger'), swaggerConfig.swagger);
28
31
 
29
32
  // Swagger UI
30
- await fastify.register(require('@fastify/swagger-ui'), swaggerConfig.swaggerUi);
33
+ await fastifyInstance.register(require('@fastify/swagger-ui'), swaggerConfig.swaggerUi);
31
34
 
32
35
  // Fastify Multipart plugin
33
- fastify.register(require('@fastify/multipart'), {
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
- fastify.addHook('preHandler', Middleware.languageDetection());
49
- fastify.addHook('preHandler', Middleware.responseFormatter());
51
+ fastifyInstance.addHook('preHandler', Middleware.languageDetection());
52
+ fastifyInstance.addHook('preHandler', Middleware.responseFormatter());
50
53
 
51
54
  // Health check endpoint
52
- fastify.get('/health', async (request, reply) => {
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 fastify;
65
+ return fastifyInstance;
63
66
  }
64
67
 
65
68
  // Load admin routes, common routes, and auto routes
66
- async function loadAdminRoutes(fastifyInstance, config) {
69
+ async function loadAdminRoutes(config) {
67
70
  try {
68
71
  // Load admin routes
69
- await loadRoutes(fastifyInstance, __dirname + '/../routes/' + config.name, true, config.name);
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);
@@ -76,14 +81,14 @@ async function loadAdminRoutes(fastifyInstance, config) {
76
81
  // Start admin service
77
82
  async function start(config) {
78
83
  try {
79
-
84
+
80
85
  setConfig(config);
81
86
 
82
87
  // Register plugins
83
- const fastifyInstance = await registerPlugins(config);
84
-
88
+ await registerPlugins(config);
89
+
85
90
  // Load admin routes
86
- await loadAdminRoutes(fastifyInstance, config);
91
+ await loadAdminRoutes(config);
87
92
 
88
93
  // Start server
89
94
  const port = config.port || 3001;
@@ -92,32 +97,11 @@ async function start(config) {
92
97
  fastifyInstance.listen({ port, host });
93
98
 
94
99
  } catch (error) {
95
- console.error('Error starting admin service:', error);
100
+ console.error('Error starting ' + config.name + ' server:', error);
96
101
  process.exit(1);
97
102
  }
98
103
  }
99
104
 
100
- // Graceful shutdown
101
- process.on('SIGINT', async () => {
102
- try {
103
- await fastify.close();
104
- process.exit(0);
105
- } catch (error) {
106
- console.error('Error during admin service shutdown:', error);
107
- process.exit(1);
108
- }
109
- });
110
-
111
- process.on('SIGTERM', async () => {
112
- try {
113
- await fastify.close();
114
- process.exit(0);
115
- } catch (error) {
116
- console.error('Error during admin service shutdown:', error);
117
- process.exit(1);
118
- }
119
- });
120
-
121
105
  // Start the admin service
122
106
  module.exports = {
123
107
  start
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@ecopex/ecopex-framework",
3
- "version": "1.0.5",
3
+ "version": "1.0.7",
4
4
  "description": "Javascript Framework for API and Admin Panel",
5
- "main": "ecosystem.config.js",
5
+ "main": "index.js",
6
6
  "scripts": {
7
7
  "start": "pm2 start ecosystem.config.js",
8
8
  "restart": "pm2 restart ecosystem.config.js",
package/env.example DELETED
@@ -1,73 +0,0 @@
1
- # ===========================================
2
- # SB System Environment Configuration
3
- # ===========================================
4
-
5
- # Application Environment
6
- NODE_ENV=development
7
- LOG_LEVEL=info
8
-
9
- # Server Configuration
10
- HOST=0.0.0.0
11
- API_PORT=3000
12
- ADMIN_PORT=3001
13
-
14
- # Database Configuration
15
- DB_HOST=localhost
16
- DB_PORT=3306
17
- DB_USER=root
18
- DB_PASSWORD=your_password_here
19
- DB_NAME=sb_system
20
-
21
- # Alternative: Use DATABASE_URL for production
22
- # DATABASE_URL=mysql://username:password@host:port/database
23
-
24
- # CORS Configuration
25
- CORS_ORIGIN=*
26
-
27
- # PM2 Process Management
28
- API_INSTANCES=1
29
- API_EXEC_MODE=fork
30
- ADMIN_INSTANCES=1
31
- ADMIN_EXEC_MODE=fork
32
-
33
- # Internationalization
34
- DEFAULT_LOCALE=en
35
- SUPPORTED_LOCALES=en,tr,es
36
-
37
- # Security (for production)
38
- # JWT_SECRET=your_jwt_secret_here
39
- # BCRYPT_ROUNDS=12
40
- # SESSION_SECRET=your_session_secret_here
41
-
42
- # Redis (for caching/sessions)
43
- # REDIS_HOST=localhost
44
- # REDIS_PORT=6379
45
- # REDIS_PASSWORD=
46
-
47
- # Email Configuration (for notifications)
48
- # SMTP_HOST=smtp.gmail.com
49
- # SMTP_PORT=587
50
- # SMTP_USER=your_email@gmail.com
51
- # SMTP_PASS=your_app_password
52
- # FROM_EMAIL=noreply@yoursystem.com
53
-
54
- # File Upload Configuration
55
- # UPLOAD_MAX_SIZE=10485760
56
- # UPLOAD_ALLOWED_TYPES=image/jpeg,image/png,image/gif,application/pdf
57
-
58
- # Rate Limiting
59
- # RATE_LIMIT_WINDOW_MS=900000
60
- # RATE_LIMIT_MAX_REQUESTS=100
61
-
62
- # Logging
63
- # LOG_FILE_PATH=./logs/app.log
64
- # LOG_MAX_SIZE=10m
65
- # LOG_MAX_FILES=5
66
-
67
- # Monitoring
68
- # HEALTH_CHECK_INTERVAL=30000
69
- # METRICS_ENABLED=true
70
-
71
- # Development Tools
72
- # DEBUG=sb_system:*
73
- # NODE_OPTIONS=--max-old-space-size=4096