Skip to content

Commit

Permalink
Add support for specifying mount points in the .cncrc file (cncjs#459) (
Browse files Browse the repository at this point in the history
  • Loading branch information
cheton authored Apr 20, 2019
1 parent 7ea8f4a commit fd8ad3f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,16 @@ pi@rpi3$ cncjs -h
Instead of passing command line options for `--watch-directory`, `--access-token-lifetime`, `--allow-remote-access`, and `--controller`, you can create a `~/.cncrc` file that contains the following configuration in JSON format:
```json
{
"mountPoints": [
{
"route": "/pendant",
"target": "/home/pi/tinyweb"
},
{
"route": "/widget",
"target": "https://cncjs.github.io/cncjs-widget-boilerplate/v1/"
}
],
"watchDirectory": "/path/to/dir",
"accessTokenLifetime": "30d",
"allowRemoteAccess": false,
Expand Down Expand Up @@ -206,6 +216,12 @@ See https://github.com/cncjs/cncjs/issues/242#issuecomment-352294549 for a detai
}
],
"baudrates": [115200, 250000],
"mountPoints": [
{
"route": "/widget",
"target": "https://cncjs.github.io/cncjs-widget-boilerplate/v1/"
}
],
"watchDirectory": "/path/to/dir",
"accessTokenLifetime": "30d",
"allowRemoteAccess": false,
Expand Down
6 changes: 6 additions & 0 deletions examples/.cncrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
}
],
"baudrates": [115200, 250000],
"mountPoints": [
{
"route": "/widget",
"target": "https://cncjs.github.io/cncjs-widget-boilerplate/v1/"
}
],
"watchDirectory": "/path/to/dir",
"accessTokenLifetime": "30d",
"allowRemoteAccess": false,
Expand Down
11 changes: 8 additions & 3 deletions src/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import app from './app';
import cncengine from './services/cncengine';
import monitor from './services/monitor';
import config from './services/configstore';
import { ensureString } from './lib/ensure-type';
import logger from './lib/logger';
import urljoin from './lib/urljoin';

Expand Down Expand Up @@ -100,15 +101,19 @@ const createServer = (options, callback) => {
}

const { port = 0, host, backlog } = options;
const mountPoints = [
...ensureArray(options.mountPoints),
...ensureArray(config.get('mountPoints'))
];
const routes = [];

ensureArray(options.mountPoints).forEach(mount => {
mountPoints.forEach(mount => {
if (!mount || !mount.route || mount.route === '/') {
log.error(`Must specify a valid route path ${JSON.stringify(mount.route)}.`);
return;
}

if (mount.target.match(/^(http|https):\/\//i)) {
if (ensureString(mount.target).match(/^(http|https):\/\//i)) {
log.info(`Starting a proxy server to proxy all requests starting with ${chalk.yellow(mount.route)} to ${chalk.yellow(mount.target)}`);

routes.push({
Expand Down Expand Up @@ -192,7 +197,7 @@ const createServer = (options, callback) => {
});
} else {
// expandTilde('~') => '/Users/<userhome>'
const directory = expandTilde(mount.target || '').trim();
const directory = expandTilde(ensureString(mount.target)).trim();

log.info(`Mounting a directory ${chalk.yellow(JSON.stringify(directory))} to serve requests starting with ${chalk.yellow(mount.route)}`);

Expand Down

0 comments on commit fd8ad3f

Please sign in to comment.