feature: SERVERUS_ROOT environment variable

This commit is contained in:
Andy Burke 2025-06-25 17:06:05 -07:00
parent 26125f4f11
commit c92ef0688b
5 changed files with 55 additions and 1 deletions

View file

@ -31,6 +31,7 @@ export type SERVER_OPTIONS = {
hostname?: string;
port?: number;
logging?: boolean | LOGGER;
root?: string;
watch?: boolean;
};
@ -78,6 +79,7 @@ export class SERVER {
private controller: AbortController | undefined;
private shutdown_binding: (() => void) | undefined;
private handlers: HANDLER_MODULE[];
private original_directory: string | undefined;
/**
* @param {SERVER_OPTIONS} (optional) options to configure the server
@ -100,6 +102,12 @@ export class SERVER {
* @returns {Promise<SERVER>}
*/
public async start(): Promise<SERVER> {
const root = this.options.root ?? Deno.env.get('SERVERUS_ROOT');
if (typeof root === 'string') {
this.original_directory = Deno.cwd();
Deno.chdir(path.resolve(root));
}
this.controller = new AbortController();
const { signal } = this.controller;
@ -197,9 +205,14 @@ export class SERVER {
}
}
if (this.original_directory) {
Deno.chdir(this.original_directory);
}
this.controller = undefined;
this.server = undefined;
this.shutdown_binding = undefined;
this.original_directory = undefined;
for (const handler_module of this.handlers) {
if (typeof handler_module.unload === 'function') {