2023-12-07 01:28:39 +01:00
|
|
|
import fs from 'fs';
|
2023-12-06 19:11:14 +01:00
|
|
|
const pagesPath = "./src/html/pages/";
|
|
|
|
|
2023-12-07 23:50:39 +01:00
|
|
|
async function genHandler() {
|
|
|
|
const handler = {
|
|
|
|
get: [],
|
|
|
|
post: [],
|
|
|
|
path: [],
|
|
|
|
delete: [],
|
2023-12-06 19:11:14 +01:00
|
|
|
};
|
|
|
|
let numberOfPages = 0;
|
2023-12-11 21:50:58 +01:00
|
|
|
const endpointFiles = fs
|
|
|
|
.readdirSync(pagesPath, { recursive: true })
|
|
|
|
.filter((file) => file.endsWith(".js"));
|
|
|
|
console.log(endpointFiles)
|
|
|
|
for (const file of endpointFiles) {
|
|
|
|
const { default: endpoint } = await import(`.${pagesPath}/${file}`);
|
|
|
|
handler[endpoint.type].push(endpoint);
|
|
|
|
console.log(
|
2023-12-12 14:46:47 +01:00
|
|
|
`\x1b[32mChargement de ${endpoint.type.toUpperCase()}: ${endpoint.path} !\x1b[0m`,
|
2023-12-11 21:50:58 +01:00
|
|
|
);
|
|
|
|
numberOfPages++;
|
|
|
|
console.log(numberOfPages + " endpoints chargés ! ");
|
|
|
|
}
|
2023-12-07 23:50:39 +01:00
|
|
|
return handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function deployHandler(app) {
|
|
|
|
const handler = await genHandler();
|
|
|
|
|
|
|
|
for(const endpoint of handler.get) {
|
|
|
|
app.get(endpoint.path, async (request, response) => {
|
|
|
|
const [path, args] = parseURL(request.originalUrl);
|
|
|
|
console.log("GET: " + path);
|
|
|
|
|
|
|
|
if (endpoint.requiresLogin && !request.session.user) {
|
|
|
|
return response.redirect("/login");
|
|
|
|
}
|
|
|
|
if (endpoint.permissions) {
|
|
|
|
if((endpoint.permissions & request.session.user.permissions) == 0) {
|
|
|
|
return response.status(403).send("Vous n'avez pas la permission d'effectuer cette action !");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return await endpoint.execute(
|
|
|
|
request,
|
|
|
|
response,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
for(const endpoint of handler.post) {
|
|
|
|
app.post(endpoint.path, async (request, response) => {
|
|
|
|
console.log("POST: " + request.originalUrl);
|
|
|
|
|
|
|
|
if (endpoint.requiresLogin && !request.session.user) {
|
|
|
|
return response.redirect("/login");
|
|
|
|
}
|
|
|
|
if (endpoint.permissions) {
|
|
|
|
if((endpoint.permissions & request.session.user.permissions) == 0) {
|
2023-12-12 13:43:47 +01:00
|
|
|
return response.status(403).send("Vous n'avez pas la permission d'effectuer cette action !");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return await endpoint.execute(
|
|
|
|
request,
|
|
|
|
response,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
for(const endpoint of handler.delete) {
|
|
|
|
app.delete(endpoint.path, async (request, response) => {
|
|
|
|
console.log("DELETE: " + request.originalUrl);
|
|
|
|
|
|
|
|
if (endpoint.requiresLogin && !request.session.user) {
|
|
|
|
return response.redirect("/login");
|
|
|
|
}
|
|
|
|
if (endpoint.permissions) {
|
|
|
|
if((endpoint.permissions & request.session.user.permissions) == 0) {
|
2023-12-07 23:50:39 +01:00
|
|
|
return response.status(403).send("Vous n'avez pas la permission d'effectuer cette action !");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return await endpoint.execute(
|
|
|
|
request,
|
|
|
|
response,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseURL(URL) {
|
|
|
|
const spURL = URL.split("?");
|
|
|
|
const path = spURL[0];
|
|
|
|
if(!spURL[1]) return [path, {}];
|
|
|
|
|
|
|
|
const args = {};
|
|
|
|
for (let i = 0; i < spURL[1].split("=").length; i+=2) {
|
|
|
|
const key = spURL[1].split("=")[i];
|
|
|
|
const val = spURL[1].split("=")[i+1];
|
|
|
|
args[key] = val;
|
|
|
|
}
|
|
|
|
return [path, args];
|
2023-12-06 19:11:14 +01:00
|
|
|
}
|
|
|
|
|
2023-12-11 21:50:58 +01:00
|
|
|
function recursiveFileSearch(directory) {
|
|
|
|
const files = [];
|
|
|
|
fs.readdirSync(directory).forEach(File => {
|
|
|
|
const Absolute = Path.join(directory, File);
|
|
|
|
if (fs.statSync(Absolute).isDirectory()) return ThroughDirectory(Absolute);
|
|
|
|
else return files.push(Absolute);
|
|
|
|
});
|
|
|
|
return files;
|
|
|
|
}
|
|
|
|
|
|
|
|
export { deployHandler, parseURL };
|