fix: let's just force the handlers dir to have an index

This commit is contained in:
Andy Burke 2025-06-22 14:10:37 -07:00
parent 9bbece2601
commit b3a399cf7a
2 changed files with 22 additions and 16 deletions

View file

@ -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",

View file

@ -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;
}
}