115 lines
3.7 KiB
JavaScript
115 lines
3.7 KiB
JavaScript
import fs from 'fs';
|
|
const pagesPath = "./src/html/pages/";
|
|
|
|
async function genHandler() {
|
|
const handler = {
|
|
get: [],
|
|
post: [],
|
|
path: [],
|
|
delete: [],
|
|
};
|
|
let numberOfPages = 0;
|
|
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(
|
|
`\x1b[32mChargement de ${endpoint.type.toUpperCase()}: ${endpoint.path} !\x1b[0m`,
|
|
);
|
|
numberOfPages++;
|
|
console.log(numberOfPages + " endpoints chargés ! ");
|
|
}
|
|
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) {
|
|
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) {
|
|
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];
|
|
}
|
|
|
|
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 };
|