serverus/serverus.ts

58 lines
1.3 KiB
TypeScript

/**
* Command line interface for SERVERUS
* @module
*/
import { parseArgs } from '@std/cli/parse-args';
import { SERVER } from './server.ts';
import * as path from '@std/path';
const settings = parseArgs(Deno.args, {
boolean: ['help', 'logs', 'version'],
string: ['hostname', 'port', 'root'],
negatable: ['logs'],
alias: {
help: 'h',
port: 'p',
root: 'r',
version: 'v'
},
default: {
hostname: 'localhost',
logs: true,
port: '8000',
root: Deno.env.get('SERVERUS_ROOT') ?? './'
}
});
if (settings.help) {
console.log(
`Usage: serverus [--h(elp)] [--v(ersion)] [--no-logs] [--r(oot) ./www]
Options:
-h, --help Show this help message
--hostname Set the hostname/ip to bind to, default: localhost
--no-logs Disable logging
-p, --port Set the port to bind to, default: 8000
-r, --root Set the root directory to serve, default: './'
-v, --version Show the version number
`
);
Deno.exit(0);
}
const resolved_root: string = path.resolve(settings.root);
Deno.chdir(resolved_root);
const server = new SERVER({
hostname: settings.hostname,
port: typeof settings.port == 'string' ? parseInt(settings.port) : undefined,
logging: settings.logs
});
Deno.addSignalListener('SIGINT', async () => {
await server.stop();
Deno.exit();
});
await server.start();