Compare commits

...

2 Commits

Author SHA1 Message Date
nightly 27d5050547 Have the webserver itself start 2022-10-19 12:21:58 +02:00
nightly b475b261db Add the ratelimit package 2022-10-19 12:20:53 +02:00
11 changed files with 149 additions and 16 deletions

21
.prettierrc Normal file
View File

@ -0,0 +1,21 @@
{
"arrowParens": "always",
"endOfLine": "lf",
"proseWrap": "preserve",
"quoteProps": "consistent",
"trailingComma": "all",
"printWidth": 140,
"tabWidth": 2,
"semi": true,
"singleQuote": false,
"useTabs": false,
"overrides": [
{
"files": ["*.liquid"],
"options": {
"parser": "angular"
}
}
]
}

View File

@ -1,21 +1,52 @@
import express from "express";
import { Liquid } from "liquidjs";
const engine = new Liquid();
const app = express();
const port = 3430;
import path from "path";
app.engine('liquid', engine.express());
app.set('views', ['./partials', './views']);
app.set('view engine', 'liquid');
//import { apiRoutes } from "./routes/api";
//import { authRoutes } from "./routes/auth";
import { indexRoutes } from "./routes/index";
app.get('/', function (req, res) {
const todos = ['fork and clone', 'make it better', 'make a pull request']
res.render('todolist', {
todos: todos,
title: 'Welcome to liquidjs!'
})
})
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");
app.listen(port, () => {
console.log('✅ Webserver started on port ', port)
})
export function startWebserver(config:any) {
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",
});
// 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());
// 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");
});
app.listen(3430, "0.0.0.0", () => {
console.log(`Webserver running on port 3430`);
});
}

View File

View File

View File

@ -0,0 +1,40 @@
/**
* @file Index routes
* @description Routings for the index page and it's redirects
* @module webserver/routes/index
*/
import express from "express";
import rateLimit from "express-rate-limit";
const router = express.Router();
const indexRateLimit = rateLimit({
windowMs: 1 * 60 * 1000,
max: 50,
message: "Too many requests in a short period of time.",
});
export function indexRoutes() {
// Index
router.get("/", indexRateLimit,async (req,res) => {
res.render("index", {
//locales: getWebLocale(bot, locale),
page: req.url,
//user: user,
});
})
// Gitea
router.get("/gitea/", async (_req, res) => {
res.redirect(301, "https://tea.nightly.town/Mangoberry/MangoRecipe");
});
// robots.txt
router.get("/robots.txt", async (_req, res) => {
res.type("text/plain");
res.send("User-agent: *\nCrawl-delay: 5\nDisallow: /public/\nDisallow: /auth/");
});
return router;
};

12
Source/classes/Main.ts Normal file
View File

@ -0,0 +1,12 @@
import path from "path";
import { startWebserver } from "../Webserver/index";
import config from "../../config.json";
export class MangoRecipe {
config: typeof config;
// Starts webservers at first boot
constructor() {
this.config = config;
startWebserver(this);
}
}

10
Source/index.ts Normal file
View File

@ -0,0 +1,10 @@
/**
* @file Index
* @description Creates an instance of Hibiki
* @license AGPL-3.0-or-later
*/
import { MangoRecipe } from "./classes/Main";
import config from "../config.json";
new MangoRecipe();

0
config.json Normal file
View File

18
package-lock.json generated
View File

@ -10,6 +10,7 @@
"license": "GPL-2.0-or-later",
"dependencies": {
"express": "^4.18.2",
"express-rate-limit": "^6.6.0",
"express-session": "^1.17.3",
"liquidjs": "^9.42.0",
"path": "^0.12.7",
@ -1453,6 +1454,17 @@
"node": ">= 0.10.0"
}
},
"node_modules/express-rate-limit": {
"version": "6.6.0",
"resolved": "https://registry.npmjs.org/express-rate-limit/-/express-rate-limit-6.6.0.tgz",
"integrity": "sha512-HFN2+4ZGdkQOS8Qli4z6knmJFnw6lZed67o6b7RGplWeb1Z0s8VXaj3dUgPIdm9hrhZXTRpCTHXA0/2Eqex0vA==",
"engines": {
"node": ">= 12.9.0"
},
"peerDependencies": {
"express": "^4 || ^5"
}
},
"node_modules/express-session": {
"version": "1.17.3",
"resolved": "https://registry.npmjs.org/express-session/-/express-session-1.17.3.tgz",
@ -4587,6 +4599,12 @@
}
}
},
"express-rate-limit": {
"version": "6.6.0",
"resolved": "https://registry.npmjs.org/express-rate-limit/-/express-rate-limit-6.6.0.tgz",
"integrity": "sha512-HFN2+4ZGdkQOS8Qli4z6knmJFnw6lZed67o6b7RGplWeb1Z0s8VXaj3dUgPIdm9hrhZXTRpCTHXA0/2Eqex0vA==",
"requires": {}
},
"express-session": {
"version": "1.17.3",
"resolved": "https://registry.npmjs.org/express-session/-/express-session-1.17.3.tgz",

View File

@ -27,6 +27,7 @@
},
"dependencies": {
"express": "^4.18.2",
"express-rate-limit": "^6.6.0",
"express-session": "^1.17.3",
"liquidjs": "^9.42.0",
"path": "^0.12.7",