@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,43 +1,43 @@
|
|
|
1
|
-
import express, { Request, Response, NextFunction } from "express";
|
|
2
|
-
import { createProxyMiddleware } from "http-proxy-middleware";
|
|
3
|
-
import { ENV } from "@/shared/config";
|
|
4
|
-
import { logger } from "@/shared/utils";
|
|
5
|
-
|
|
6
|
-
const app = express();
|
|
7
|
-
|
|
8
|
-
// Root endpoint
|
|
9
|
-
app.get("/", (_req: Request, res: Response) => {
|
|
10
|
-
logger.info("Gateway", "Root endpoint accessed");
|
|
11
|
-
|
|
12
|
-
res.json({
|
|
13
|
-
status: "ok",
|
|
14
|
-
service: "API Gateway",
|
|
15
|
-
version: "1.0.0",
|
|
16
|
-
endpoints: {
|
|
17
|
-
health: "/health",
|
|
18
|
-
healthService: "/api/v1/health"
|
|
19
|
-
}
|
|
20
|
-
});
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
// Health check for the gateway itself
|
|
24
|
-
app.get("/health", (_req: Request, res: Response) => {
|
|
25
|
-
logger.info("Gateway", "Health check accessed");
|
|
26
|
-
res.json({ status: "ok", service: "gateway" });
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
// Service routes - will be dynamically configured
|
|
30
|
-
/*__ROUTES__*/
|
|
31
|
-
|
|
32
|
-
// 404 handler
|
|
33
|
-
app.use((_req: Request, res: Response) => {
|
|
34
|
-
res.status(404).json({ error: "Route not found" });
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
// Error handler
|
|
38
|
-
app.use((err: Error, _req: Request, res: Response, next: NextFunction) => {
|
|
39
|
-
logger.error("Gateway error:", err);
|
|
40
|
-
res.status(500).json({ error: "Internal gateway error" });
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
export default app;
|
|
1
|
+
import express, { Request, Response, NextFunction } from "express";
|
|
2
|
+
import { createProxyMiddleware } from "http-proxy-middleware";
|
|
3
|
+
import { ENV } from "@/shared/config";
|
|
4
|
+
import { logger } from "@/shared/utils";
|
|
5
|
+
|
|
6
|
+
const app = express();
|
|
7
|
+
|
|
8
|
+
// Root endpoint
|
|
9
|
+
app.get("/", (_req: Request, res: Response) => {
|
|
10
|
+
logger.info("Gateway", "Root endpoint accessed");
|
|
11
|
+
|
|
12
|
+
res.json({
|
|
13
|
+
status: "ok",
|
|
14
|
+
service: "API Gateway",
|
|
15
|
+
version: "1.0.0",
|
|
16
|
+
endpoints: {
|
|
17
|
+
health: "/health",
|
|
18
|
+
healthService: "/api/v1/health",
|
|
19
|
+
},
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
// Health check for the gateway itself
|
|
24
|
+
app.get("/health", (_req: Request, res: Response) => {
|
|
25
|
+
logger.info("Gateway", "Health check accessed");
|
|
26
|
+
res.json({ status: "ok", service: "gateway" });
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
// Service routes - will be dynamically configured
|
|
30
|
+
/*__ROUTES__*/
|
|
31
|
+
|
|
32
|
+
// 404 handler
|
|
33
|
+
app.use((_req: Request, res: Response) => {
|
|
34
|
+
res.status(404).json({ error: "Route not found" });
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
// Error handler
|
|
38
|
+
app.use((err: Error, _req: Request, res: Response, next: NextFunction) => {
|
|
39
|
+
logger.error("Gateway error:", err);
|
|
40
|
+
res.status(500).json({ error: "Internal gateway error" });
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
export default app;
|
|
@@ -1,33 +1,33 @@
|
|
|
1
|
-
export const gatewayDeps = ["http-proxy-middleware"];
|
|
2
|
-
|
|
3
|
-
export const generateGatewayRoutes = (services, mode = "docker") => {
|
|
4
|
-
const routes = services
|
|
5
|
-
.filter((s) => s !== "gateway")
|
|
6
|
-
.map((service, index) => {
|
|
7
|
-
const servicePort = `${service.toUpperCase().replace(/-/g, "_")}_PORT`;
|
|
8
|
-
const port = 4001 + index; // Host port mapping: gateway=4000, services start at 4001
|
|
9
|
-
|
|
10
|
-
// Docker: use container name with internal port 4000
|
|
11
|
-
// Non-docker: use localhost with mapped host port
|
|
12
|
-
const host = mode === "docker" ? service : "localhost";
|
|
13
|
-
// Build a placeholder that will render as ${SERVICE_PORT} in the generated code
|
|
14
|
-
const servicePortPlaceholder = "${" + servicePort + "}";
|
|
15
|
-
|
|
16
|
-
return `
|
|
17
|
-
// Proxy to ${service}
|
|
18
|
-
const ${servicePort} = ENV.${servicePort} || ${port}
|
|
19
|
-
app.use("/api", createProxyMiddleware({
|
|
20
|
-
target: \`http://${host}:${servicePortPlaceholder}/api\`,
|
|
21
|
-
changeOrigin: true,
|
|
22
|
-
on: {
|
|
23
|
-
error: (err, req, res) => {
|
|
24
|
-
logger.error(\`Proxy error for ${service}:\`, err);
|
|
25
|
-
(res as Response).status(503).json({ error: "Service unavailable" });
|
|
26
|
-
},
|
|
27
|
-
},
|
|
28
|
-
}));`;
|
|
29
|
-
})
|
|
30
|
-
.join("\n");
|
|
31
|
-
|
|
32
|
-
return routes;
|
|
33
|
-
};
|
|
1
|
+
export const gatewayDeps = ["http-proxy-middleware"];
|
|
2
|
+
|
|
3
|
+
export const generateGatewayRoutes = (services, mode = "docker") => {
|
|
4
|
+
const routes = services
|
|
5
|
+
.filter((s) => s !== "gateway")
|
|
6
|
+
.map((service, index) => {
|
|
7
|
+
const servicePort = `${service.toUpperCase().replace(/-/g, "_")}_PORT`;
|
|
8
|
+
const port = 4001 + index; // Host port mapping: gateway=4000, services start at 4001
|
|
9
|
+
|
|
10
|
+
// Docker: use container name with internal port 4000
|
|
11
|
+
// Non-docker: use localhost with mapped host port
|
|
12
|
+
const host = mode === "docker" ? service : "localhost";
|
|
13
|
+
// Build a placeholder that will render as ${SERVICE_PORT} in the generated code
|
|
14
|
+
const servicePortPlaceholder = "${" + servicePort + "}";
|
|
15
|
+
|
|
16
|
+
return `
|
|
17
|
+
// Proxy to ${service}
|
|
18
|
+
const ${servicePort} = ENV.${servicePort} || ${port}
|
|
19
|
+
app.use("/api", createProxyMiddleware({
|
|
20
|
+
target: \`http://${host}:${servicePortPlaceholder}/api\`,
|
|
21
|
+
changeOrigin: true,
|
|
22
|
+
on: {
|
|
23
|
+
error: (err, req, res) => {
|
|
24
|
+
logger.error(\`Proxy error for ${service}:\`, err);
|
|
25
|
+
(res as Response).status(503).json({ error: "Service unavailable" });
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
}));`;
|
|
29
|
+
})
|
|
30
|
+
.join("\n");
|
|
31
|
+
|
|
32
|
+
return routes;
|
|
33
|
+
};
|
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
import http from "http";
|
|
2
|
-
import app from "./app";
|
|
3
|
-
import { logger } from "@/shared/utils";
|
|
4
|
-
import { ENV } from "@/shared/config";
|
|
5
|
-
|
|
6
|
-
const PORT = ENV.GATEWAY_PORT || 4000;
|
|
7
|
-
|
|
8
|
-
const server = http.createServer(app);
|
|
9
|
-
|
|
10
|
-
server.listen(PORT, () => {
|
|
11
|
-
logger.info(`🚀 API Gateway running on port ${PORT}`);
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
process.on("SIGTERM", () => {
|
|
15
|
-
logger.info("SIGTERM signal received: closing HTTP server");
|
|
16
|
-
server.close(() => {
|
|
17
|
-
logger.info("HTTP server closed");
|
|
18
|
-
});
|
|
19
|
-
});
|
|
1
|
+
import http from "http";
|
|
2
|
+
import app from "./app";
|
|
3
|
+
import { logger } from "@/shared/utils";
|
|
4
|
+
import { ENV } from "@/shared/config";
|
|
5
|
+
|
|
6
|
+
const PORT = ENV.GATEWAY_PORT || 4000;
|
|
7
|
+
|
|
8
|
+
const server = http.createServer(app);
|
|
9
|
+
|
|
10
|
+
server.listen(PORT, () => {
|
|
11
|
+
logger.info(`🚀 API Gateway running on port ${PORT}`);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
process.on("SIGTERM", () => {
|
|
15
|
+
logger.info("SIGTERM signal received: closing HTTP server");
|
|
16
|
+
server.close(() => {
|
|
17
|
+
logger.info("HTTP server closed");
|
|
18
|
+
});
|
|
19
|
+
});
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
services:
|
|
2
|
-
api:
|
|
3
|
-
build: .
|
|
4
|
-
ports:
|
|
5
|
-
- "4000:4000"
|
|
1
|
+
services:
|
|
2
|
+
api:
|
|
3
|
+
build: .
|
|
4
|
+
ports:
|
|
5
|
+
- "4000:4000"
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
module.exports = {
|
|
2
|
-
apps: [{ name: "api", script: "src/server.ts" }]
|
|
3
|
-
};
|
|
1
|
+
module.exports = {
|
|
2
|
+
apps: [{ name: "api", script: "src/server.ts" }],
|
|
3
|
+
};
|