Maybe URLs like this? I think this'll need some reworking but it's a start.

main
Zed A. Shaw 9 months ago
parent ded0867b9e
commit 53d67f185b
  1. 27
      commands/app.js
  2. 4
      controllers/todo.js

@ -18,6 +18,7 @@ let CONFIGURED = false;
nunjucks.configure("templates", { watch: true }); nunjucks.configure("templates", { watch: true });
const configure = async (fastify, opts) => { const configure = async (fastify, opts) => {
const app = fastify(opts);
// MAGIC: this is how you trick the importer to reload modules // MAGIC: this is how you trick the importer to reload modules
// that have changed. Since it uses a URL you can add a query // that have changed. Since it uses a URL you can add a query
@ -25,23 +26,23 @@ const configure = async (fastify, opts) => {
// tags the module as being "new" when it's still the same file // tags the module as being "new" when it's still the same file
// TODO: maybe use fs timestamps instead? // TODO: maybe use fs timestamps instead?
// BUG: sometimes reload is too fast for vim and crashes // BUG: sometimes reload is too fast for vim and crashes
const controller = await import(`../controllers/todo.js?update=${new Date()}`); const control_mod = await import(`../controllers/todo.js?update=${new Date()}`);
const handler = new controller.Todo();
const app = fastify(opts);
// this is a sample that uses the handler we dynamic load // this is a sample that uses the handler we dynamic load
// to handle the /todo but not sure how to work the actual // to handle the /todo but not sure how to work the actual
// URL mappings for it. Also not sure about using classes // URL mappings for it. Also not sure about using classes
app.get("/todo", async (req, rep) => { for(let [url, controller] of Object.entries(control_mod.default)) {
try { const handler = new controller();
await handler.get(req, rep); app.get(`/${url}`, async (req, rep) => {
} catch(error) { try {
console.error(error); await handler.get(req, rep);
console.error(error.stack); } catch(error) {
console.error(error.source); console.error(error);
} console.error(error.stack);
}); console.error(error.source);
}
});
}
app.register(FastifyStatic, { app.register(FastifyStatic, {
root: path.join(path.resolve("."), 'static'), root: path.join(path.resolve("."), 'static'),

@ -14,3 +14,7 @@ export class Todo {
rep.code(200).type("text/html").send(result); rep.code(200).type("text/html").send(result);
} }
} }
// you use the export default to set the route URL?
// I mean, it works not sure if I like it though
export default { todo: Todo };

Loading…
Cancel
Save