From 187cc32c66c6e0024527d4c567b89addfa3d4c69 Mon Sep 17 00:00:00 2001 From: Flummi Date: Wed, 1 Apr 2020 12:08:15 +0200 Subject: [PATCH] blah --- src/index.mjs | 30 ++++++++++++++++-------------- src/router.mjs | 15 +++++++++------ src/views.mjs | 19 +++++++++++++------ 3 files changed, 38 insertions(+), 26 deletions(-) diff --git a/src/index.mjs b/src/index.mjs index 30f81a4..275b43a 100644 --- a/src/index.mjs +++ b/src/index.mjs @@ -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); } diff --git a/src/router.mjs b/src/router.mjs index 713ef7f..4dc642b 100644 --- a/src/router.mjs +++ b/src/router.mjs @@ -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) { diff --git a/src/views.mjs b/src/views.mjs index 724120a..c5c2724 100644 --- a/src/views.mjs +++ b/src/views.mjs @@ -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}`)))); } };