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 all commits
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: 7 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version: 2
updates:
- package-ecosystem: npm
directory: "/"
schedule:
interval: daily
versioning-strategy: widen
15 changes: 15 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: CI
on: pull_request
jobs:
test:
timeout-minutes: 5
runs-on: ubuntu-latest
steps:
- uses: Brightspace/third-party-actions@actions/checkout
- uses: Brightspace/third-party-actions@actions/setup-node
- name: Install dependencies
run: npm install
- name: Lint and Test
run: npm run test
env:
FORCE_COLOR: 3
23 changes: 23 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Release
on:
push:
branches:
- master
jobs:
release:
name: Release
timeout-minutes: 5
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: Brightspace/third-party-actions@actions/checkout
with:
persist-credentials: false
- name: Setup Node
uses: Brightspace/third-party-actions@actions/setup-node
- name: Semantic Release
uses: BrightspaceUI/actions/semantic-release@master
with:
GITHUB_TOKEN: ${{ secrets.D2L_GITHUB_TOKEN }}
NPM: true
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
node_modules/
coverage/
9 changes: 0 additions & 9 deletions .npmignore

This file was deleted.

13 changes: 0 additions & 13 deletions .travis.yml

This file was deleted.

3 changes: 1 addition & 2 deletions CODEOWNERS
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
* @dlockhart
* @dbatiste
* @dlockhart @dbatiste
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ recommend that a file or class name and description of purpose be included on
the same "printed page" as the copyright notice for easier identification within
third-party archives.

Copyright 2014 D2L Corporation
Copyright 2021 D2L Corporation

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
58 changes: 46 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# frau-local-appresolver

[![NPM version][npm-image]][npm-url]
[![Build status][ci-image]][ci-url]
[![Coverage Status][coverage-image]][coverage-url]
[![Dependency Status][dependencies-image]][dependencies-url]

A free-range-app utility for resolving locally hosted apps.

Expand All @@ -22,22 +19,23 @@ 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
```

```javascript
```json
"scripts": {
"resolver": "frau-local-appresolver"
},
Expand All @@ -47,6 +45,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,10 +77,51 @@ 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`.

## Versioning & Releasing

> TL;DR: Commits prefixed with `fix:` and `feat:` will trigger patch and minor releases when merged to `main`. Read on for more details...

