Skip to content

Commit

Permalink
Merge 1fe6e31 into ee3bfa2
Browse files Browse the repository at this point in the history
  • Loading branch information
imcdonald-d2l committed May 12, 2021
2 parents ee3bfa2 + 1fe6e31 commit 605a7c6
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 18 deletions.
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,26 @@ frau-local-appresolver --appclass|-c urn:d2l:fra:class:some-app
However additional options (described below) can be configured:

```javascript
frau-local-appresolver --appclass|-c urn:d2l:fra:class:some-app
--configfile|-f appconfig.json
--hostname|-h acme.com
--port|-p 3000
frau-local-appresolver --appclass|-c urn:d2l:fra:class:some-app
--configfile|-f appconfig.json
--hostname|-h acme.com
--port|-p 3000
--dist|-d dist
--baseRoute|-b app/
```

```javascript
"scripts": {
"resolver": "frau-local-appresolver"
},
"config": {
"config": {
"frauLocalAppResolver": {
"appClass": "urn:d2l:fra:class:some-app",
"configFile": "appconfig.json",
"hostname": "acme.com",
"port": "3000",
"dist": "dist"
"dist": "dist",
"baseRoute": "app/"
}
}
```
Expand Down Expand Up @@ -77,6 +79,7 @@ var target = appResolver.getUrl();
- `port` - The port to listen on. By default, port `3000` is used, which is the port that the LMS expects it on.
- `hostname` - The hostname (or IP) to listen on. By default, the hostname of the operating system is used. You should not need to change this.
- `configFile` - The name of the app config file. By default, `appconfig.json` is used. You should not need to change this.
- `baseRoute` - Specifies the base route to be included in urls. By default, `app/` is used. Setting this to different values (e.g. `''`) will allow you to use tools such as `es-dev-server` where you want the endpoint hosted at `http://localhost:3000/index.html` instead of `http://localhost:3000/app/index.html`.

## Contributing
Contributions are welcome, please submit a pull request!
Expand Down
3 changes: 2 additions & 1 deletion bin/appresolvercli
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ var chalk = require('chalk'),
argv = require('yargs')
optionsProvider = require('../src/optionsProvider');

argv = argv.usage('Usage: frau-local-appresolver --appclass|-c [--configfile|-f] [--hostname|-h] [--port|-p] [--dist|-d]')
argv = argv.usage('Usage: frau-local-appresolver --appclass|-c [--configfile|-f] [--hostname|-h] [--port|-p] [--dist|-d] [--baseRoute|-b]')
.example('frau-local-appresolver -c urn:d2l:fra:class:some-app -f appconfig.json -h acme.com -p 3000 -d dist')
.alias('c', 'appclass')
.alias('f', 'configfile')
.alias('h', 'hostname')
.alias('p', 'port')
.alias('d', 'dist')
.alias('b', 'baseRoute')
.argv;

var appResolver = require('../src/appresolver');
Expand Down
5 changes: 3 additions & 2 deletions src/appresolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ function LocalAppRegistry(appClass, opts) {
opts.port = opts.port || 3000;
opts.dist = opts.dist || 'dist';
opts.configFile = opts.configFile || 'appconfig.json';
opts.baseRoute = opts.baseRoute !== undefined ? opts.baseRoute : '/app';

this._opts = opts;
}
Expand All @@ -55,7 +56,7 @@ LocalAppRegistry.prototype.host = function() {

app.use(cors());

app.use('/app', serveStatic(self._opts.dist));
app.use(this._opts.baseRoute, serveStatic(self._opts.dist));

var encodedAppClass = encodeURIComponent(self._opts.appClass);
app.get('/resolve/' + encodedAppClass, function(req, res) {
Expand All @@ -80,7 +81,7 @@ LocalAppRegistry.prototype.host = function() {
};

LocalAppRegistry.prototype.getUrl = function() {
return 'http://' + this._opts.hostname + ':' + this._opts.port + '/app/';
return 'http://' + this._opts.hostname + ':' + this._opts.port + this._opts.baseRoute + '/';
};

LocalAppRegistry.prototype.getConfigUrl = function() {
Expand Down
7 changes: 6 additions & 1 deletion src/optionsProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,18 @@ module.exports = {
return argv.port ||
process.env.npm_package_config_frauLocalAppResolver_port;
},
getBaseRoute: function(argv) {
return argv.baseRoute ||
process.env.npm_package_config_frauLocalAppResolver_baseRoute;
},
getOptions: function(argv) {
return {
appClass: this.getAppClass(argv),
configFile: this.getConfigFile(argv),
dist: this.getDist(argv),
hostname: this.getHostname(argv),
port: this.getPort(argv)
port: this.getPort(argv),
baseRoute: this.getBaseRoute(argv)
};
}
};
28 changes: 20 additions & 8 deletions test/appresolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,6 @@ describe('appresolver', function() {
}).to.throw(Error, 'appClass is a required argument for LocalAppResolver.');
});

it('hostname', function() {
var hostname = appresolver(APP_CLASS)._opts.hostname;
expect(hostname)
.to.have.string(require('os').hostname().replace('.local', ''));
expect(hostname)
.to.not.have.string('.local');
});

it('port', function() {
expect(appresolver(APP_CLASS)._opts.port)
.to.be.equal(DEFAULT_PORT);
Expand All @@ -53,6 +45,11 @@ describe('appresolver', function() {
.to.be.equal('appconfig.json');
});

it('baseRoute', function() {
expect(appresolver(APP_CLASS)._opts.baseRoute)
.to.be.equal('/app');
});

});

describe('hostname', function() {
Expand All @@ -62,6 +59,11 @@ describe('appresolver', function() {
.to.be.equal('http://somehost:' + DEFAULT_PORT + '/app/');
});

it('should strip ".local" domain from OSX hostname - with baseRoute', function() {
expect(appresolver(APP_CLASS, { hostname: 'somehost.local', baseRoute: '' }).getUrl())
.to.be.equal('http://somehost:' + DEFAULT_PORT + '/');
});

});

describe('getUrl', function() {
Expand All @@ -71,6 +73,11 @@ describe('appresolver', function() {
.to.be.equal('http://somehost.com:11111/app/');
});

it('should return expected url - with baseRoute', function() {
expect(appresolver(APP_CLASS, { hostname: 'somehost.com', port: 11111, baseRoute: '' }).getUrl())
.to.be.equal('http://somehost.com:11111/');
});

});

describe('getConfigUrl', function() {
Expand All @@ -80,6 +87,11 @@ describe('appresolver', function() {
.to.be.equal('http://somehost.com:11111/app/someconf.js');
});

it('should return expected url - with baseRoute', function() {
expect(appresolver(APP_CLASS, { hostname: 'somehost.com', port: 11111, configFile: 'someconf.js', baseRoute: '' }).getConfigUrl())
.to.be.equal('http://somehost.com:11111/someconf.js');
});

});

describe('host', function() {
Expand Down

0 comments on commit 605a7c6

Please sign in to comment.