diff --git a/deno.json b/deno.json index 656b882..1cec2b7 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.12", + "version": "0.0.4", "license": "MIT", "exports": { ".": "./serverus.ts", diff --git a/handlers/index.ts b/handlers/index.ts deleted file mode 100644 index 410f0fb..0000000 --- a/handlers/index.ts +++ /dev/null @@ -1,9 +0,0 @@ -import * as markdown from './markdown.ts'; -import * as static_files from './static.ts'; -import * as typescript from './typescript.ts'; - -export default { - markdown, - static: static_files, - typescript -}; diff --git a/server.ts b/server.ts index a20ac66..aa25279 100644 --- a/server.ts +++ b/server.ts @@ -4,9 +4,10 @@ */ import * as colors from '@std/fmt/colors'; +import * as fs from '@std/fs'; import * as path from '@std/path'; -const DEFAULT_HANDLER_DIRECTORIES = [import.meta.resolve('./handlers')]; +const DEFAULT_HANDLER_DIRECTORIES = [path.resolve(path.join(path.dirname(path.fromFileUrl(import.meta.url)), 'handlers'))]; /** A `HANDLER` must take a `Request` and return a `Response` if it can handle it. */ type HANDLER = (request: Request) => Promise | Response | null | undefined; @@ -102,34 +103,25 @@ export class SERVER { this.controller = new AbortController(); const { signal } = this.controller; - const HANDLERS_DIRECTORIES: string[] = Deno.env.get('SERVERUS_HANDLERS') - ?.split(/[;]/g) - ?.filter((dir) => dir.length > 0) - ?.map((dir) => { - return dir.includes('://') ? dir : path.resolve(dir); - }) ?? DEFAULT_HANDLER_DIRECTORIES; + const HANDLERS_DIRECTORIES: string[] = + Deno.env.get('SERVERUS_HANDLERS')?.split(/[;:]/g)?.filter((dir) => dir.length > 0)?.map((dir) => path.resolve(dir)) ?? + DEFAULT_HANDLER_DIRECTORIES; for (const handler_directory of HANDLERS_DIRECTORIES) { - const index_file = path.join(handler_directory, 'index.ts'); - - try { - const handlers_index = await import(index_file); - - 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}`); + const resolved_handler_directory_glob = path.resolve(path.join(handler_directory, '*.ts')); + for await (const globbed_record of fs.expandGlob(resolved_handler_directory_glob)) { + if (!globbed_record.isFile) { continue; } - throw error; + + const import_path = new URL('file://' + globbed_record.path, import.meta.url).toString(); + const handler_module: HANDLER_MODULE = await import(import_path); + if (typeof handler_module.default !== 'function') { + console.warn(`Could not load handler, no default exported function: ${globbed_record.path}`); + continue; + } + + this.handlers.push(handler_module); } } diff --git a/tests/04_test_overriding_handlers.test.ts b/tests/04_test_overriding_handlers.test.ts index 43c806e..90b7a04 100644 --- a/tests/04_test_overriding_handlers.test.ts +++ b/tests/04_test_overriding_handlers.test.ts @@ -16,12 +16,13 @@ Deno.test({ const old_handlers: string | undefined = Deno.env.get('SERVERUS_HANDLERS'); try { + Deno.chdir('./tests/www'); Deno.env.set( 'SERVERUS_HANDLERS', - `${import.meta.resolve('./handlers')}${old_handlers ? (';' + old_handlers) : ''}` + `${path.join(path.dirname(path.resolve(path.fromFileUrl(import.meta.url))), 'handlers')}${ + old_handlers ? (';' + old_handlers) : '' + }` ); - - Deno.chdir('./tests/www'); test_server_info = await get_ephemeral_listen_server(); const response = await fetch(`http://${test_server_info.hostname}:${test_server_info.port}/echo/hello_world.foo`, { diff --git a/tests/handlers/index.ts b/tests/handlers/index.ts deleted file mode 100644 index f04292b..0000000 --- a/tests/handlers/index.ts +++ /dev/null @@ -1,5 +0,0 @@ -import * as foo from './foo.ts'; - -export default { - foo -};