Compare commits

..

No commits in common. "5ee654f28033b03a3227dfc0f1226b6b5f698a25" and "bc8b67361d40e01a9b34e5deb94775d26f657c1f" have entirely different histories.

5 changed files with 22 additions and 43 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.12",
"version": "0.0.4",
"license": "MIT",
"exports": {
".": "./serverus.ts",

View file

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

View file

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

View file

@ -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`, {

View file

@ -1,5 +0,0 @@
import * as foo from './foo.ts';
export default {
foo
};