-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
124 lines (109 loc) · 3.82 KB
/
Copy pathapp.js
File metadata and controls
124 lines (109 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
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
const express = require("express");
const helmet = require("helmet");
const cors = require('cors');
const fs = require("fs");
const YAML = require("js-yaml");
const path = require("path");
const swaggerUi = require("swagger-ui-express");
const session = require('express-session');
// const cron = require("node-cron");
const routes = require("./src/routes/index");
const errorHandler = require("./src/middlewares/errorHandler");
const { loadConfigsToCache } = require("./src/services/config.service");
const logger = require("./src/config/logger");
const { monitoringMiddleware, healthHandler, metricsHandler } = require("./src/middlewares/monitoring");
const client = require("./src/config/redis");
const passport = require("./src/config/passport");
require("dotenv").config();
const app = express();
app.use(express.json());
// Session configuration
app.use(session({
secret: process.env.SESSION_SECRET || "your-secret-key",
resave: false,
saveUninitialized: false,
cookie: {
secure: process.env.NODE_ENV === "production",
httpOnly: true,
maxAge: 24 * 60 * 60 * 1000, // 24 hours
},
}));
// Passport initialization
app.use(passport.initialize());
app.use(passport.session());
// Security headers
// app.use(
// helmet({
// contentSecurityPolicy: {
// useDefaults: false,
// directives: {
// defaultSrc: ["'self'", "http://localhost:*"],
// connectSrc: [
// "'self'",
// "http://localhost:*",
// "https://www.facebook.com",
// "https://graph.facebook.com",
// "https://connect.facebook.net",
// "https://accounts.google.com",
// "https://oauth2.googleapis.com",
// "https://www.googleapis.com",
// ],
// scriptSrc: ["'self'", "'unsafe-inline'", "https://connect.facebook.net"],
// styleSrc: ["'self'", "'unsafe-inline'"],
// imgSrc: ["'self'", "data:"],
// frameSrc: ["'self'", "https://www.facebook.com"],
// },
// },
// crossOriginOpenerPolicy: { policy: "same-origin-allow-popups" },
// })
// );
// app.use(cors({
// origin: process.env.CLIENT_URL || 'http://localhost:*',
// methods: ['GET', 'POST', 'PUT', 'DELETE'],
// credentials: true,
// }));
app.use(monitoringMiddleware);
app.get("/health", healthHandler);
app.get("/metrics", metricsHandler);
// Serve static frontend for OAuth testing BEFORE API routes and error handler
app.use(express.static(path.join(__dirname, "./src/views")));
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "./src/views/login.html"));
});
// cron.schedule("*/10 * * * * *", () => logger.info("Running every 10s"), { timezone: "Asia/Kolkata" });
// cron.schedule("0 9 * * *", () => logger.info("Good morning! 9:00 AM IST"), { timezone: "Asia/Kolkata" });
// cron.schedule("30 2 * * *", () => logger.info("New York 2:30 AM local time"), { timezone: "America/New_York" });
app.use(errorHandler);
app.use("/api", routes);
const swaggerFile = path.join(__dirname, "./src/api-docs/openapi.yaml");
const swaggerDocument = YAML.load(fs.readFileSync(swaggerFile, "utf8"));
app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerDocument));
(async () => {
try {
await loadConfigsToCache();
logger.info("Config cache loaded at startup");
} catch (err) {
logger.error("Failed to load config cache", err);
process.exit(1);
}
})();
(async () => {
try {
await client.connect();
logger.info("Redis client connected");
} catch (error) {
logger.error("Failed to connect to Redis", error);
process.exit(1);
}
})();
process.on("SIGINT", async () => {
logger.info("Shutting down gracefully Redis disconnected");
await client.quit();
process.exit(0);
});
module.exports = app;
// logger.info("info");
// logger.warn("warning");
// logger.error("error");
// logger.debug("debug");
// logger.http("http log");