The [sematic-release GitHub Action](https://github.com/BrightspaceUI/actions/tree/master/semantic-release) is called from the `release.yml` GitHub Action workflow to handle version changes and releasing.

### Version Changes

All version changes should obey [semantic versioning](https://semver.org/) rules:
1. **MAJOR** version when you make incompatible API changes,
2. **MINOR** version when you add functionality in a backwards compatible manner, and
3. **PATCH** version when you make backwards compatible bug fixes.

The next version number will be determined from the commit messages since the previous release. Our semantic-release configuration uses the [Angular convention](https://github.com/conventional-changelog/conventional-changelog/tree/master/packages/conventional-changelog-angular) when analyzing commits:
* Commits which are prefixed with `fix:` or `perf:` will trigger a `patch` release. Example: `fix: validate input before using`
* Commits which are prefixed with `feat:` will trigger a `minor` release. Example: `feat: add toggle() method`
* To trigger a MAJOR release, include `BREAKING CHANGE:` with a space or two newlines in the footer of the commit message
* Other suggested prefixes which will **NOT** trigger a release: `build:`, `ci:`, `docs:`, `style:`, `refactor:` and `test:`. Example: `docs: adding README for new component`

To revert a change, add the `revert:` prefix to the original commit message. This will cause the reverted change to be omitted from the release notes. Example: `revert: fix: validate input before using`.

### Releases

When a release is triggered, it will:
* Update the version in `package.json`
* Tag the commit
* Create a GitHub release (including release notes)
* Deploy a new package to NPM

### Releasing from Maintenance Branches

Occasionally you'll want to backport a feature or bug fix to an older release. `semantic-release` refers to these as [maintenance branches](https://semantic-release.gitbook.io/semantic-release/usage/workflow-configuration#maintenance-branches).

Maintenance branch names should be of the form: `+([0-9])?(.{+([0-9]),x}).x`.

Regular expressions are complicated, but this essentially means branch names should look like:
* `1.15.x` for patch releases on top of the `1.15` release (after version `1.16` exists)
* `2.x` for feature releases on top of the `2` release (after version `3` exists)

## Contributing

Contributions are welcome, please submit a pull request!

### Code Style
Expand All @@ -91,9 +131,3 @@ contributions should make use of them.

[npm-url]: https://www.npmjs.org/package/frau-local-appresolver
[npm-image]: https://img.shields.io/npm/v/frau-local-appresolver.svg
[ci-url]: https://travis-ci.org/Brightspace/frau-local-appresolver
[ci-image]: https://img.shields.io/travis-ci/Brightspace/frau-local-appresolver.svg
[coverage-url]: https://coveralls.io/r/Brightspace/frau-local-appresolver?branch=master
[coverage-image]: https://img.shields.io/coveralls/Brightspace/frau-local-appresolver.svg
[dependencies-url]: https://david-dm.org/brightspace/frau-local-appresolver
[dependencies-image]: https://img.shields.io/david/Brightspace/frau-local-appresolver.svg
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
41 changes: 19 additions & 22 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
"frau-local-appresolver": "bin/appresolvercli"
},
"scripts": {
"lint": "npm run lint:src && npm run lint:test",
"lint:src": "eslint src",
"lint:test": "eslint test",
"report-coverage": "coveralls < ./coverage/lcov.info",
"test:unit": "istanbul cover node_modules/mocha/bin/_mocha -- --recursive",
"test": "npm run lint -s && npm run test:unit -s"
"lint": "eslint .",
"test:unit": "mocha",
"test": "npm run lint && npm run test:unit"
},
"files": [
"/bin",
"/src"
],
"repository": {
"type": "git",
"url": "https://github.com/Brightspace/frau-local-appresolver.git"
Expand All @@ -29,23 +30,19 @@
},
"homepage": "https://github.com/Brightspace/frau-local-appresolver",
"dependencies": {
"chalk": "^2.0.1",
"cors": "^2.8.4",
"deasync": "^0.1.13",
"express": "^4.15.3",
"serve-static": "^1.12.3",
"superagent-d2l-cors-proxy": "^0.3.0",
"yargs": "^8.0.2"
"chalk": "^4",
"cors": "^2",
"deasync": "^0.1",
"express": "^4",
"serve-static": "^1",
"superagent-d2l-cors-proxy": "^0.3",
"yargs": "^17"
},
"devDependencies": {
"chai": "^4.1.0",
"coveralls": "^2.13.1",
"eslint": "^4.3.0",
"eslint-config-brightspace": "0.3.1",
"istanbul": "^0.4.5",
"mocha": "^3.4.2",
"request": "^2.81.0",
"sinon": "^2.4.1",
"sinon-chai": "^2.12.0"
"chai": "^4",
"eslint": "^7",
"eslint-config-brightspace": "^0.16",
"mocha": "^9",
"request": "^2"
}
}
31 changes: 20 additions & 11 deletions src/appresolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,6 @@ var corsProxy = require('superagent-d2l-cors-proxy'),
dns = require('dns'),
deasync = require('deasync');

function getHostname(opts) {
var hostname = opts.hostname || getFQDN() || os.hostname();
if (hostname.indexOf('.local', hostname.length - 6) !== -1) {
hostname = hostname.substr(0, hostname.length - 6);
}
return hostname;
}

var getFQDN = deasync(function(cb) {
var uqdn = os.hostname();
dns.lookup(uqdn, { hints: dns.ADDRCONFIG }, function(err, ip) {
Expand All @@ -29,6 +21,14 @@ var getFQDN = deasync(function(cb) {
});
});

function getHostname(opts) {
var hostname = opts.hostname || getFQDN() || os.hostname();
if (hostname.indexOf('.local', hostname.length - 6) !== -1) {
hostname = hostname.substr(0, hostname.length - 6);
}
return hostname;
}

function LocalAppRegistry(appClass, opts) {

if (!appClass) {
Expand Down Expand Up @@ -71,17 +71,26 @@ LocalAppRegistry.prototype.host = function() {
);

return new Promise(function(resolve, reject) {
app.listen(self._opts.port, function() {
self._server = app.listen(self._opts.port, function() {
resolve();
}).on('error', function(err) {
});
self._server.on('error', function(err) {
reject(err);
});
});

};

LocalAppRegistry.prototype.close = function() {
if (this._server) {
this._server.close();
}
};

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
Loading