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

Created Sails.js example #109

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion docs/index.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions examples/sails/config/local.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Local environment settings
*
* For more information, check out:
* https://sailsjs.com/docs/concepts/configuration/the-local-js-file
*/

module.exports = {
// Configuration options for ngrok.js script.
ngrok: {
// auth: 'username:notSoSecretPassword', // Uncomment to set a basic-auth user/password for the Ngrok tunnel. Password must be between 8 and 128 characters.
domain: 'my-ngrok-app.ngrok-free.app',
token: '{{my_ngrok_token_here}}',
port: 4242 // This sets Sails' port when running `ngrok.js`.
},

// The port to attach the API to. This does NOT affect the `ngrok.js` script.
port: 1337,
};
21 changes: 21 additions & 0 deletions examples/sails/config/ngrok.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* This file is used to configure the `ngrok.js` script.
*/

module.exports.ngrok = {
// Set an HTTP basic-auth wall for the app.
auth: process.env.NGROK_BASIC || undefined, // Use a string of 'username:password' style (raw password)

// Default Ngrok authtoken, to tie to your account.
// https://dashboard.ngrok.com/get-started/your-authtoken
token: process.env.NGROK_AUTHTOKEN || process.env.NGROK_TOKEN || undefined, // NEVER store PRODUCTION secrets in Git-controlled files!

// The static domain to use for the Ngrok tunnel. Something like: 'running-grey-gazelle.ngrok-free.app'
domain: process.env.NGROK_DOMAIN || undefined,

// The default region for the Ngrok tunnel.
region: process.env.NGROK_REGION || undefined,

// The default port to start Sails for the Ngrok tunnel.
port: process.env.PORT || 4242 // Use 4242 instead of 1337, so we can run 2 instances if we want.
};
103 changes: 103 additions & 0 deletions examples/sails/ngrok.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#!/usr/bin/env node

/**
*
* Original version of this example can be found here:
* https://github.com/neonexus/sails-react-bootstrap-webpack/blob/release/ngrok.js
*
*/

/**
* This is the file used to lift Sails, and start Ngrok.
*
* Run it via node: `node ngrok.js`
* Run it directly: `./ngrok.js` (if file is marked as executable)
*/

const moduleLoader = require('sails/lib/hooks/moduleloader');
const path = require('path');
const rc = require('sails/accessible/rc');
const ngrok = require('@ngrok/ngrok');
const sails = require('sails');
const {spawn} = require('child_process');

// Load configuration the way Sails would.
moduleLoader({
config: {
environment: process.env.NODE_ENV || 'development',
paths: {
config: path.join(__dirname, 'config')
}
}
}).loadUserConfig((err, config) => {
if (err) {
console.error('');
console.error('There was an issue loading user configuration:');
console.error('');
console.error(err);
console.error('');

return process.exit(1);
}

// Set Ngrok defaults. These can be overwritten in `config/ngrok.js` or `config/local.js`.
// Basically, this is just a safety net, should one delete the `config/ngrok.js` file.
config = {
ngrok: {
auth: process.env.NGROK_BASIC || undefined, // Basic auth for the Ngrok tunnel.
token: process.env.NGROK_AUTHTOKEN || process.env.NGROK_TOKEN || undefined, // The Ngrok token for your account.
domain: process.env.NGROK_DOMAIN || undefined, // The Ngrok domain to use.
region: process.env.NGROK_REGION || undefined, // Defaults to Global.
port: process.env.PORT || 4242 // The port to start Sails on.
},
...config
};

ngrok.forward({
addr: config.ngrok.port, // This is actually the port to we'll use for Sails. Ngrok will handle its own ports.
authtoken: config.ngrok.token,
basic_auth: config.ngrok.auth, // eslint-disable-line
domain: config.ngrok.domain,
region: config.ngrok.region,
schemes: ['HTTPS']
}).then((listener) => {
let origins;
const ngrokUrl = listener.url();

// Add the Ngrok URL to our allowed origins.
if (config.security && config.security.cors && config.security.cors.allowOrigins) {
origins = [...config.security.cors.allowOrigins];

if (!config.security.cors.allowOrigins.includes(ngrokUrl)) {
origins.push(ngrokUrl);
}
} else {
origins = [ngrokUrl];
}

// Start Sails with some configuration overrides.
sails.lift({
...rc('sails'),
port: config.ngrok.port,
security: {
cors: {
allowOrigins: origins
}
}
}, (err) => {
if (err) {
console.error(err);

return process.exit(1);
}

// Hurray! We are up and running!
});
}).catch((e) => {
console.log('');
console.log('There was an error starting the Ngrok tunnel. Here is the error:');
console.log('');
console.log(e.message);
console.log('');
});
});
6 changes: 6 additions & 0 deletions examples/sails/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"dependencies": {
"@ngrok/ngrok": "^0.*",
"sails": "^1.*"
}
}