ffffng/server/config.js

159 lines
4.4 KiB
JavaScript
Raw Permalink Normal View History

2014-05-12 20:08:19 +02:00
'use strict';
2018-12-17 22:49:54 +01:00
module.exports = (() => {
const commandLineArgs = require('command-line-args');
const commandLineUsage = require('command-line-usage');
const commandLineDefs = [
{ name: 'help', alias: 'h', type: Boolean, description: 'Show this help' },
{ name: 'config', alias: 'c', type: String, description: 'Location of config.json' },
{ name: 'version', alias: 'v', type: Boolean, description: 'Show ffffng version' }
];
let commandLineOptions;
try {
commandLineOptions = commandLineArgs(commandLineDefs);
} catch (e) {
console.error(e.message);
console.error('Try \'--help\' for more information.');
process.exit(1);
}
2016-05-24 16:40:57 +02:00
2018-12-17 22:49:54 +01:00
const fs = require('graceful-fs');
2016-05-24 19:40:02 +02:00
2018-12-17 22:49:54 +01:00
const packageJsonFile = __dirname + '/../package.json';
let version = 'unknown';
if (fs.existsSync(packageJsonFile)) {
version = JSON.parse(fs.readFileSync(packageJsonFile, 'utf8')).version;
}
function usage () {
console.log(commandLineUsage([
{
header: 'ffffng - ' + version + ' - Freifunk node management form',
optionList: commandLineDefs
}
2018-12-17 22:49:54 +01:00
]));
}
2018-12-17 22:49:54 +01:00
if (commandLineOptions.help) {
usage();
process.exit(0);
}
if (commandLineOptions.version) {
console.log('ffffng - ' + version);
process.exit(0);
2016-05-20 22:38:13 +02:00
}
2018-12-17 22:49:54 +01:00
if (!commandLineOptions.config) {
usage();
process.exit(1);
}
2018-12-17 22:49:54 +01:00
const deepExtend = require('deep-extend');
const defaultConfig = {
server: {
baseUrl: 'http://localhost:8080',
port: 8080,
databaseFile: '/tmp/ffffng.sqlite',
peersPath: '/tmp/peers',
logging: {
directory: '/tmp/logs',
debug: false,
profile: false,
logRequests: false
},
internal: {
active: false,
user: 'admin',
password: 'secret'
},
email: {
from: 'Freifunk Knotenformular <no-reply@musterstadt.freifunk.net>',
// For details see: https://nodemailer.com/2-0-0-beta/setup-smtp/
smtp: {
host: 'mail.example.com',
port: '465',
secure: true,
auth: {
user: 'user@example.com',
pass: 'pass'
}
}
},
map: {
nodesJsonUrl: ['http://map.musterstadt.freifunk.net/nodes.json']
}
},
client: {
community: {
name: 'Freifunk Musterstadt',
domain: 'musterstadt.freifunk.net',
contactEmail: 'kontakt@musterstadt.freifunk.net',
sites: [],
domains: []
},
legal: {
privacyUrl: null,
imprintUrl: null
},
map: {
mapUrl: 'http://map.musterstadt.freifunk.net'
},
monitoring: {
enabled: false
},
coordsSelector: {
showInfo: false,
showBorderForDebugging: false,
localCommunityPolygon: [],
lat: 53.565278,
lng: 10.001389,
defaultZoom: 10,
layers: {}
}
}
};
2016-05-20 22:38:13 +02:00
2018-12-17 22:49:54 +01:00
const configJSONFile = commandLineOptions.config;
let configJSON = {};
2017-05-06 17:31:34 +02:00
2018-12-17 22:49:54 +01:00
if (fs.existsSync(configJSONFile)) {
configJSON = JSON.parse(fs.readFileSync(configJSONFile, 'utf8'));
} else {
console.error('config.json not found: ' + configJSONFile);
process.exit(1);
}
2018-12-17 22:49:54 +01:00
const _ = require('lodash');
2016-05-20 22:38:13 +02:00
2018-12-17 22:49:54 +01:00
function stripTrailingSlash (obj, field) {
const url = obj[field];
if (_.isString(url) && _.last(url) === '/') {
obj[field] = url.substr(0, url.length - 1);
}
}
const config = deepExtend({}, defaultConfig, configJSON);
2018-12-17 22:49:54 +01:00
stripTrailingSlash(config.server, 'baseUrl');
stripTrailingSlash(config.client.map, 'mapUrl');
2018-12-17 22:49:54 +01:00
const url = require('url');
config.server.rootPath = url.parse(config.server.baseUrl).pathname;
config.client.rootPath = config.server.rootPath;
2018-12-17 22:49:54 +01:00
return {
config,
version
}
})()