@ifecodes/backend-template 1.1.8 → 1.4.0
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/LICENSE +201 -0
- package/README.md +423 -365
- package/bin/cli.js +1276 -836
- package/bin/lib/microservice-config.js +155 -150
- package/bin/lib/prompts.js +277 -241
- package/bin/lib/readme-generator.js +364 -335
- package/bin/lib/service-setup.js +901 -679
- package/package.json +64 -55
- package/template/base/js/.env.example +5 -5
- package/template/base/js/.eslintrc.json +10 -13
- package/template/base/js/.husky/pre-commit +1 -7
- package/template/base/js/.prettierrc +7 -7
- package/template/base/js/eslint.config.js +33 -31
- package/template/base/js/package.json +29 -28
- package/template/base/js/src/app.js +20 -15
- package/template/base/js/src/config/env.js +32 -2
- package/template/base/js/src/config/index.js +2 -2
- package/template/base/js/src/docs/index.js +5 -0
- package/template/base/js/src/docs/route-registry.js +63 -0
- package/template/base/js/src/middlewares/error-handler.middleware.js +22 -0
- package/template/base/js/src/middlewares/index.js +9 -3
- package/template/base/js/src/middlewares/method-not-allowed.middleware.js +8 -2
- package/template/base/js/src/middlewares/not-found.middleware.js +4 -1
- package/template/base/js/src/middlewares/observability.middleware.js +24 -0
- package/template/base/js/src/middlewares/root.middleware.js +7 -5
- package/template/base/js/src/middlewares/validation.middleware.js +39 -0
- package/template/base/js/src/modules/index.js +8 -8
- package/template/base/js/src/modules/v1/health/health.controller.auth.js +29 -0
- package/template/base/js/src/modules/v1/health/health.controller.js +4 -4
- package/template/base/js/src/modules/v1/health/health.route.js +70 -5
- package/template/base/js/src/modules/v1/health/index.js +1 -1
- package/template/base/js/src/modules/v1/index.js +3 -3
- package/template/base/js/src/routes.js +13 -6
- package/template/base/js/src/server.js +18 -18
- package/template/base/js/src/utils/http-error.js +27 -6
- package/template/base/js/src/utils/index.js +28 -22
- package/template/base/js/src/utils/logger.js +57 -67
- package/template/base/ts/.eslintrc.json +13 -17
- package/template/base/ts/.prettierrc +7 -7
- package/template/base/ts/eslint.config.js +33 -33
- package/template/base/ts/package.json +41 -39
- package/template/base/ts/src/app.ts +20 -15
- package/template/base/ts/src/config/db.ts +4 -4
- package/template/base/ts/src/config/env.ts +40 -10
- package/template/base/ts/src/config/index.ts +2 -2
- package/template/base/ts/src/docs/index.ts +3 -0
- package/template/base/ts/src/docs/route-registry.ts +98 -0
- package/template/base/ts/src/middlewares/error-handler.middleware.ts +26 -0
- package/template/base/ts/src/middlewares/index.ts +6 -3
- package/template/base/ts/src/middlewares/method-not-allowed.middleware.ts +23 -16
- package/template/base/ts/src/middlewares/not-found.middleware.ts +10 -8
- package/template/base/ts/src/middlewares/observability.middleware.ts +25 -0
- package/template/base/ts/src/middlewares/root.middleware.ts +16 -14
- package/template/base/ts/src/middlewares/validation.middleware.ts +46 -0
- package/template/base/ts/src/modules/index.ts +8 -8
- package/template/base/ts/src/modules/v1/health/health.controller.auth.ts +26 -0
- package/template/base/ts/src/modules/v1/health/health.controller.ts +18 -18
- package/template/base/ts/src/modules/v1/health/health.route.ts +68 -9
- package/template/base/ts/src/modules/v1/health/index.ts +1 -1
- package/template/base/ts/src/modules/v1/index.ts +8 -8
- package/template/base/ts/src/routes.ts +23 -15
- package/template/base/ts/src/server.ts +19 -19
- package/template/base/ts/src/utils/http-error.ts +63 -45
- package/template/base/ts/src/utils/index.ts +14 -11
- package/template/base/ts/src/utils/logger.ts +58 -68
- package/template/base/ts/tsconfig.json +21 -22
- package/template/features/auth/argon2/inject.js +50 -50
- package/template/features/auth/base/health-openapi.ts +62 -0
- package/template/features/auth/base/inject.js +174 -172
- package/template/features/auth/bcrypt/inject.js +40 -40
- package/template/features/auth/models/user.model.js +24 -24
- package/template/features/auth/models/user.model.ts +1 -1
- package/template/features/auth/modules/auth.controller.js +21 -21
- package/template/features/auth/modules/auth.controller.ts +28 -20
- package/template/features/auth/modules/auth.routes.js +89 -10
- package/template/features/auth/modules/auth.routes.ts +86 -11
- package/template/features/auth/modules/auth.service.js +29 -29
- package/template/features/auth/modules/auth.service.ts +38 -38
- package/template/features/auth/modules/index.js +1 -1
- package/template/features/auth/utils/hash.ts +20 -20
- package/template/features/auth/utils/jwt.js +12 -12
- package/template/features/cors/inject.js +14 -13
- package/template/features/helmet/inject.js +7 -6
- package/template/features/morgan/inject.js +8 -7
- package/template/features/rate-limit/inject.js +7 -6
- package/template/gateway/js/app.js +42 -42
- package/template/gateway/js/inject.js +33 -31
- package/template/gateway/js/server.js +19 -19
- package/template/gateway/ts/app.ts +43 -43
- package/template/gateway/ts/inject.js +33 -33
- package/template/gateway/ts/server.ts +19 -19
- package/template/microservice/docker/docker-compose.yml +5 -5
- package/template/microservice/nodocker/pm2.config.js +3 -3
|
@@ -1,150 +1,155 @@
|
|
|
1
|
-
import fs from "fs";
|
|
2
|
-
import path from "path";
|
|
3
|
-
import { fileURLToPath } from "url";
|
|
4
|
-
|
|
5
|
-
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
6
|
-
|
|
7
|
-
export const generateDockerCompose = (target, allServices, projectname) => {
|
|
8
|
-
const dockerCompose = {
|
|
9
|
-
services: {},
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
// Build environment variables map for all services
|
|
13
|
-
const allServicePorts = allServices.map((service, index) => {
|
|
14
|
-
const isGateway = service === "gateway";
|
|
15
|
-
const port = isGateway
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
`
|
|
61
|
-
`
|
|
62
|
-
`
|
|
63
|
-
`
|
|
64
|
-
`
|
|
65
|
-
|
|
66
|
-
`
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
"\n
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
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
|
-
|
|
119
|
-
|
|
120
|
-
|
|
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
|
-
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { fileURLToPath } from "url";
|
|
4
|
+
|
|
5
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
6
|
+
|
|
7
|
+
export const generateDockerCompose = (target, allServices, projectname) => {
|
|
8
|
+
const dockerCompose = {
|
|
9
|
+
services: {},
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
// Build environment variables map for all services
|
|
13
|
+
const allServicePorts = allServices.map((service, index) => {
|
|
14
|
+
const isGateway = service === "gateway";
|
|
15
|
+
const port = isGateway
|
|
16
|
+
? 4000
|
|
17
|
+
: 4001 +
|
|
18
|
+
allServices.filter((s, i) => s !== "gateway" && i < index).length;
|
|
19
|
+
const envVarName = `${service.toUpperCase().replace(/-/g, "_")}_PORT`;
|
|
20
|
+
return { service, port, envVarName };
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
for (const serviceName of allServices) {
|
|
24
|
+
// Gateway runs on 4000, other services start from 4001
|
|
25
|
+
const serviceInfo = allServicePorts.find((s) => s.service === serviceName);
|
|
26
|
+
const port = serviceInfo.port;
|
|
27
|
+
const envVarName = serviceInfo.envVarName;
|
|
28
|
+
|
|
29
|
+
// Build environment variables array - include all service ports
|
|
30
|
+
const environmentVars = [
|
|
31
|
+
`NODE_ENV=\${NODE_ENV:-development}`,
|
|
32
|
+
...allServicePorts.map(
|
|
33
|
+
(s) => `${s.envVarName}=\${${s.envVarName}:-${s.port}}`
|
|
34
|
+
),
|
|
35
|
+
];
|
|
36
|
+
|
|
37
|
+
dockerCompose.services[serviceName] = {
|
|
38
|
+
build: {
|
|
39
|
+
context: `./services/${serviceName}`,
|
|
40
|
+
dockerfile: "Dockerfile",
|
|
41
|
+
},
|
|
42
|
+
image: `${serviceName}:latest`,
|
|
43
|
+
container_name: projectname + "_" + serviceName,
|
|
44
|
+
ports: [`\${${envVarName}:-${port}}:\${${envVarName}:-${port}}`],
|
|
45
|
+
environment: environmentVars,
|
|
46
|
+
volumes: [
|
|
47
|
+
`./services/${serviceName}:/app`,
|
|
48
|
+
`./shared:/app/shared`,
|
|
49
|
+
`/app/node_modules`,
|
|
50
|
+
],
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
fs.writeFileSync(
|
|
55
|
+
path.join(target, "docker-compose.yml"),
|
|
56
|
+
`services:\n` +
|
|
57
|
+
Object.entries(dockerCompose.services)
|
|
58
|
+
.map(
|
|
59
|
+
([name, config]) =>
|
|
60
|
+
` ${name}:\n` +
|
|
61
|
+
` build:\n` +
|
|
62
|
+
` context: ${config.build.context}\n` +
|
|
63
|
+
` dockerfile: ${config.build.dockerfile}\n` +
|
|
64
|
+
` image: ${config.image}\n` +
|
|
65
|
+
` container_name: ${config.container_name}\n` +
|
|
66
|
+
` ports:\n - "${config.ports[0]}"\n` +
|
|
67
|
+
` environment:\n` +
|
|
68
|
+
config.environment.map((e) => ` - ${e}`).join("\n") +
|
|
69
|
+
"\n" +
|
|
70
|
+
` volumes:\n` +
|
|
71
|
+
config.volumes.map((v) => ` - ${v}`).join("\n")
|
|
72
|
+
)
|
|
73
|
+
.join("\n") +
|
|
74
|
+
"\n\n" +
|
|
75
|
+
// Add x-watch section to help file watchers map service src and shared folders
|
|
76
|
+
`x-watch:\n` +
|
|
77
|
+
allServices
|
|
78
|
+
.map(
|
|
79
|
+
(name) => ` ${name}:\n - ./services/${name}/src\n - ./shared`
|
|
80
|
+
)
|
|
81
|
+
.join("\n")
|
|
82
|
+
);
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
export const generatePm2Config = (target, allServices) => {
|
|
86
|
+
const pm2Config = {
|
|
87
|
+
apps: allServices.map((serviceName, index) => {
|
|
88
|
+
const isGateway = serviceName === "gateway";
|
|
89
|
+
const port = isGateway
|
|
90
|
+
? 4000
|
|
91
|
+
: 4001 +
|
|
92
|
+
allServices.filter((s, i) => s !== "gateway" && i < index).length;
|
|
93
|
+
|
|
94
|
+
return {
|
|
95
|
+
name: serviceName,
|
|
96
|
+
script: `./services/${serviceName}/src/server.ts`,
|
|
97
|
+
interpreter: "node",
|
|
98
|
+
node_args:
|
|
99
|
+
"-r ts-node/register/transpile-only -r tsconfig-paths/register",
|
|
100
|
+
watch: true,
|
|
101
|
+
ignore_watch: ["node_modules", ".git", "logs"],
|
|
102
|
+
watch_delay: 1000,
|
|
103
|
+
instances: 1,
|
|
104
|
+
exec_mode: "fork",
|
|
105
|
+
env: {
|
|
106
|
+
PORT: port,
|
|
107
|
+
TS_NODE_PROJECT: `services/${serviceName}/tsconfig.json`,
|
|
108
|
+
},
|
|
109
|
+
};
|
|
110
|
+
}),
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
fs.writeFileSync(
|
|
114
|
+
path.join(target, "pm2.config.js"),
|
|
115
|
+
`module.exports = ${JSON.stringify(pm2Config, null, 2)};\n`
|
|
116
|
+
);
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
export const copyDockerfile = (target, servicesToCreate) => {
|
|
120
|
+
const dockerfilePath = path.join(
|
|
121
|
+
__dirname,
|
|
122
|
+
"../../template/microservice/docker/Dockerfile"
|
|
123
|
+
);
|
|
124
|
+
|
|
125
|
+
for (const serviceName of servicesToCreate) {
|
|
126
|
+
const serviceDockerfile = path.join(
|
|
127
|
+
target,
|
|
128
|
+
"services",
|
|
129
|
+
serviceName,
|
|
130
|
+
"Dockerfile"
|
|
131
|
+
);
|
|
132
|
+
if (!fs.existsSync(serviceDockerfile)) {
|
|
133
|
+
fs.copyFileSync(dockerfilePath, serviceDockerfile);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
export const copyDockerignore = (target, servicesToCreate) => {
|
|
139
|
+
const dockerignorePath = path.join(
|
|
140
|
+
__dirname,
|
|
141
|
+
"../../template/microservice/docker/.dockerignore"
|
|
142
|
+
);
|
|
143
|
+
|
|
144
|
+
for (const serviceName of servicesToCreate) {
|
|
145
|
+
const serviceDockerignore = path.join(
|
|
146
|
+
target,
|
|
147
|
+
"services",
|
|
148
|
+
serviceName,
|
|
149
|
+
".dockerignore"
|
|
150
|
+
);
|
|
151
|
+
if (!fs.existsSync(serviceDockerignore)) {
|
|
152
|
+
fs.copyFileSync(dockerignorePath, serviceDockerignore);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
};
|