blah
This commit is contained in:
parent
3df5c94b58
commit
187cc32c66
|
@ -1,5 +1,4 @@
|
||||||
import http from "http";
|
import http from "http";
|
||||||
import path from "path";
|
|
||||||
import url from "url";
|
import url from "url";
|
||||||
import querystring from "querystring";
|
import querystring from "querystring";
|
||||||
|
|
||||||
|
@ -7,21 +6,24 @@ import router from "./router.mjs";
|
||||||
import views from "./views.mjs";
|
import views from "./views.mjs";
|
||||||
|
|
||||||
export default class flummpress {
|
export default class flummpress {
|
||||||
constructor(opts) {
|
constructor() {
|
||||||
this.opts = { ...{
|
this.router = new router();
|
||||||
views: null,
|
this.views = new views();
|
||||||
routes: null
|
}
|
||||||
}, ...opts };
|
|
||||||
return (async () => {
|
use(item) {
|
||||||
if(this.opts.routes)
|
switch(item.type) {
|
||||||
await router.loadRoutes(this.opts.routes);
|
case "route":
|
||||||
if(this.opts.views)
|
item.data.forEach((v, k) => this.router.routes.set(k, v));
|
||||||
await views.loadViews(this.opts.views);
|
break;
|
||||||
return this;
|
case "view":
|
||||||
})();
|
item.data.forEach((v, k) => this.views.set(k, v));
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
listen(...args) {
|
listen(...args) {
|
||||||
|
this.router.routes.forEach((v, k) => console.log("route set", v.method, k));
|
||||||
return http.createServer(async (req, res, r) => {
|
return http.createServer(async (req, res, r) => {
|
||||||
req.url = url.parse(req.url.replace(/(?!^.)(\/+)?%/, ''));
|
req.url = url.parse(req.url.replace(/(?!^.)(\/+)?%/, ''));
|
||||||
req.url.qs = querystring.parse(req.url.query);
|
req.url.qs = querystring.parse(req.url.query);
|
||||||
|
@ -36,7 +38,7 @@ export default class flummpress {
|
||||||
body
|
body
|
||||||
}) => res.writeHead(code, { "Content-Type": `${type}; charset=utf-8` }).end(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}`);
|
console.log(`[${(new Date()).toLocaleTimeString()}] ${res.statusCode} ${req.method}\t${req.url.pathname}`);
|
||||||
}).listen(...args);
|
}).listen(...args);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import { promises as fs } from "fs";
|
import { promises as fs } from "fs";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
|
|
||||||
export default new class {
|
export default class Router {
|
||||||
#mimes;
|
#mimes;
|
||||||
constructor() {
|
constructor() {
|
||||||
this.routes = new Map();
|
this.routes = new Map();
|
||||||
|
@ -10,18 +10,21 @@ export default new class {
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
(await fs.readdir(path))
|
(await fs.readdir(path))
|
||||||
.filter(f => f.endsWith(".mjs"))
|
.filter(f => f.endsWith(".mjs"))
|
||||||
.map(async route => await import(`${path}/${route}`))
|
.map(async route => (await import(`${path}/${route}`)).default(this))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
route(method, args) {
|
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() {
|
get() {
|
||||||
this.route("GET", arguments);
|
return this.route("GET", arguments);
|
||||||
}
|
}
|
||||||
post() {
|
post() {
|
||||||
this.route("POST", arguments);
|
return this.route("POST", arguments);
|
||||||
}
|
}
|
||||||
async static({ dir = path.resolve() + "/public", route = /^\/public/ }) {
|
async static({ dir = path.resolve() + "/public", route = /^\/public/ }) {
|
||||||
if(!this.#mimes) {
|
if(!this.#mimes) {
|
||||||
|
|
|
@ -1,15 +1,22 @@
|
||||||
import { promises as fs } from "fs";
|
import { promises as fs } from "fs";
|
||||||
|
|
||||||
export default new class {
|
export default class ViewController {
|
||||||
|
#views;
|
||||||
constructor() {
|
constructor() {
|
||||||
this.views = new Map();
|
this.#views = new Map();
|
||||||
}
|
}
|
||||||
get(view) {
|
get(name) {
|
||||||
return this.views.get(view) || false;
|
return this.#views.get(name) || false;
|
||||||
|
}
|
||||||
|
set(name, view) {
|
||||||
|
return {
|
||||||
|
type: "view",
|
||||||
|
data: this.#views.set(name, view)
|
||||||
|
};
|
||||||
}
|
}
|
||||||
async loadViews(path) {
|
async loadViews(path) {
|
||||||
(await fs.readdir(path))
|
return await Promise.all((await fs.readdir(path))
|
||||||
.filter(view => view.endsWith(".html"))
|
.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}`))));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue
Block a user