74 lines
2.2 KiB
JavaScript
74 lines
2.2 KiB
JavaScript
import express from "express";
|
|
import session from "express-session";
|
|
import { deployHandler } from "./utils/handler.js";
|
|
import favicon from "serve-favicon";
|
|
|
|
async function launchWeb() {
|
|
const app = express();
|
|
|
|
app.use(express.json()); // Used to parse JSON bodies
|
|
app.use(express.urlencoded({ extended: false })); //Parse URL-encoded bodies
|
|
app.use(
|
|
session({
|
|
secret: process.env.SECRET,
|
|
resave: false,
|
|
saveUninitialized: true,
|
|
}),
|
|
);
|
|
|
|
app.use("/assets", express.static(`${process.env.WWW}/assets`));
|
|
app.use(favicon(`${process.env.WWW}/assets/images/favicon.ico`));
|
|
|
|
await deployHandler();
|
|
|
|
app.post("*", async (request, response) => {
|
|
console.log("POST: " + request.originalUrl);
|
|
if (!global.handler.post[request.originalUrl]) return;
|
|
if (
|
|
global.handler.post[request.originalUrl].requiresLogin &&
|
|
!request.session.user
|
|
) {
|
|
return response.redirect("/login");
|
|
}
|
|
if (
|
|
global.handler.post[request.originalUrl].permissions &&
|
|
global.handler.post[request.originalUrl].permissions.reduce(
|
|
(a, b) => a + b,
|
|
) &
|
|
(request.session.user.permissions == 0)
|
|
) {
|
|
return response.status(403);
|
|
}
|
|
return await global.handler.post[request.originalUrl].execute(
|
|
request,
|
|
response,
|
|
);
|
|
});
|
|
|
|
app.get("*", async (request, response) => {
|
|
const [path, args] = parseURL(request.originalUrl);
|
|
//console.log(parseURL(request.originalUrl));
|
|
console.log(`GET: ${path}${args ? "?" + args : ""}`);
|
|
|
|
if (!global.handler.get[path]) return;
|
|
if (global.handler.get[path].requiresLogin && !request.session.user) {
|
|
return response.redirect("/login");
|
|
}
|
|
return await global.handler.get[path].execute(request, response, args);
|
|
});
|
|
|
|
const PORT = process.env.PORT || 3000;
|
|
app.listen(PORT, () => {
|
|
console.log(`App available at http://localhost:${PORT}`);
|
|
});
|
|
}
|
|
|
|
function parseURL(URL) {
|
|
const spURL = URL.split("?");
|
|
const path = spURL[0];
|
|
const args = spURL[1];
|
|
return [path, args];
|
|
}
|
|
|
|
export { launchWeb };
|