2022-10-17 11:56:45 +02:00
|
|
|
import express from "express";
|
2022-10-18 12:30:12 +02:00
|
|
|
import { Liquid } from "liquidjs";
|
2022-10-19 12:21:58 +02:00
|
|
|
import path from "path";
|
|
|
|
|
2022-10-19 13:17:33 +02:00
|
|
|
// import { apiRoutes } from "./routes/api";
|
|
|
|
// import { authRoutes } from "./routes/auth";
|
2022-10-19 12:21:58 +02:00
|
|
|
import { indexRoutes } from "./routes/index";
|
|
|
|
|
|
|
|
const isProduction = process.env.NODE_ENV === "production";
|
|
|
|
const LAYOUTS_DIRECTORY = path.join(__dirname, "layouts");
|
|
|
|
const PARTIALS_DIRECTORY = path.join(__dirname, "partials");
|
|
|
|
const PUBLIC_DIRECTORY = path.join(__dirname, "public");
|
|
|
|
const VIEWS_DIRECTORY = path.join(__dirname, "views");
|
|
|
|
|
2022-10-19 13:07:30 +02:00
|
|
|
export function startWebserver(config: any) {
|
2022-10-19 12:21:58 +02:00
|
|
|
const app = express();
|
|
|
|
app.disable("x-powered-by");
|
|
|
|
|
|
|
|
app.use(express.urlencoded({ extended: true, limit: "1mb" }));
|
|
|
|
app.use(express.json({ limit: "1mb" }));
|
|
|
|
|
|
|
|
// Liquid engine options
|
|
|
|
const engine = new Liquid({
|
|
|
|
root: [VIEWS_DIRECTORY, PARTIALS_DIRECTORY, LAYOUTS_DIRECTORY],
|
|
|
|
cache: isProduction,
|
|
|
|
lenientIf: true,
|
|
|
|
jsTruthy: true,
|
|
|
|
extname: ".liquid",
|
|
|
|
});
|
|
|
|
|
2022-10-19 16:00:06 +02:00
|
|
|
if (config) {
|
2022-10-20 09:07:07 +02:00
|
|
|
console.log(config.port);
|
2022-10-19 16:00:06 +02:00
|
|
|
}
|
|
|
|
|
2022-10-19 12:21:58 +02:00
|
|
|
// View engine options
|
|
|
|
app.engine("liquid", engine.express());
|
|
|
|
app.set("views", [VIEWS_DIRECTORY, PARTIALS_DIRECTORY, LAYOUTS_DIRECTORY]);
|
|
|
|
app.set("view engine", "liquid");
|
|
|
|
|
|
|
|
// Uses public folder
|
|
|
|
app.use("/public/", express.static(PUBLIC_DIRECTORY));
|
|
|
|
|
|
|
|
// Routes
|
|
|
|
app.use("/", indexRoutes());
|
2022-10-19 13:07:30 +02:00
|
|
|
|
2022-10-19 12:21:58 +02:00
|
|
|
// 404 handler
|
|
|
|
app.use((req, res) => {
|
|
|
|
if (req.accepts("html")) return res.status(404).render("404", { url: req.url, errorCode: 404 });
|
|
|
|
else if (req.accepts("json")) return res.status(404).send({ error: "404" });
|
|
|
|
else res.status(404).type("txt").send("404");
|
|
|
|
});
|
|
|
|
|
2022-10-20 09:07:07 +02:00
|
|
|
app.listen(config.port, "0.0.0.0", () => {
|
2022-10-20 10:11:45 +02:00
|
|
|
console.log(`Webserver running on port ${config.port}`);
|
2022-10-19 12:21:58 +02:00
|
|
|
});
|
2022-10-19 13:07:30 +02:00
|
|
|
}
|