This commit is contained in:
Flummi
2020-04-02 04:35:28 +02:00
parent 5ff96cdf5e
commit d39deeb038
100 changed files with 34498 additions and 1100 deletions

View File

@@ -1,4 +1,8 @@
class Router {
import { promises as fs } from "fs";
import path from "path";
export default new class Router {
#mimes;
constructor() {
this.routes = new Map();
};
@@ -12,11 +16,32 @@ class Router {
post() {
this.route("POST", arguments);
};
async static({ dir = path.resolve() + "/public", route = /^\/public/ }) {
if(!this.#mimes) {
this.#mimes = new Map();
(await fs.readFile("/etc/mime.types", "utf-8"))
.split("\n")
.filter(e => !e.startsWith("#") && e)
.map(e => e.split(/\s{2,}/))
.filter(e => e.length > 1)
.forEach(m => m[1].split(" ").forEach(ext => this.#mimes.set(ext, m[0])));
}
this.get(route, async (req, res) => {
try {
return res.reply({
type: this.#mimes.get(req.url.path.split(".").pop()).toLowerCase(),
body: await fs.readFile(path.join(dir, req.url.path.replace(route, "")))
});
} catch {
return res.reply({
code: 404,
body: "404 - file not found"
});
}
});
};
};
const router = new Router();
export default router;
export const routes = router.routes;
Map.prototype.getRegex = function(path, method, tmp) {
return (!(tmp = [...this.entries()].filter(r => r[0].exec(path) && r[1].method.includes(method))[0])) ? false : tmp[1].f;
Map.prototype.getRoute = function(path, method, tmp) {
return (!(tmp = [...this.entries()].filter(r => ( r[0] === path || r[0].exec?.(path) ) && r[1].method.includes(method) )[0])) ? false : tmp[1].f;
};