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

Add publicEndpoint to override resolved endpoint #37

Merged
merged 11 commits into from
Oct 5, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,18 @@ The FRAU app resolver can be run either directly on the console CLI (assuming de

Launching the local app resolver can be as simple as:

```javascript
```sh
frau-local-appresolver --appclass|-c urn:d2l:fra:class:some-app
```

However additional options (described below) can be configured:

```javascript
```sh
frau-local-appresolver --appclass|-c urn:d2l:fra:class:some-app
--configfile|-f appconfig.json
--hostname|-h acme.com
--port|-p 3000
--publicEndpoint|-e https://xyz.ngrok.io
--dist|-d dist
--baseRoute|-b /app
```
Expand All @@ -47,6 +48,7 @@ frau-local-appresolver --appclass|-c urn:d2l:fra:class:some-app
"configFile": "appconfig.json",
"hostname": "acme.com",
"port": "3000",
"publicEndpoint": "https://xyz.ngrok.io",
"dist": "dist",
"baseRoute": "/app"
}
Expand Down Expand Up @@ -78,6 +80,7 @@ var target = appResolver.getUrl();
- `dist` - The directory containing the app files to serve. By default, the `dist` directory is used.
- `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.
- `publicEndpoint` - If provided overrides the protocol (http) hostname and port for endpoint resolution
Copy link
Member

Choose a reason for hiding this comment

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

Another option would be to make the new parameter protocol, so then you could use the existing port and hostname options and just set protocol = https. 🤷

Copy link
Contributor Author

@ktonon ktonon Oct 5, 2021

Choose a reason for hiding this comment

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

I tried that first, but I ran into trouble when trying to run the server. I also had to change the value of the port, which messed up this line:

app.listen(self._opts.port, function() {

I only want to override the value which gets written into the appconfig.json file, i.e. for the loader endpoint. Having a single value to override makes it easier to customize the endpoint from the command line.

For example, in the smart-curriculum project, I'm doing this:

npm run build:config -- --publicEndpoint=https://arkt.ngrok.io

Copy link
Member

Choose a reason for hiding this comment

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

Cool, makes sense!

- `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`.

Expand Down
3 changes: 2 additions & 1 deletion bin/appresolvercli
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ 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] [--baseRoute|-b]')
argv = argv.usage('Usage: frau-local-appresolver --appclass|-c [--configfile|-f] [--hostname|-h] [--port|-p] [--publicEndpoint|-e] [--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('e', 'publicEndpoint')
.alias('d', 'dist')
.alias('b', 'baseRoute')
.argv;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "frau-local-appresolver",
"version": "1.1.0",
"version": "1.2.0",
"description": "A free-range-app utility for resolving locally hosted apps.",
"main": "src/index.js",
"bin": {
Expand Down
5 changes: 4 additions & 1 deletion src/appresolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ function LocalAppRegistry(appClass, opts) {
opts.appClass = appClass;
opts.hostname = getHostname(opts);
opts.port = opts.port || 3000;
opts.publicEndpoint = opts.publicEndpoint;
opts.dist = opts.dist || 'dist';
opts.configFile = opts.configFile || 'appconfig.json';
opts.baseRoute = opts.baseRoute !== undefined ? opts.baseRoute : '/app';
Expand Down Expand Up @@ -81,7 +82,9 @@ LocalAppRegistry.prototype.host = function() {
};

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

LocalAppRegistry.prototype.getConfigUrl = function() {
Expand Down
5 changes: 5 additions & 0 deletions src/optionsProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ module.exports = {
return argv.port ||
process.env.npm_package_config_frauLocalAppResolver_port;
},
getPublicEndpoint: function(argv) {
return argv.publicEndpoint ||
process.env.npm_package_config_frauLocalAppResolver_publicEndpoint;
},
getBaseRoute: function(argv) {
return argv.baseRoute ||
process.env.npm_package_config_frauLocalAppResolver_baseRoute;
Expand All @@ -32,6 +36,7 @@ module.exports = {
dist: this.getDist(argv),
hostname: this.getHostname(argv),
port: this.getPort(argv),
publicEndpoint: this.getPublicEndpoint(argv),
baseRoute: this.getBaseRoute(argv)
};
}
Expand Down
9 changes: 9 additions & 0 deletions test/appresolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@ describe('appresolver', function() {

});

describe('getPublicEndpoint', function() {

it('should override the resolved url', function() {
expect(appresolver(APP_CLASS, { hostname: 'somehost.com', port: 11111, configFile: 'someconf.js', publicEndpoint: 'https://otherhost.com' }).getConfigUrl())
.to.be.equal('https://otherhost.com/app/someconf.js');
});

});

describe('host', function() {

var resolver = appresolver(APP_CLASS, { dist: 'test/testDist', hostname: 'localhost' });
Expand Down