This commit is contained in:
Flummi 2020-04-01 12:08:15 +02:00
parent 3df5c94b58
commit 187cc32c66
3 changed files with 38 additions and 26 deletions

View File

@ -1,5 +1,4 @@
import http from "http";
import path from "path";
import url from "url";
import querystring from "querystring";
@ -7,21 +6,24 @@ import router from "./router.mjs";
import views from "./views.mjs";
export default class flummpress {
constructor(opts) {
this.opts = { ...{
views: null,
routes: null
}, ...opts };
return (async () => {
if(this.opts.routes)
await router.loadRoutes(this.opts.routes);
if(this.opts.views)
await views.loadViews(this.opts.views);
return this;
})();
constructor() {
this.router = new router();
this.views = new views();
}
use(item) {
switch(item.type) {
case "route":
item.data.forEach((v, k) => this.router.routes.set(k, v));
break;
case "view":
item.data.forEach((v, k) => this.views.set(k, v));
break;
}
}
listen(...args) {
this.router.routes.forEach((v, k) => console.log("route set", v.method, k));
return http.createServer(async (req, res, r) => {
req.url = url.parse(req.url.replace(/(?!^.)(\/+)?%/, ''));
req.url.qs = querystring.parse(req.url.query);
@ -36,7 +38,7 @@ export default class flummpress {
body
}) => res.writeHead(code, { "Content-Type": `${type}; charset=utf-8` }).end(body);
!(r = router.routes.getRegex(req.url.pathname, req.method)) ? res.writeHead(404).end(`404 - ${req.url.pathname}`) : await r(req, res);
!(r = this.router.routes.getRegex(req.url.pathname, req.method)) ? res.writeHead(404).end(`404 - ${req.url.pathname}`) : await r(req, res);
console.log(`[${(new Date()).toLocaleTimeString()}] ${res.statusCode} ${req.method}\t${req.url.pathname}`);
}).listen(...args);
}

View File

@ -1,7 +1,7 @@
import { promises as fs } from "fs";
import path from "path";
export default new class {
export default class Router {
#mimes;
constructor() {
this.routes = new Map();
@ -10,18 +10,21 @@ export default new class {
await Promise.all(
(await fs.readdir(path))
.filter(f => f.endsWith(".mjs"))
.map(async route => await import(`${path}/${route}`))
.map(async route => (await import(`${path}/${route}`)).default(this))
);
}
route(method, args) {
this.routes.set(args[0], { method: method, f: args[1] });
console.log("route set", method, args[0]);
//console.log("route set", method, args[0]);
return {
type: "route",
data: this.routes.set(args[0], { method: method, f: args[1] })
};
}
get() {
this.route("GET", arguments);
return this.route("GET", arguments);
}
post() {
this.route("POST", arguments);
return this.route("POST", arguments);
}
async static({ dir = path.resolve() + "/public", route = /^\/public/ }) {
if(!this.#mimes) {

View File

@ -1,15 +1,22 @@
import { promises as fs } from "fs";
export default new class {
export default class ViewController {
#views;
constructor() {
this.views = new Map();
this.#views = new Map();
}
get(view) {
return this.views.get(view) || false;
get(name) {
return this.#views.get(name) || false;
}
set(name, view) {
return {
type: "view",
data: this.#views.set(name, view)
};
}
async loadViews(path) {
(await fs.readdir(path))
return await Promise.all((await fs.readdir(path))
.filter(view => view.endsWith(".html"))
.forEach(async view => this.views.set(view.slice(0, -5), await fs.readFile(`${path}/${view}`)));
.map(async view => this.set(view.slice(0, -5), await fs.readFile(`${path}/${view}`))));
}
};