feature: add SERVERUS_TYPESCRIPT_IMPORT_LOGGING environment variable

feature(wip): reload on changes
This commit is contained in:
Andy Burke 2025-06-25 16:53:45 -07:00
parent 62eba8612b
commit 26125f4f11
5 changed files with 18 additions and 7 deletions

View file

@ -1,7 +1,7 @@
{ {
"name": "@andyburke/serverus", "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.", "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.1.0", "version": "0.2.0",
"license": "MIT", "license": "MIT",
"exports": { "exports": {
".": "./serverus.ts", ".": "./serverus.ts",

View file

@ -74,6 +74,11 @@ export default async function handle_typescript(request: Request): Promise<Respo
const module: ROUTE_HANDLER = await import(import_path) as ROUTE_HANDLER; const module: ROUTE_HANDLER = await import(import_path) as ROUTE_HANDLER;
const pattern = new URLPattern({ pathname: route_path }); const pattern = new URLPattern({ pathname: route_path });
if (Deno.env.get('SERVERUS_TYPESCRIPT_IMPORT_LOGGING')) {
console.log(`imported: ${import_path}`);
}
routes.set(pattern, module); routes.set(pattern, module);
} catch (error) { } catch (error) {
console.error(`Error mounting module ${import_path} at ${route_path}\n\n${error}\n`); console.error(`Error mounting module ${import_path} at ${route_path}\n\n${error}\n`);

View file

@ -31,6 +31,7 @@ export type SERVER_OPTIONS = {
hostname?: string; hostname?: string;
port?: number; port?: number;
logging?: boolean | LOGGER; logging?: boolean | LOGGER;
watch?: boolean;
}; };
/** /**
@ -176,6 +177,9 @@ export class SERVER {
Deno.addSignalListener('SIGTERM', this.shutdown_binding); Deno.addSignalListener('SIGTERM', this.shutdown_binding);
Deno.addSignalListener('SIGINT', this.shutdown_binding); Deno.addSignalListener('SIGINT', this.shutdown_binding);
if (this.options.watch) {
Deno.watchFs;
}
return this; return this;
} }

View file

@ -8,31 +8,34 @@ import { SERVER } from './server.ts';
import * as path from '@std/path'; import * as path from '@std/path';
const settings = parseArgs(Deno.args, { const settings = parseArgs(Deno.args, {
boolean: ['help', 'logs', 'version'], boolean: ['help', 'logs', 'version', 'watch'],
string: ['hostname', 'port', 'root'], string: ['hostname', 'port', 'root'],
negatable: ['logs'], negatable: ['logs', 'watch'],
alias: { alias: {
help: 'h', help: 'h',
port: 'p', port: 'p',
root: 'r', root: 'r',
version: 'v' version: 'v',
watch: 'w'
}, },
default: { default: {
hostname: 'localhost', hostname: 'localhost',
logs: true, logs: true,
port: '8000', port: '8000',
root: Deno.env.get('SERVERUS_ROOT') ?? './' root: Deno.env.get('SERVERUS_ROOT') ?? './',
watch: true
} }
}); });
if (settings.help) { if (settings.help) {
console.log( console.log(
`Usage: serverus [--h(elp)] [--v(ersion)] [--no-logs] [--r(oot) ./www] `Usage: serverus [--h(elp)] [--v(ersion)] [--no-logs] [--no-watch] [--r(oot) ./www]
Options: Options:
-h, --help Show this help message -h, --help Show this help message
--hostname Set the hostname/ip to bind to, default: localhost --hostname Set the hostname/ip to bind to, default: localhost
--no-logs Disable logging --no-logs Disable logging
--no-watch Disable watch/reloading for certain types of file changes (handlers, typescript)
-p, --port Set the port to bind to, default: 8000 -p, --port Set the port to bind to, default: 8000
-r, --root Set the root directory to serve, default: './' -r, --root Set the root directory to serve, default: './'
-v, --version Show the version number -v, --version Show the version number

View file

@ -1,6 +1,5 @@
import * as asserts from '@std/assert'; import * as asserts from '@std/assert';
import { EPHEMERAL_SERVER, get_ephemeral_listen_server } from './helpers.ts'; import { EPHEMERAL_SERVER, get_ephemeral_listen_server } from './helpers.ts';
import * as path from '@std/path';
Deno.test({ Deno.test({
name: 'override the default handlers', name: 'override the default handlers',