2023-10-21 18:16:11 +02:00
|
|
|
const createError = require('http-errors');
|
|
|
|
const express = require('express');
|
|
|
|
const path = require('path');
|
|
|
|
const cookieParser = require('cookie-parser');
|
2024-12-29 15:18:12 +01:00
|
|
|
var favicon = require('express-favicon');
|
2023-10-21 18:16:11 +02:00
|
|
|
const app = express();
|
|
|
|
const fs = require("fs");
|
2023-10-31 17:00:25 +01:00
|
|
|
const log = require("loguix")
|
|
|
|
|
2023-11-02 11:09:47 +01:00
|
|
|
const { __glob } = require('./bin/global-variables');
|
|
|
|
const users = require("./bin/users")
|
|
|
|
const {User} = require("./bin/users")
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-11-02 18:16:15 +01:00
|
|
|
var wlog = log.getInstance("Serveur")
|
2023-10-31 17:00:25 +01:00
|
|
|
|
|
|
|
|
2023-10-21 18:16:11 +02:00
|
|
|
|
|
|
|
setup()
|
|
|
|
|
2023-10-31 17:00:25 +01:00
|
|
|
|
2023-10-21 18:16:11 +02:00
|
|
|
function getRouters() {
|
|
|
|
|
2023-11-02 11:09:47 +01:00
|
|
|
wlog.log("Récupération de " + fs.readdirSync(__glob.ROUTES).length + " routeurs depuis : " + __glob.ROUTES)
|
2023-10-21 18:16:11 +02:00
|
|
|
for(var route of fs.readdirSync(__glob.ROUTES)) {
|
|
|
|
|
|
|
|
if(route == "index.js") {
|
|
|
|
|
|
|
|
app.use("/", require(__glob.ROUTES + "index"))
|
|
|
|
} else {
|
|
|
|
|
|
|
|
app.use("/" + route.replace(".js", ""), require(__glob.ROUTES + route))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function setup() {
|
|
|
|
|
|
|
|
app.set('views', path.join(__dirname, 'views'));
|
|
|
|
app.set('view engine', 'ejs');
|
|
|
|
|
|
|
|
app.use(express.json());
|
|
|
|
app.use(express.urlencoded({ extended: false }));
|
|
|
|
app.use(cookieParser());
|
|
|
|
app.use(express.static(path.join(__dirname, 'public')));
|
2023-11-03 14:25:39 +01:00
|
|
|
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')))
|
2023-10-21 18:16:11 +02:00
|
|
|
|
2023-11-05 12:12:18 +01:00
|
|
|
|
2023-10-21 18:16:11 +02:00
|
|
|
getRouters()
|
2023-11-02 11:09:47 +01:00
|
|
|
users.fetchUsers()
|
2023-10-21 18:16:11 +02:00
|
|
|
|
|
|
|
// catch 404 and forward to error handler
|
|
|
|
app.use(function(req, res, next) {
|
2023-10-31 17:00:25 +01:00
|
|
|
res.locals.message = "Page non trouvé";
|
|
|
|
res.locals.error = {
|
|
|
|
"status": "404",
|
|
|
|
"stack": ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// render the error page
|
|
|
|
res.status(404 || 404);
|
|
|
|
res.render('utils/error');
|
2023-10-21 18:16:11 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// error handler
|
|
|
|
app.use(function(err, req, res, next) {
|
|
|
|
// set locals, only providing error in development
|
|
|
|
res.locals.message = err.message;
|
|
|
|
res.locals.error = req.app.get('env') === 'development' ? err : {};
|
|
|
|
|
|
|
|
// render the error page
|
|
|
|
res.status(err.status || 500);
|
2023-10-31 17:00:25 +01:00
|
|
|
res.render('utils/error');
|
2023-10-21 18:16:11 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = app;
|