UNPKG

webserv

Version:

a quick, flexible, fully typed development server

145 lines 5.43 kB
#!/usr/bin/env node var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; result["default"] = mod; return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; (function (factory) { if (typeof module === "object" && typeof module.exports === "object") { var v = factory(require, exports); if (v !== undefined) module.exports = v; } else if (typeof define === "function" && define.amd) { define(["require", "exports", "path", "yargs", "../config", "../config/utils/config"], factory); } })(function (require, exports) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const path_1 = require("path"); const yargs = __importStar(require("yargs")); const config_1 = __importDefault(require("../config")); const config_2 = require("../config/utils/config"); const argv = yargs .options('config', { alias: 'c', type: 'string', describe: 'load a webserv configuration at the provided location' }) .options('log', { alias: 'l', describe: 'display all logs to the console', type: 'string' }) .option('mode', { alias: 'm', describe: 'use http or https', choices: ['http', 'https'] }) .option('port', { alias: 'p', describe: 'sets the server port to use', type: 'number' }) .option('type', { alias: 't', describe: 'start a predefined service', type: 'array' }) .option('typescript', { alias: 'tsConfig', describe: 'use ts-node to load externals', type: 'boolean' }).argv; function defaultConfig() { if (argv.config) { throw new Error(`Config ${argv.config} not found`); } const serverConfig = { services: [] }; if (!argv.type) { const fileServiceConfig = { name: 'file', routes: { '*': '.' } }; serverConfig.services.push(fileServiceConfig); } return { servers: [serverConfig] }; } function getConfig(name, ...options) { switch (name) { case 'crud': return { route: '*' }; case 'proxy': return { target: options[0] }; case 'file': return { paths: { '*': options[0] } }; case 'upload': return { route: '*', directory: options[0] }; case 'log': return { respondOk: true }; break; default: return {}; } } function run() { return __awaiter(this, void 0, void 0, function* () { const loadedConfig = yield config_2.loadConfig(argv.config); const config = loadedConfig ? loadedConfig.config : defaultConfig(); const workingDirectory = loadedConfig ? path_1.dirname(loadedConfig.configPath) : process.cwd(); const server = config.servers[0]; if (config.servers.length > 1 && (argv.type || argv.port || argv.mode)) { console.warn('Multiple servers defined. Command line arguments will apply to the first server.'); } argv.log && (config.logLevel = argv.log); if (argv.type) { const [name, options] = argv.type.map((val) => String(val)); server.services.push(Object.assign({ name }, getConfig(name, options))); } argv.port && (server.port = argv.port); argv.mode && (server.type = argv.mode); return config_1.default(config, { configPath: workingDirectory }); }); } exports.run = run; run().catch((err) => { console.error('failed to start webserv'); console.error(`reason: ${err.message}`); console.error(err.stack); process.exitCode = 1; process.exit(); }); }); //# sourceMappingURL=webserv.js.map