From 53d67f185b2545b6939dc1d15d66d57875c8e539 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Thu, 22 Feb 2024 15:56:04 -0500 Subject: [PATCH] Maybe URLs like this? I think this'll need some reworking but it's a start. --- commands/app.js | 27 ++++++++++++++------------- controllers/todo.js | 4 ++++ 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/commands/app.js b/commands/app.js index 7c919f2..be83324 100644 --- a/commands/app.js +++ b/commands/app.js @@ -18,6 +18,7 @@ let CONFIGURED = false; nunjucks.configure("templates", { watch: true }); const configure = async (fastify, opts) => { + const app = fastify(opts); // MAGIC: this is how you trick the importer to reload modules // 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 // TODO: maybe use fs timestamps instead? // BUG: sometimes reload is too fast for vim and crashes - const controller = await import(`../controllers/todo.js?update=${new Date()}`); - - const handler = new controller.Todo(); - const app = fastify(opts); + const control_mod = await import(`../controllers/todo.js?update=${new Date()}`); // this is a sample that uses the handler we dynamic load // to handle the /todo but not sure how to work the actual // URL mappings for it. Also not sure about using classes - app.get("/todo", async (req, rep) => { - try { - await handler.get(req, rep); - } catch(error) { - console.error(error); - console.error(error.stack); - console.error(error.source); - } - }); + for(let [url, controller] of Object.entries(control_mod.default)) { + const handler = new controller(); + app.get(`/${url}`, async (req, rep) => { + try { + await handler.get(req, rep); + } catch(error) { + console.error(error); + console.error(error.stack); + console.error(error.source); + } + }); + } app.register(FastifyStatic, { root: path.join(path.resolve("."), 'static'), diff --git a/controllers/todo.js b/controllers/todo.js index 104b489..277c68a 100644 --- a/controllers/todo.js +++ b/controllers/todo.js @@ -14,3 +14,7 @@ export class Todo { 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 };