export async function getConfig()

in experimental/traffic-portal/server.config.ts [398:507]


export async function getConfig(args: Args, ver: ServerVersion): Promise<ServerConfig> {
	let cfg = defaultConfig;
	cfg.version = ver;

	let readFromFile = false;
	try {
		const cfgFromFile = JSON.parse(await readFile(args.configFile, {encoding: "utf8"}));
		if (isConfig(cfgFromFile)) {
			cfg = cfgFromFile;
			cfg.version = ver;
		} else {
			throw new Error("bad contents; doesn't represent a configuration file");
		}
		readFromFile = true;
	} catch (err) {
		const msg = `invalid configuration file at '${args.configFile}'`;
		if (err instanceof Error) {
			if (!isSystemError(err) || (err.code !== "ENOENT" || args.configFile !== defaultConfigFile)) {
				throw new Error(`${msg}: ${err.message}`);
			}
		} else {
			throw new Error(`${msg}: ${err}`);
		}
	}

	if(args.browserFolder !== defaultConfig.browserFolder) {
		cfg.browserFolder = args.browserFolder;
	}

	try {
		if (!(await readdir(cfg.browserFolder)).includes("index.html")) {
			throw new Error("directory doesn't include an 'index.html' file");
		}
	} catch (e) {
		throw new Error(`setting browser directory: ${e instanceof Error ? e.message : e}`);
	}

	if(args.port !== defaultConfig.port) {
		cfg.port = args.port;
	}
	if (isNaN(cfg.port) || cfg.port <= 0 || cfg.port > 65535) {
		throw new Error(`invalid port: ${cfg.port}`);
	}

	if (args.trafficOps) {
		cfg.trafficOps = args.trafficOps;
	} else if (!readFromFile) {
		const envURL = process.env.TO_URL;
		if (!envURL) {
			throw new Error("Traffic Ops URL must be specified");
		}
		try {
			cfg.trafficOps = new URL(envURL);
		} catch (e) {
			throw new Error(`invalid Traffic Ops URL from environment: ${envURL}`);
		}
	}

	if (args.tpv1Url) {
		cfg.tpv1Url = args.tpv1Url;
	}

	if (readFromFile && cfg.useSSL) {
		if (args.certPath) {
			cfg.certPath = args.certPath;
		}
		if (args.keyPath) {
			cfg.keyPath = args.keyPath;
		}
	} else if (!readFromFile || cfg.useSSL === undefined) {
		if (args.certPath) {
			if (!args.keyPath) {
				throw new Error("must specify either both a key path and a cert path, or neither");
			}
			cfg = {
				browserFolder: cfg.browserFolder,
				certPath: args.certPath,
				certificateAuthPaths: [],
				insecure: cfg.insecure,
				keyPath: args.keyPath,
				port: cfg.port,
				tpv1Url: cfg.tpv1Url,
				trafficOps: cfg.trafficOps,
				useSSL: true,
				version: ver
			};
		} else if (args.keyPath) {
			throw new Error("must specify either both a key path and a cert path, or neither");
		}
	}

	if(args.insecure) {
		cfg.insecure = args.insecure;
	}

	if (cfg.useSSL) {
		try {
			await access(cfg.certPath, constants.R_OK);
		} catch (e) {
			throw new Error(`checking certificate file "${cfg.certPath}": ${e instanceof Error ? e.message : e}`);
		}
		try {
			await access(cfg.keyPath, constants.R_OK);
		} catch (e) {
			throw new Error(`checking key file "${cfg.keyPath}": ${e instanceof Error ? e.message : e}`);
		}
	}

	return cfg;
}