nodejs-structure-cli 1.0.1 → 1.0.2
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/bin/index.js +5 -5
- package/package.json +1 -1
- package/templates/common/database/js/database.js.ejs +5 -3
- package/templates/common/database/js/models/User.js.ejs +4 -4
- package/templates/common/database/js/mongoose.js.ejs +6 -4
- package/templates/common/health/js/healthRoute.js.ejs +11 -8
- package/templates/common/package.json.ejs +1 -0
- package/templates/common/shutdown/js/gracefulShutdown.js.ejs +23 -14
- package/templates/common/src/config/auth.js.ejs +1 -1
- package/templates/common/src/controllers/authController.js.ejs +2 -2
- package/templates/common/src/middleware/auth.js.ejs +1 -1
- package/templates/common/src/middleware/upload.js.ejs +1 -1
- package/templates/common/src/routes/authRoutes.js.ejs +1 -1
- package/templates/mvc/js/src/config/env.js.ejs +5 -4
- package/templates/mvc/js/src/config/swagger.js.ejs +7 -3
- package/templates/mvc/js/src/controllers/userController.js.ejs +17 -21
- package/templates/mvc/js/src/index.js.ejs +26 -25
- package/templates/mvc/js/src/routes/api.js.ejs +7 -6
- package/templates/mvc/js/src/utils/errorMessages.js +1 -1
- package/templates/mvc/js/src/utils/errorMiddleware.js +4 -4
- package/templates/mvc/js/src/utils/httpCodes.js +1 -1
- package/templates/mvc/js/src/utils/logger.js +3 -3
package/bin/index.js
CHANGED
|
@@ -42,13 +42,13 @@ program
|
|
|
42
42
|
"-c, --communication <communication>",
|
|
43
43
|
"Communication (REST APIs, GraphQL, Kafka)",
|
|
44
44
|
)
|
|
45
|
-
.option(
|
|
46
|
-
"--ci-provider <provider>",
|
|
47
|
-
"CI/CD Provider (None, GitHub Actions, Jenkins, GitLab CI)",
|
|
48
|
-
)
|
|
45
|
+
.option("--ci-provider <provider>", "CI/CD Provider (None, GitHub Actions, Jenkins, GitLab CI)")
|
|
49
46
|
.option("--include-security", "Include Enterprise Security Hardening")
|
|
50
47
|
.option("--no-include-security", "Exclude Enterprise Security Hardening")
|
|
51
|
-
.option("--caching <type>", "Caching Layer (None/Redis)")
|
|
48
|
+
.option("--caching <type>", "Caching Layer (None/Redis/Memory Cache)")
|
|
49
|
+
.option("--auth <type>", "Authentication (None, Better-Auth, JWT, OAuth)")
|
|
50
|
+
.option("--google-login <type>", "Google Login (None, Google Login)")
|
|
51
|
+
.option("--include-multer <choice>", "Add image upload capability (No/Yes)")
|
|
52
52
|
.action(async (options) => {
|
|
53
53
|
// Fix for Commander camelCase conversion
|
|
54
54
|
if (options.ciProvider) {
|
package/package.json
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { Sequelize } from 'sequelize';
|
|
2
|
+
import dotenv from 'dotenv';
|
|
3
|
+
|
|
4
|
+
dotenv.config();
|
|
3
5
|
|
|
4
6
|
<% if (database === 'MySQL') { %>const dialect = 'mysql';<% } -%>
|
|
5
7
|
<% if (database === 'PostgreSQL') { %>const dialect = 'postgres';<% } -%>
|
|
@@ -16,4 +18,4 @@ const sequelize = new Sequelize(
|
|
|
16
18
|
}
|
|
17
19
|
);
|
|
18
20
|
|
|
19
|
-
|
|
21
|
+
export default sequelize;
|
|
@@ -39,10 +39,10 @@ class UserModel {
|
|
|
39
39
|
}
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
-
|
|
42
|
+
export default UserModel;
|
|
43
43
|
<% } else { -%>
|
|
44
|
-
|
|
45
|
-
<% if (architecture === 'MVC') { %>
|
|
44
|
+
import { DataTypes, Model } from 'sequelize';
|
|
45
|
+
<% if (architecture === 'MVC') { %>import sequelize from '../config/database.js';<% } else { %>import sequelize from '../database.js';<% } %>
|
|
46
46
|
|
|
47
47
|
class User extends Model {}
|
|
48
48
|
|
|
@@ -87,5 +87,5 @@ User.init(
|
|
|
87
87
|
}
|
|
88
88
|
);
|
|
89
89
|
|
|
90
|
-
|
|
90
|
+
export default User;
|
|
91
91
|
<% } -%>
|
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
|
|
1
|
+
import mongoose from 'mongoose';
|
|
2
2
|
|
|
3
3
|
let logger;
|
|
4
4
|
<% if (architecture === 'MVC') { %>
|
|
5
|
-
|
|
5
|
+
import logger_mvc from '../utils/logger.js';
|
|
6
|
+
logger = logger_mvc;
|
|
6
7
|
<% } else { %>
|
|
7
|
-
|
|
8
|
+
import logger_ca from '../log/logger.js';
|
|
9
|
+
logger = logger_ca;
|
|
8
10
|
<% } %>
|
|
9
11
|
const connectDB = async () => {
|
|
10
12
|
const dbHost = process.env.DB_HOST || 'localhost';
|
|
@@ -30,4 +32,4 @@ const connectDB = async () => {
|
|
|
30
32
|
}
|
|
31
33
|
};
|
|
32
34
|
|
|
33
|
-
|
|
35
|
+
export default connectDB; // Export function to call in index.js
|
|
@@ -1,16 +1,19 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import express from 'express';
|
|
2
|
+
import logger from '<% if (architecture === "MVC") { %>../utils/logger.js<% } else { %>../../infrastructure/log/logger.js<% } %>';
|
|
3
|
+
import HTTP_STATUS from '<% if (architecture === "MVC") { %>../utils/httpCodes.js<% } else { %>../../utils/httpCodes.js<% } %>';
|
|
4
|
+
|
|
5
5
|
<%_ if (database !== 'None') { -%>
|
|
6
|
-
|
|
6
|
+
import ERROR_MESSAGES from '<% if (architecture === "MVC") { %>../utils/errorMessages.js<% } else { %>../../utils/errorMessages.js<% } %>';
|
|
7
7
|
<%_ } -%>
|
|
8
|
+
|
|
8
9
|
<%_ if (database === 'MongoDB') { -%>
|
|
9
|
-
|
|
10
|
+
import mongoose from 'mongoose';
|
|
10
11
|
<%_ } else if (database !== 'None') { -%>
|
|
11
|
-
|
|
12
|
+
import sequelize from '<% if (architecture === "MVC") { %>../config/database.js<% } else { %>../../infrastructure/database/database.js<% } %>';
|
|
12
13
|
<%_ } -%>
|
|
13
14
|
|
|
15
|
+
const router = express.Router();
|
|
16
|
+
|
|
14
17
|
router.get('/', async (req, res) => {
|
|
15
18
|
const healthData = {
|
|
16
19
|
status: 'UP',
|
|
@@ -47,4 +50,4 @@ router.get('/', async (req, res) => {
|
|
|
47
50
|
res.status(HTTP_STATUS.OK).json(healthData);
|
|
48
51
|
});
|
|
49
52
|
|
|
50
|
-
|
|
53
|
+
export default router;
|
|
@@ -1,17 +1,30 @@
|
|
|
1
1
|
<%_
|
|
2
|
-
let loggerPath = './logger';
|
|
3
|
-
let dbPath = '../config/database';
|
|
4
|
-
let redisPath = '../config/redisClient';
|
|
5
|
-
let kafkaPath = '../services/kafkaService';
|
|
2
|
+
let loggerPath = './logger.js';
|
|
3
|
+
let dbPath = '../config/database.js';
|
|
4
|
+
let redisPath = '../config/redisClient.js';
|
|
5
|
+
let kafkaPath = '../services/kafkaService.js';
|
|
6
6
|
|
|
7
7
|
if (architecture === 'Clean Architecture') {
|
|
8
|
-
loggerPath = '../infrastructure/log/logger';
|
|
9
|
-
dbPath = '../infrastructure/database/database';
|
|
10
|
-
redisPath = '../infrastructure/caching/redisClient';
|
|
11
|
-
kafkaPath = '../infrastructure/messaging/kafkaClient';
|
|
8
|
+
loggerPath = '../infrastructure/log/logger.js';
|
|
9
|
+
dbPath = '../infrastructure/database/database.js';
|
|
10
|
+
redisPath = '../infrastructure/caching/redisClient.js';
|
|
11
|
+
kafkaPath = '../infrastructure/messaging/kafkaClient.js';
|
|
12
12
|
}
|
|
13
13
|
-%>
|
|
14
|
-
|
|
14
|
+
import logger from '<%- loggerPath %>';
|
|
15
|
+
<%_ if (database !== 'None') { -%>
|
|
16
|
+
<%_ if (database === 'MongoDB') { -%>
|
|
17
|
+
import mongoose from 'mongoose';
|
|
18
|
+
<%_ } else { -%>
|
|
19
|
+
import sequelize from '<%- dbPath %>';
|
|
20
|
+
<%_ } -%>
|
|
21
|
+
<%_ if (caching === 'Redis') { -%>
|
|
22
|
+
import redisService from '<%- redisPath %>';
|
|
23
|
+
<%_ } -%>
|
|
24
|
+
<%_ if (communication === 'Kafka') { -%>
|
|
25
|
+
import { disconnectKafka } from '<%- kafkaPath %>';
|
|
26
|
+
<%_ } -%>
|
|
27
|
+
<%_ } -%>
|
|
15
28
|
|
|
16
29
|
const setupGracefulShutdown = (server) => {
|
|
17
30
|
const gracefulShutdown = async (signal) => {
|
|
@@ -25,22 +38,18 @@ const setupGracefulShutdown = (server) => {
|
|
|
25
38
|
try {
|
|
26
39
|
<%_ if (database !== 'None') { -%>
|
|
27
40
|
<%_ if (database === 'MongoDB') { -%>
|
|
28
|
-
const mongoose = require('mongoose');
|
|
29
41
|
await mongoose.connection.close(false);
|
|
30
42
|
logger.info('MongoDB connection closed.');
|
|
31
43
|
<%_ } else { -%>
|
|
32
|
-
const sequelize = require('<%- dbPath %>');
|
|
33
44
|
await sequelize.close();
|
|
34
45
|
logger.info('Database connection closed.');
|
|
35
46
|
<%_ } -%>
|
|
36
47
|
<%_ } -%>
|
|
37
48
|
<%_ if (caching === 'Redis') { -%>
|
|
38
|
-
const redisService = require('<%- redisPath %>');
|
|
39
49
|
await redisService.quit();
|
|
40
50
|
logger.info('Redis connection closed.');
|
|
41
51
|
<%_ } -%>
|
|
42
52
|
<%_ if (communication === 'Kafka') { -%>
|
|
43
|
-
const { disconnectKafka } = require('<%- kafkaPath %>');
|
|
44
53
|
await disconnectKafka();
|
|
45
54
|
logger.info('Kafka connection closed.');
|
|
46
55
|
<%_ } -%>
|
|
@@ -62,4 +71,4 @@ const setupGracefulShutdown = (server) => {
|
|
|
62
71
|
process.on('SIGINT', () => gracefulShutdown('SIGINT'));
|
|
63
72
|
};
|
|
64
73
|
|
|
65
|
-
|
|
74
|
+
export default setupGracefulShutdown;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { betterAuth } from "better-auth";
|
|
2
2
|
<% if (database === 'MongoDB') { %>import { mongodbAdapter } from "better-auth/adapters/mongodb";
|
|
3
3
|
import mongoose from "mongoose";<% } else if (database === 'MySQL' || database === 'PostgreSQL') { %>import { sequelizeAdapter } from "better-auth/adapters/sequelize";
|
|
4
|
-
import sequelize from "./database";<% } %>
|
|
4
|
+
import sequelize from "./database<% if (language === 'TypeScript') { %>.ts<% } else { %>.js<% } %>";<% } %>
|
|
5
5
|
|
|
6
6
|
export const auth = betterAuth({
|
|
7
7
|
database: <% if (database === 'MongoDB') { %>mongodbAdapter(mongoose.connection.db)<% } else if (database === 'MySQL' || database === 'PostgreSQL') { %>sequelizeAdapter(sequelize)<% } else { %>null // Better-Auth requires a database adapter<% } %>,
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
<% if (auth === 'Better-Auth') { %>
|
|
2
|
-
import { auth } from '../config/auth<% if (language === 'TypeScript') { %>.ts<% } %>';
|
|
2
|
+
import { auth } from '../config/auth<% if (language === 'TypeScript') { %>.ts<% } else { %>.js<% } %>';
|
|
3
3
|
|
|
4
4
|
export const register = async (req, res, next) => {
|
|
5
5
|
try {
|
|
@@ -34,7 +34,7 @@ export const login = async (req, res, next) => {
|
|
|
34
34
|
<% } else if (auth === 'JWT') { %>
|
|
35
35
|
import jwt from 'jsonwebtoken';
|
|
36
36
|
import bcrypt from 'bcryptjs';
|
|
37
|
-
import { ApiError } from '../utils/errorMiddleware<% if (language === 'TypeScript') { %>.ts<% } %>';
|
|
37
|
+
import { ApiError } from '../utils/errorMiddleware<% if (language === 'TypeScript') { %>.ts<% } else { %>.js<% } %>';
|
|
38
38
|
|
|
39
39
|
// Mock User database (In production, use your database models)
|
|
40
40
|
const users = [];
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import jwt from 'jsonwebtoken';
|
|
2
|
-
import { ApiError } from '../utils/errorMiddleware<% if (language === 'TypeScript') { %>.ts<% } %>';
|
|
2
|
+
import { ApiError } from '../utils/errorMiddleware<% if (language === 'TypeScript') { %>.ts<% } else { %>.js<% } %>';
|
|
3
3
|
|
|
4
4
|
export const authMiddleware = (req, res, next) => {
|
|
5
5
|
const authHeader = req.headers.authorization;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import multer from 'multer';
|
|
2
2
|
import path from 'path';
|
|
3
|
-
import { ApiError } from '../utils/errorMiddleware<% if (language === 'TypeScript') { %>.ts<% } %>';
|
|
3
|
+
import { ApiError } from '../utils/errorMiddleware<% if (language === 'TypeScript') { %>.ts<% } else { %>.js<% } %>';
|
|
4
4
|
|
|
5
5
|
// Storage configuration
|
|
6
6
|
const storage = multer.diskStorage({
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import express from 'express';
|
|
2
2
|
<% if (auth === 'OAuth' || googleLogin === 'Google Login') { %>import passport from 'passport';<% } %>
|
|
3
|
-
import * as authController from '../controllers/authController<% if (language === 'TypeScript') { %>.ts<% } %>';
|
|
3
|
+
import * as authController from '../controllers/authController<% if (language === 'TypeScript') { %>.ts<% } else { %>.js<% } %>';
|
|
4
4
|
|
|
5
5
|
const router = express.Router();
|
|
6
6
|
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import logger from '../utils/logger.js';
|
|
3
|
+
import dotenv from 'dotenv';
|
|
3
4
|
|
|
4
5
|
if (process.env.NODE_ENV !== 'production') {
|
|
5
|
-
|
|
6
|
+
dotenv.config();
|
|
6
7
|
}
|
|
7
8
|
const envSchema = z.object({
|
|
8
9
|
NODE_ENV: z.enum(['development', 'production', 'test']).default('development'),
|
|
@@ -43,4 +44,4 @@ if (!_env.success) {
|
|
|
43
44
|
|
|
44
45
|
const env = _env.data;
|
|
45
46
|
|
|
46
|
-
|
|
47
|
+
export { env };
|
|
@@ -1,6 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import path from 'path';
|
|
2
|
+
import YAML from 'yamljs';
|
|
3
|
+
import { fileURLToPath } from 'url';
|
|
4
|
+
|
|
5
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
6
|
+
const __dirname = path.dirname(__filename);
|
|
3
7
|
|
|
4
8
|
const swaggerDocument = YAML.load(path.join(__dirname, 'swagger.yml'));
|
|
5
9
|
|
|
6
|
-
|
|
10
|
+
export default swaggerDocument;
|
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import User from '../models/User.js';
|
|
2
|
+
import ERROR_MESSAGES from '../utils/errorMessages.js';
|
|
3
3
|
<% if (communication !== 'GraphQL') { -%>
|
|
4
|
-
|
|
4
|
+
import HTTP_STATUS from '../utils/httpCodes.js';
|
|
5
5
|
<% } -%>
|
|
6
|
-
|
|
6
|
+
import logger from '../utils/logger.js';
|
|
7
7
|
<%_ if (caching === 'Redis') { -%>
|
|
8
|
-
|
|
8
|
+
import cacheService from '../config/redisClient.js';
|
|
9
9
|
<%_ } else if (caching === 'Memory Cache') { -%>
|
|
10
|
-
|
|
10
|
+
import cacheService from '../config/memoryCache.js';
|
|
11
11
|
<%_ } -%>
|
|
12
12
|
<%_ if (communication === 'Kafka') { -%>
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
import { sendMessage } from '../services/kafkaService.js';
|
|
14
|
+
import { KAFKA_ACTIONS } from '../utils/kafkaEvents.js';
|
|
15
15
|
<%_ } -%>
|
|
16
16
|
<%_ if (auth && auth !== 'None') { _%>
|
|
17
|
-
|
|
17
|
+
import bcrypt from 'bcryptjs';
|
|
18
18
|
<%_ } _%>
|
|
19
19
|
|
|
20
20
|
<% if (communication === 'GraphQL') { -%>
|
|
21
|
-
const getUsers = async () => {
|
|
21
|
+
export const getUsers = async () => {
|
|
22
22
|
try {
|
|
23
23
|
<%_ if (caching === 'Redis' || caching === 'Memory Cache') { -%>
|
|
24
24
|
const users = await cacheService.getOrSet('users:all', async () => {
|
|
@@ -42,7 +42,7 @@ const getUsers = async () => {
|
|
|
42
42
|
}
|
|
43
43
|
};
|
|
44
44
|
|
|
45
|
-
const createUser = async (data) => {
|
|
45
|
+
export const createUser = async (data) => {
|
|
46
46
|
try {
|
|
47
47
|
const { name, email, password, imageUrl } = data;
|
|
48
48
|
const userData = { name, email };
|
|
@@ -73,7 +73,7 @@ const createUser = async (data) => {
|
|
|
73
73
|
}
|
|
74
74
|
};
|
|
75
75
|
|
|
76
|
-
const updateUser = async (id, data) => {
|
|
76
|
+
export const updateUser = async (id, data) => {
|
|
77
77
|
try {
|
|
78
78
|
<%_ if (database === 'MongoDB') { -%>
|
|
79
79
|
const updatedUser = await User.findByIdAndUpdate(id, data, { new: true });
|
|
@@ -105,7 +105,7 @@ const updateUser = async (id, data) => {
|
|
|
105
105
|
}
|
|
106
106
|
};
|
|
107
107
|
|
|
108
|
-
const deleteUser = async (id) => {
|
|
108
|
+
export const deleteUser = async (id) => {
|
|
109
109
|
try {
|
|
110
110
|
<%_ if (database === 'MongoDB') { -%>
|
|
111
111
|
const deleted = await User.findByIdAndDelete(id);
|
|
@@ -133,10 +133,8 @@ const deleteUser = async (id) => {
|
|
|
133
133
|
throw error;
|
|
134
134
|
}
|
|
135
135
|
};
|
|
136
|
-
|
|
137
|
-
module.exports = { getUsers, createUser, updateUser, deleteUser };
|
|
138
136
|
<% } else { -%>
|
|
139
|
-
const getUsers = async (req, res, next) => {
|
|
137
|
+
export const getUsers = async (req, res, next) => {
|
|
140
138
|
try {
|
|
141
139
|
<%_ if (caching === 'Redis' || caching === 'Memory Cache') { -%>
|
|
142
140
|
const users = await cacheService.getOrSet('users:all', async () => {
|
|
@@ -160,7 +158,7 @@ const getUsers = async (req, res, next) => {
|
|
|
160
158
|
}
|
|
161
159
|
};
|
|
162
160
|
|
|
163
|
-
const createUser = async (req, res, next) => {
|
|
161
|
+
export const createUser = async (req, res, next) => {
|
|
164
162
|
try {
|
|
165
163
|
const { name, email, password } = req.body || {};
|
|
166
164
|
const userData = { name, email };
|
|
@@ -194,7 +192,7 @@ const createUser = async (req, res, next) => {
|
|
|
194
192
|
}
|
|
195
193
|
};
|
|
196
194
|
|
|
197
|
-
const updateUser = async (req, res, next) => {
|
|
195
|
+
export const updateUser = async (req, res, next) => {
|
|
198
196
|
try {
|
|
199
197
|
const { id } = req.params;
|
|
200
198
|
const { name, email, password } = req.body || {};
|
|
@@ -248,7 +246,7 @@ const updateUser = async (req, res, next) => {
|
|
|
248
246
|
}
|
|
249
247
|
};
|
|
250
248
|
|
|
251
|
-
const deleteUser = async (req, res, next) => {
|
|
249
|
+
export const deleteUser = async (req, res, next) => {
|
|
252
250
|
try {
|
|
253
251
|
const { id } = req.params;
|
|
254
252
|
<%_ if (database === 'MongoDB') { -%>
|
|
@@ -283,6 +281,4 @@ const deleteUser = async (req, res, next) => {
|
|
|
283
281
|
next(error);
|
|
284
282
|
}
|
|
285
283
|
};
|
|
286
|
-
|
|
287
|
-
module.exports = { getUsers, createUser, updateUser, deleteUser };
|
|
288
284
|
<% } -%>
|
|
@@ -1,34 +1,38 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
<%_ if (communication === 'REST APIs' || communication === 'Kafka') { -%>
|
|
5
|
-
|
|
6
|
-
<%_ if (communication === 'Kafka') { -%>
|
|
1
|
+
import { env } from './config/env.js';
|
|
2
|
+
import express from 'express';
|
|
3
|
+
import cors from 'cors';
|
|
4
|
+
<%_ if (communication === 'REST APIs' || communication === 'Kafka') { -%>import apiRoutes from './routes/api.js';<%_ } %>
|
|
5
|
+
import healthRoutes from './routes/healthRoute.js';
|
|
6
|
+
<%_ if (communication === 'Kafka') { -%>import { connectKafka, sendMessage } from './services/kafkaService.js';<%_ } -%>
|
|
7
7
|
<%_ if (communication === 'GraphQL') { -%>
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
8
|
+
import { ApolloServer } from '@apollo/server';
|
|
9
|
+
import { expressMiddleware } from '@as-integrations/express4';
|
|
10
|
+
import { ApolloServerPluginLandingPageLocalDefault } from '@apollo/server/plugin/landingPage/default';
|
|
11
|
+
import { unwrapResolverError } from '@apollo/server/errors';
|
|
12
|
+
import { ApiError } from './errors/ApiError.js';
|
|
13
|
+
import { typeDefs, resolvers } from './graphql/index.js';
|
|
14
|
+
import { gqlContext } from './graphql/context.js';
|
|
15
15
|
<% } %>
|
|
16
16
|
<%_ if (communication === 'REST APIs' || communication === 'Kafka') { -%>
|
|
17
|
-
|
|
18
|
-
|
|
17
|
+
import swaggerUi from 'swagger-ui-express';
|
|
18
|
+
import swaggerSpecs from './config/swagger.js';
|
|
19
19
|
<%_ } -%>
|
|
20
|
-
|
|
20
|
+
import setupGracefulShutdown from './utils/gracefulShutdown.js';
|
|
21
|
+
import logger from './utils/logger.js';
|
|
22
|
+
import morgan from 'morgan';
|
|
23
|
+
import { errorMiddleware } from './utils/errorMiddleware.js';
|
|
24
|
+
import path from 'path';
|
|
25
|
+
import { fileURLToPath } from 'url';
|
|
26
|
+
|
|
27
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
28
|
+
const __dirname = path.dirname(__filename);
|
|
21
29
|
|
|
22
30
|
const app = express();
|
|
23
31
|
const PORT = env.PORT;
|
|
24
|
-
const logger = require('./utils/logger');
|
|
25
|
-
const morgan = require('morgan');
|
|
26
|
-
const { errorMiddleware } = require('./utils/errorMiddleware');
|
|
27
32
|
|
|
28
33
|
app.use(cors());
|
|
29
34
|
app.use(express.json());
|
|
30
35
|
<% if (includeMulter) { %>
|
|
31
|
-
const path = require('path');
|
|
32
36
|
app.use('/uploads', express.static(path.join(__dirname, '../uploads')));
|
|
33
37
|
<% } %>
|
|
34
38
|
app.use(morgan('combined', { stream: { write: message => logger.info(message.trim()) } }));
|
|
@@ -38,7 +42,6 @@ app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpecs));
|
|
|
38
42
|
<%_ } -%>
|
|
39
43
|
<%_ if (viewEngine === 'EJS' || viewEngine === 'Pug') { -%>
|
|
40
44
|
// View Engine Setup
|
|
41
|
-
const path = require('path');
|
|
42
45
|
app.set('views', path.join(__dirname, 'views'));
|
|
43
46
|
app.set('view engine', '<%= viewEngine.toLowerCase() %>');
|
|
44
47
|
app.use(express.static(path.join(__dirname, '../public')));<%_ } %>
|
|
@@ -106,12 +109,10 @@ const startServer = async () => {
|
|
|
106
109
|
|
|
107
110
|
<%_ if (database !== 'None') { -%>
|
|
108
111
|
// Database Sync
|
|
109
|
-
<%_ if (database !== 'None') { -%>
|
|
110
112
|
<%_ if (database === 'MongoDB') { -%>
|
|
111
|
-
|
|
113
|
+
import connectDB from './config/database.js';
|
|
112
114
|
<%_ } else { -%>
|
|
113
|
-
|
|
114
|
-
<%_ } -%>
|
|
115
|
+
import sequelize from './config/database.js';
|
|
115
116
|
<%_ } -%>
|
|
116
117
|
const syncDatabase = async () => {
|
|
117
118
|
let retries = 30;
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
|
|
1
|
+
import express from 'express';
|
|
2
|
+
import userController from '../controllers/userController.js';
|
|
3
|
+
<% if (auth && auth !== 'None') { %>import { authMiddleware } from '../middleware/auth.js';<% } %>
|
|
4
|
+
<% if (includeMulter) { %>import { upload } from '../middleware/upload.js';<% } %>
|
|
5
|
+
<% if (auth && auth !== 'None') { %>import authRoutes from './authRoutes.js';<% } %>
|
|
6
|
+
|
|
2
7
|
const router = express.Router();
|
|
3
|
-
const userController = require('../controllers/userController');
|
|
4
|
-
<% if (auth && auth !== 'None') { %>const { authMiddleware } = require('../middleware/auth');<% } %>
|
|
5
|
-
<% if (includeMulter) { %>const { upload } = require('../middleware/upload');<% } %>
|
|
6
|
-
<% if (auth && auth !== 'None') { %>const authRoutes = require('./authRoutes');<% } %>
|
|
7
8
|
|
|
8
9
|
<% if (auth && auth !== 'None') { %>router.use('/auth', authRoutes);<% } %>
|
|
9
10
|
|
|
@@ -12,4 +13,4 @@ router.post('/users', <% if (auth && auth !== 'None') { %>authMiddleware, <% } %
|
|
|
12
13
|
router.patch('/users/:id', <% if (auth && auth !== 'None') { %>authMiddleware, <% } %><% if (includeMulter) { %>upload.single('image'), <% } %>userController.updateUser);
|
|
13
14
|
router.delete('/users/:id', <% if (auth && auth !== 'None') { %>authMiddleware, <% } %>userController.deleteUser);
|
|
14
15
|
|
|
15
|
-
|
|
16
|
+
export default router;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import logger from './logger.js';
|
|
2
|
+
import { ApiError } from '../errors/ApiError.js';
|
|
3
|
+
import HTTP_STATUS from './httpCodes.js';
|
|
4
4
|
|
|
5
5
|
// eslint-disable-next-line no-unused-vars
|
|
6
6
|
const errorMiddleware = (err, req, res, next) => {
|
|
@@ -26,4 +26,4 @@ const errorMiddleware = (err, req, res, next) => {
|
|
|
26
26
|
});
|
|
27
27
|
};
|
|
28
28
|
|
|
29
|
-
|
|
29
|
+
export { errorMiddleware };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import winston from 'winston';
|
|
2
|
+
import 'winston-daily-rotate-file';
|
|
3
3
|
|
|
4
4
|
const logger = winston.createLogger({
|
|
5
5
|
level: 'info',
|
|
@@ -37,4 +37,4 @@ logger.add(new winston.transports.Console({
|
|
|
37
37
|
: winston.format.json(),
|
|
38
38
|
}));
|
|
39
39
|
|
|
40
|
-
|
|
40
|
+
export default logger;
|