From b3a399cf7a345e05481980eeeefbd410def095b0 Mon Sep 17 00:00:00 2001 From: Andy Burke Date: Sun, 22 Jun 2025 14:10:37 -0700 Subject: [PATCH] fix: let's just force the handlers dir to have an index --- deno.json | 2 +- server.ts | 36 +++++++++++++++++++++--------------- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/deno.json b/deno.json index c5f8a39..b3e304d 100644 --- a/deno.json +++ b/deno.json @@ -1,7 +1,7 @@ { "name": "@andyburke/serverus", "description": "A flexible HTTP server for mixed content. Throw static files, markdown, Typescript and (hopefully, eventually) more into a directory and serverus can serve it up a bit more like old-school CGI.", - "version": "0.0.7", + "version": "0.0.8", "license": "MIT", "exports": { ".": "./serverus.ts", diff --git a/server.ts b/server.ts index 7445356..8a2e358 100644 --- a/server.ts +++ b/server.ts @@ -110,24 +110,30 @@ export class SERVER { for (const handler_directory of HANDLERS_DIRECTORIES) { const root_directory = path.resolve(handler_directory); + const index_file = path.join(root_directory, 'index.ts'); - for await ( - const entry of walk(root_directory, { - exts: ['.ts'], - skip: [/\.test\.ts$/] - }) - ) { - if (!entry.isFile) { + try { + const handlers_index = await import(index_file); + console.dir({ + root_directory, + index_file, + handlers_index + }); + for (const handler_name of Object.keys(handlers_index.default)) { + const handler_module: HANDLER_MODULE = handlers_index.default[handler_name]; + if (typeof handler_module?.default !== 'function') { + console.warn(`Could not load handler "${handler_name}" - no default exported function`); + continue; + } + + this.handlers.push(handler_module); + } + } catch (error) { + if (error instanceof Deno.errors.NotFound) { + console.warn(`Could not load handler index from: ${index_file}`); continue; } - - const handler_module: HANDLER_MODULE = await import(entry.path); - if (typeof handler_module.default !== 'function') { - console.warn(`Could not load handler, no default exported function: ${entry.path}`); - continue; - } - - this.handlers.push(handler_module); + throw error; } }