diff --git a/deno.json b/deno.json index 1cec2b7..656b882 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.4", + "version": "0.0.12", "license": "MIT", "exports": { ".": "./serverus.ts", diff --git a/handlers/index.ts b/handlers/index.ts new file mode 100644 index 0000000..410f0fb --- /dev/null +++ b/handlers/index.ts @@ -0,0 +1,9 @@ +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 aa25279..a20ac66 100644 --- a/server.ts +++ b/server.ts @@ -4,10 +4,9 @@ */ import * as colors from '@std/fmt/colors'; -import * as fs from '@std/fs'; import * as path from '@std/path'; -const DEFAULT_HANDLER_DIRECTORIES = [path.resolve(path.join(path.dirname(path.fromFileUrl(import.meta.url)), 'handlers'))]; +const DEFAULT_HANDLER_DIRECTORIES = [import.meta.resolve('./handlers')]; /** A `HANDLER` must take a `Request` and return a `Response` if it can handle it. */ type HANDLER = (request: Request) => Promise | Response | null | undefined; @@ -103,25 +102,34 @@ 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) => path.resolve(dir)) ?? - DEFAULT_HANDLER_DIRECTORIES; + 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; for (const handler_directory of HANDLERS_DIRECTORIES) { - 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) { + 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}`); continue; } - - 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); + throw error; } } diff --git a/tests/04_test_overriding_handlers.test.ts b/tests/04_test_overriding_handlers.test.ts index 90b7a04..43c806e 100644 --- a/tests/04_test_overriding_handlers.test.ts +++ b/tests/04_test_overriding_handlers.test.ts @@ -16,13 +16,12 @@ Deno.test({ const old_handlers: string | undefined = Deno.env.get('SERVERUS_HANDLERS'); try { - Deno.chdir('./tests/www'); Deno.env.set( 'SERVERUS_HANDLERS', - `${path.join(path.dirname(path.resolve(path.fromFileUrl(import.meta.url))), 'handlers')}${ - old_handlers ? (';' + old_handlers) : '' - }` + `${import.meta.resolve('./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 new file mode 100644 index 0000000..f04292b --- /dev/null +++ b/tests/handlers/index.ts @@ -0,0 +1,5 @@ +import * as foo from './foo.ts'; + +export default { + foo +};