Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: allow server configuration, plugin configuration and defaults to be separate from the server install #232

Merged
merged 7 commits into from
Sep 2, 2017
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,29 @@ You can also configure the path to the settings file with environment variable `

The http port can be configured separately with environment variable `PORT`. You can also [run on port 80 with systemd](https://github.com/tkurki/marinepi-provisioning/blob/d3d624629799a3b96234a90fc42bc22dae4fd3a2/roles/node-app/templates/node_app_systemd_socket.j2). Environment variable NMEA0183PORT sets the NMEA 0183 tcp port.

Storing Configuration Outside The Server Install Directory
==========================================================
You can store configuration like the settings file, plugin cofiguration, defaults.js and the mapcache in a directory outside of the server install using the `-c` option (or the `SIGNALK_NODE_CONDFIG_DIR` env variable).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo


By default, the server will look for a `settings.json` and a `defaults.json` file in the given directory.

For example, `./bin/signalk-server -c /usr/local/etc/node_server_config`

In this case, the server would look for the settings file at `/usr/local/etc/node_server_config/settings.json`

You can overwrite the default settings file name by specifying the -s argument.

For example, ./bin/signalk-server -c /usr/local/etc/node_server_config -s test_settings.json`

In this case, the server would look for the settings file at `/usr/local/etc/node_server_config/test_settings.json`

You should also put the charts mapcache directory at `/usr/local/etc/node_server_config/public/mapcache`.


Environment variables
---------------------
- `SIGNALK_NODE_SETTINGS` override the path to the settings file
- `SIGNALK_NODE_CONDFIG_DIR` override the path to find server configuration files
- `PORT` override the port for http/ws service
- `EXTERNALPORT` the port used in /signalk response and Bonjour advertisement. Has precedence over configuration file.
- `EXTERNALHOST` the host used in /signalk response and Bonjour advertisement. Has precedence over configuration file.
Expand All @@ -71,6 +91,7 @@ Environment variables
- `TCPSTREAMADDRESS` override the address the Signal K Stream (deltas) over TCP is listening on
- `DISABLEPLUGINS` disable all plugins so that they can not be enabled


Real Inputs
---------------
To hook the server up to your real inputs you need to create a configuration file that connects to your input source and applies the relevant parsers / converters in the provider pipeline.
Expand Down
14 changes: 11 additions & 3 deletions lib/config/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ module.exports = function(app) {
return path.join(app.config.appPath, app.overrides.settings);
}

var settingsFile = argv.s || 'settings/settings.json';
var settingsFile = argv.s || (argv.c ? 'settings.json' : 'settings/settings.json');

return path.join(app.config.appPath, settingsFile);
return path.join(app.config.configPath, settingsFile);
}

if(app.overrides.settings !== null && typeof app.overrides.settings === 'object') {
Expand All @@ -39,6 +39,13 @@ module.exports = function(app) {
} else {
app.__argv = process.argv.slice(2);
app.argv = require('minimist')(app.__argv);

if(process.env.SIGNALK_NODE_CONDFIG_DIR) {
app.config.configPath = path.resolve(process.env.SIGNALK_NODE_CONDFIG_DIR)
} else {
app.config.configPath = app.argv.c || app.config.appPath
}

var settings = getSettingsFilename(app.argv);

debug("Using settings file: " + settings);
Expand All @@ -51,7 +58,8 @@ module.exports = function(app) {
}

try {
app.config.defaults = require('../../settings/defaults.json');
var defaultsFile = app.argv.c ? 'defaults.json' : 'settings/defaults.json';
app.config.defaults = require(path.join(app.config.configPath, defaultsFile));
} catch(e) {
console.error("No settings/defaults.json available")
}
Expand Down
10 changes: 9 additions & 1 deletion lib/interfaces/charts.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ const versionPrefix = "/v1";
const apiPathPrefix = pathPrefix + versionPrefix + "/api/";
const parseStringAsync = Promise.promisify(require("xml2js").parseString);
const path = require("path");
const chartBaseDir = path.join(__dirname, "..", "..", "public", "mapcache");
const express = require('express');
var chartBaseDir

const chartProviders = {};
loadCharts();
Expand All @@ -35,6 +36,13 @@ module.exports = function(app) {
api.start = function() {
debug("Starting charts interface");

chartBaseDir = path.join(app.config.configPath, "public", "mapcache");

if ( app.config.configPath != app.config.appPath ) {
debug("using mapcache from config dir")
app.use('/mapcache', express.static(chartBaseDir));
}

app.get(apiPathPrefix + "resources/charts/*", (req, res, next) => {
loadCharts(app, req).then(charts => {
var parts = req.path.split("/");
Expand Down
7 changes: 4 additions & 3 deletions lib/interfaces/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module.exports = function(app) {
start: function() {
startPlugins(app)

ensureExists(path.join(__dirname, "../../plugin-config-data"));
ensureExists(path.join(app.config.configPath, "plugin-config-data"));

app.use('/plugins/configure', express.static(path.join(__dirname, '/../../plugin-config/public')));

Expand Down Expand Up @@ -61,7 +61,6 @@ module.exports = function(app) {
});
}
}
};

function ensureExists(dir) {
if(!fs.existsSync(dir)) {
Expand All @@ -70,7 +69,7 @@ function ensureExists(dir) {
}

function pathForPluginId(id) {
return path.join(__dirname, "../../plugin-config-data", id + '.json')
return path.join(app.config.configPath, "plugin-config-data", id + '.json')
}

function savePluginOptions(pluginId, data, callback) {
Expand Down Expand Up @@ -169,3 +168,5 @@ function registerPlugin(app, pluginName, metadata) {
app.use("/signalk/v1/api", plugin.signalKApiRoutes(express.Router()))
}
}
};

12 changes: 11 additions & 1 deletion providers/filestream.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

var path = require('path');
var PassThrough = require('stream').PassThrough;
const fs = require('fs');

function EndIgnoringPassThrough() {
PassThrough.call(this);
Expand All @@ -59,7 +60,16 @@ FileStream.prototype.pipe = function(pipeTo) {
}

FileStream.prototype.startStream = function() {
var filename = path.isAbsolute(this.options.filename) ? this.options.filename : path.join(__dirname, '..', this.options.filename);
var filename
if ( path.isAbsolute(this.options.filename) ) {
filename = this.options.filename
} else {
filename = path.join(this.options.app.config.configPath, this.options.filename)
if( !fs.existsSync(filename) ) {
filename = path.join(__dirname, '..', this.options.filename)
}
}

this.filestream = require('fs').createReadStream(filename);
if(this.keepRunning) {
this.filestream.on('end', this.startStream.bind(this));
Expand Down