{"size": 1135, "ext": "js", "max_stars_repo_path": "util/config.js", "max_stars_repo_name": "ethanent/coroute", "max_stars_repo_stars_event_min_datetime": "2018-02-26T00:43:31.000Z", "max_stars_repo_stars_event_max_datetime": "2021-04-15T11:45:52.000Z", "max_issues_repo_path": "util/config.js", "max_issues_repo_name": "ethanent/coroute", "max_issues_repo_issues_event_min_datetime": "2018-04-09T20:11:40.000Z", "max_issues_repo_issues_event_max_datetime": "2019-05-25T18:20:39.000Z", "max_forks_repo_path": "util/config.js", "max_forks_repo_name": "ethanent/coroute", "max_forks_repo_forks_event_min_datetime": "2018-03-05T02:26:48.000Z", "max_forks_repo_forks_event_max_datetime": "2020-04-30T00:09:27.000Z"}
{"max_stars_count": 155, "max_issues_count": 2, "max_forks_count": 7, "avg_line_length": 17.196969697, "max_line_length": 78, "alphanum_fraction": 0.5674008811}
const util = require('util')
const fs = require('fs')
const pfs = {
	'readFile': util.promisify(fs.readFile),
	'writeFile': util.promisify(fs.writeFile)
}

const defaultConfig = {
	'listen': {
		'my_https': {
			'https': {
				'key': './key.pem',
				'cert': './cert.pem'
			},
			'port': 443,
			'host': 'localhost'
		},
		'my_http': {
			'host': 'localhost',
			'port': 80
		}
	},
	'route': [
		{
			'from': {
				'server': 'my_http',
				'host': 'example.com'
			},
			'to': 'http://localhost:5135'
		},
		{
			'from': {
				'server': 'my_https',
				'method': 'GET'
			},
			'to': 'http://localhost:5137'
		},
		{
			'from': {},
			'to': 'https://fallback.ethanent.example'
		}
	]
}

module.exports = async (configPath) => {
	let configData = false
	try {
		configData = await pfs.readFile(configPath)
	}
	catch (err) {
		await pfs.writeFile(configPath, JSON.stringify(defaultConfig, null, '\t'))
	}

	if (configData === false) {
		return defaultConfig
	}
	else {
		try {
			return JSON.parse(configData)
		}
		catch (err) {
			console.error('Fatal: Failed to parse config as it contains invalid JSON.')
			process.exit(1)
		}
	}
}