-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
110 lines (96 loc) · 3.3 KB
/
server.js
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
import fs from "fs";
import cors from "cors";
import path from "path";
import morgan from "morgan";
import express from "express";
import winston from "winston";
import favicon from "serve-favicon";
import { fileURLToPath } from "url";
import session from "express-session";
import { db } from "./models/index.js";
import router from "./routes/index.js";
import cookieParser from "cookie-parser";
import { initial } from "./config/db.config.js";
import { serverConfig } from "./config/server.config.mjs";
import { removeExpiredRefreshTokens } from "./background.js";
import { _config } from "./config/global.config.js";
const app = express();
let logger;
if (serverConfig.environment !== "production") {
// Configuration des répertoires et fichiers de journal
const logDirectory = path.join(
path.dirname(fileURLToPath(import.meta.url)),
"logs"
);
const logFilePath = path.join(logDirectory, "access.log");
const errFilePath = path.join(logDirectory, "error.log");
// Vérification et création du répertoire des journaux
if (!fs.existsSync(logDirectory)) {
fs.mkdirSync(logDirectory);
}
// Adaptateur pour le flux de Winston
class WinstonStreamAdapter {
write(message) {
logger.info(message);
}
}
//Configuration du logger Winston
logger = winston.createLogger({
level: "info",
format: winston.format.json(),
transports: [
new winston.transports.Console(),
new winston.transports.File({ filename: errFilePath, level: "error" }),
new winston.transports.File({ filename: logFilePath, level: "info" }),
],
});
// Utilisation du logger Winston avec Morgan pour la journalisation des requêtes
app.use(morgan("combined", { stream: new WinstonStreamAdapter() }));
// Utilisation de setTimeout pour déclencher removeExpiredRefreshTokens après 20 secondes
setTimeout(() => {
removeExpiredRefreshTokens();
}, 20000);
} else {
app.use(morgan("dev"));
}
// Configuration des middlewares
app.use(session(serverConfig.sessionOptions));
app.use(cors(serverConfig.corsOptions));
app.use(cookieParser());
// Configuration du favicon
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const faviconPath = path.join(__dirname, "appointment.png");
app.use(favicon(faviconPath));
// Configuration du body parser
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// Configuration des routes
app.use("/", router);
// Gestionnaire d'erreurs pour les erreurs internes du serveur (500)
app.use((err, req, res, next) => {
logger.error(err.stack);
console.error(err.stack);
res
.status(err.status || 500)
.json({ message: err.message || "Internal Server Error" });
});
// Démarrage du serveur
app.listen(serverConfig.port, () => {
console.log(`Server is running on port ${serverConfig.port}.`);
});
// Fonction pour la connexion à la base de données et initialisation
async function connectToDb() {
try {
// await db.mongoose.connect(serverConfig.mongodb_connect);
await db.mongoose.connect(_config.mongo_url);
console.log("Successfully connected to MongoDB.");
await initial();
} catch (error) {
logger.error("Connection", error);
console.error("Connection", error);
process.exit(1);
}
}
// Appel de la fonction de connexion à la base de données
connectToDb();