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