|
|
|
@ -19,18 +19,15 @@ let CONFIGURED = false; |
|
|
|
|
// nunjucks does it's own reload so let it do that
|
|
|
|
|
nunjucks.configure("pages", { watch: true }); |
|
|
|
|
|
|
|
|
|
const load_mode = async (mod) => { |
|
|
|
|
const load_mod = async (app, mod) => { |
|
|
|
|
const control_mod = await import(`../${mod}?update=${Date.now()}`); |
|
|
|
|
const pf = path.parse(mod); |
|
|
|
|
const url = `/${pf.name}`; |
|
|
|
|
|
|
|
|
|
console.log("URL IS", url); |
|
|
|
|
|
|
|
|
|
for(let verb of ["get", "post", "put", "delete"]) { |
|
|
|
|
const handler = control_mod[verb]; |
|
|
|
|
|
|
|
|
|
if(handler !== undefined) { |
|
|
|
|
console.log("ADDING VERB", verb); |
|
|
|
|
app[verb](url, async (req, rep) => { |
|
|
|
|
try { |
|
|
|
|
await handler(req, rep); |
|
|
|
@ -49,7 +46,7 @@ const configure = async (fastify, opts) => { |
|
|
|
|
|
|
|
|
|
// forces reload of the modules
|
|
|
|
|
for(let mod of fg.sync("{api,controllers}/*.js")) { |
|
|
|
|
load_mode(mod); |
|
|
|
|
await load_mod(app, mod); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
app.register(Formbody); |
|
|
|
@ -57,19 +54,17 @@ const configure = async (fastify, opts) => { |
|
|
|
|
app.get("/*", (req, rep) => { |
|
|
|
|
const target = path.resolve(path.join("pages", req.url)); |
|
|
|
|
const index = path.resolve(path.join("pages", req.url, "index.html")); |
|
|
|
|
console.log("TARGET", target, "INDEX", index); |
|
|
|
|
|
|
|
|
|
if(fs.existsSync(index)) { |
|
|
|
|
const fname = path.join(req.url.slice(1), "index.html"); |
|
|
|
|
console.log("RENDERING", fname); |
|
|
|
|
const result = nunjucks.render(fname); |
|
|
|
|
const result = nunjucks.render(index); |
|
|
|
|
rep.code(200).type("text/html").send(result); |
|
|
|
|
} else if(fs.existsSync(target)) { |
|
|
|
|
const fname = req.url.slice(1); |
|
|
|
|
const result = nunjucks.render(fname); |
|
|
|
|
rep.code(200).type("text/html").send(result); |
|
|
|
|
} else { |
|
|
|
|
rep.code(404); |
|
|
|
|
console.error("404 NOT FOUND", target, "NO INDEX", index); |
|
|
|
|
rep.code(404).type("text/html").send("Not Found"); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
@ -94,7 +89,7 @@ const reload = () => { |
|
|
|
|
|
|
|
|
|
export const main = async (arg, opts) => { |
|
|
|
|
try { |
|
|
|
|
chokidar.watch(["lib","commands","controllers","static","migrations","tests"]) |
|
|
|
|
chokidar.watch(["lib","api", "pages", "commands","controllers","static","migrations","tests"]) |
|
|
|
|
.on("add", path => reload()) |
|
|
|
|
.on("change", path => reload()) |
|
|
|
|
.on("unlink", path => reload()) |
|
|
|
|