Skip to content

Adds support for using Parse Dashboard as Express middleware #209

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

Closed
wants to merge 3 commits into from
Closed
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
114 changes: 114 additions & 0 deletions Parse-Dashboard/cli/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* Copyright (c) 2016-present, Parse, LLC
* All rights reserved.
*
* This source code is licensed under the license found in the LICENSE file in
* the root directory of this source tree.
*/
"use strict";
const packageJson = require('package-json');
const basicAuth = require('basic-auth');
const path = require('path');
const jsonFile = require('json-file-plus');

var ParseDashboard = require('../index.js');
// Command line tool for npm start

const program = require('commander');
program.option('--appId [appId]', 'the app Id of the app you would like to manage.');
program.option('--masterKey [masterKey]', 'the master key of the app you would like to manage.');
program.option('--serverURL [serverURL]', 'the server url of the app you would like to manage.');
program.option('--appName [appName]', 'the name of the app you would like to manage. Optional.');
program.option('--config [config]', 'the path to the configuration file');
program.option('--port [port]', 'the port to run parse-dashboard');
program.option('--allowInsecureHTTP [allowInsecureHTTP]', 'set this flag when you are running the dashboard behind an HTTPS load balancer or proxy with early SSL termination.');

program.parse(process.argv);


const port = program.port || process.env.PORT || 4040;
const allowInsecureHTTP = program.allowInsecureHTTP || process.env.PARSE_DASHBOARD_ALLOW_INSECURE_HTTP;

let explicitConfigFileProvided = !!program.config;
let configFile = null;
let configFromCLI = null;
let configServerURL = program.serverURL || process.env.PARSE_DASHBOARD_SERVER_URL;
let configMasterKey = program.masterKey || process.env.PARSE_DASHBOARD_MASTER_KEY;
let configAppId = program.appId || process.env.PARSE_DASHBOARD_APP_ID;
let configAppName = program.appName || process.env.PARSE_DASHBOARD_APP_NAME;
let configUserId = program.userId || process.env.PARSE_DASHBOARD_USER_ID;
let configUserPassword = program.userPassword || process.env.PARSE_DASHBOARD_USER_PASSWORD;
if (!program.config && !process.env.PARSE_DASHBOARD_CONFIG) {
if (configServerURL && configMasterKey && configAppId) {
configFromCLI = {
data: {
apps: [
{
appId: configAppId,
serverURL: configServerURL,
masterKey: configMasterKey,
appName: configAppName,
},
]
}
};
if (configUserId && configUserPassword) {
configFromCLI.data.users = [
{
user: configUserId,
pass: configUserPassword,
}
];
}
} else if (!configServerURL && !configMasterKey && !configAppName) {
configFile = path.join(__dirname, '/../parse-dashboard-config.json');
}
} else if (!program.config && process.env.PARSE_DASHBOARD_CONFIG) {
configFromCLI = {
data: JSON.parse(process.env.PARSE_DASHBOARD_CONFIG)
};
} else {
configFile = program.config;
if (program.appId || program.serverURL || program.masterKey || program.appName) {
console.log('You must provide either a config file or required CLI options (app ID, Master Key, and server URL); not both.');
process.exit(3);
}
}



let p = null;
if (configFile) {
p = jsonFile(configFile);
} else if (configFromCLI) {
p = Promise.resolve(configFromCLI);
} else {
//Failed to load default config file.
console.log('You must provide either a config file or an app ID, Master Key, and server URL. See parse-dashboard --help for details.');
process.exit(4);
}

p.then(config => {
config.data.port = port;
ParseDashboard.runServer(config.data);
}, error => {
if (error instanceof SyntaxError) {
console.log('Your config file contains invalid JSON. Exiting.');
process.exit(1);
} else if (error.code === 'ENOENT') {
if (explicitConfigFileProvided) {
console.log('Your config file is missing. Exiting.');
process.exit(2);
} else {
console.log('You must provide either a config file or required CLI options (app ID, Master Key, and server URL); not both.');
process.exit(3);
}
} else {
console.log('There was a problem with your config. Exiting.');
process.exit(-1);
}
})
.catch(error => {
console.log('There was a problem loading the dashboard. Exiting.');
process.exit(-1);
});
211 changes: 86 additions & 125 deletions Parse-Dashboard/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,114 +5,48 @@
* This source code is licensed under the license found in the LICENSE file in
* the root directory of this source tree.
*/
// Command line tool for npm start
"use strict"
"use strict";
const packageJson = require('package-json');
const basicAuth = require('basic-auth');
const path = require('path');
const jsonFile = require('json-file-plus');
const express = require('express');

const program = require('commander');
program.option('--appId [appId]', 'the app Id of the app you would like to manage.');
program.option('--masterKey [masterKey]', 'the master key of the app you would like to manage.');
program.option('--serverURL [serverURL]', 'the server url of the app you would like to manage.');
program.option('--appName [appName]', 'the name of the app you would like to manage. Optional.');
program.option('--config [config]', 'the path to the configuration file');
program.option('--port [port]', 'the port to run parse-dashboard');
program.option('--allowInsecureHTTP [allowInsecureHTTP]', 'set this flag when you are running the dashboard behind an HTTPS load balancer or proxy with early SSL termination.');
class ParseDashboard {
Copy link
Contributor

Choose a reason for hiding this comment

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

this won't work as we don't pass it through babel AFAIK

Copy link
Author

Choose a reason for hiding this comment

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

The code doesn't pass through babel, but Node.js supports some features of ECMAScript 6 (including classes, albeit only in strict mode) without the need for babel compilation. (See: https://nodejs.org/en/docs/es6/)


program.parse(process.argv);

const currentVersionFeatures = require('../package.json').parseDashboardFeatures;

var newFeaturesInLatestVersion = [];
packageJson('parse-dashboard', 'latest').then(latestPackage => {
if (latestPackage.parseDashboardFeatures instanceof Array) {
newFeaturesInLatestVersion = latestPackage.parseDashboardFeatures.filter(feature => {
return currentVersionFeatures.indexOf(feature) === -1;
});
}
});

const port = program.port || process.env.PORT || 4040;
const allowInsecureHTTP = program.allowInsecureHTTP || process.env.PARSE_DASHBOARD_ALLOW_INSECURE_HTTP;

let explicitConfigFileProvided = !!program.config;
let configFile = null;
let configFromCLI = null;
let configServerURL = program.serverURL || process.env.PARSE_DASHBOARD_SERVER_URL;
let configMasterKey = program.masterKey || process.env.PARSE_DASHBOARD_MASTER_KEY;
let configAppId = program.appId || process.env.PARSE_DASHBOARD_APP_ID;
let configAppName = program.appName || process.env.PARSE_DASHBOARD_APP_NAME;
let configUserId = program.userId || process.env.PARSE_DASHBOARD_USER_ID;
let configUserPassword = program.userPassword || process.env.PARSE_DASHBOARD_USER_PASSWORD;
if (!program.config && !process.env.PARSE_DASHBOARD_CONFIG) {
if (configServerURL && configMasterKey && configAppId) {
configFromCLI = {
data: {
apps: [
{
appId: configAppId,
serverURL: configServerURL,
masterKey: configMasterKey,
appName: configAppName,
},
]
}
};
if (configUserId && configUserPassword) {
configFromCLI.data.users = [
{
user: configUserId,
pass: configUserPassword,
}
];
constructor(dashboardConfig) {
if(typeof dashboardConfig === 'undefined')
{
dashboardConfig = {};
}
} else if (!configServerURL && !configMasterKey && !configAppName) {
configFile = path.join(__dirname, 'parse-dashboard-config.json');
}
} else if (!program.config && process.env.PARSE_DASHBOARD_CONFIG) {
configFromCLI = {
data: JSON.parse(process.env.PARSE_DASHBOARD_CONFIG)
};
} else {
configFile = program.config;
if (program.appId || program.serverURL || program.masterKey || program.appName) {
console.log('You must provide either a config file or required CLI options (app ID, Master Key, and server URL); not both.');
process.exit(3);
}
}

let p = null;
if (configFile) {
p = jsonFile(configFile);
} else if (configFromCLI) {
p = Promise.resolve(configFromCLI);
} else {
//Failed to load default config file.
console.log('You must provide either a config file or an app ID, Master Key, and server URL. See parse-dashboard --help for details.');
process.exit(4);
}
p.then(config => {
config.data.apps.forEach(app => {
if (!app.appName) {
app.appName = app.appId;
if(!dashboardConfig.apps)
{
dashboardConfig.apps = [];
}
});

const app = express();
ParseDashboard.checkConfig(dashboardConfig);
this.config = {
apps : dashboardConfig.apps,
users: dashboardConfig.users,
};
}

// Serve public files.
app.use(express.static(path.join(__dirname,'public')));

// Serve the configuration.
app.get('/parse-dashboard-config.json', function(req, res) {
//Server the provided config retrieved from request to the provided response object
static serveConfig(config,req,res){
var currentVersionFeatures = require('../package.json').parseDashboardFeatures;
var newFeaturesInLatestVersion = [];
packageJson('parse-dashboard', 'latest').then(latestPackage => {
if (latestPackage.parseDashboardFeatures instanceof Array) {
newFeaturesInLatestVersion = latestPackage.parseDashboardFeatures.filter(feature => {
return currentVersionFeatures.indexOf(feature) === -1;
});
}
});
const response = {
apps: config.data.apps,
apps: config.apps,
newFeaturesInLatestVersion: newFeaturesInLatestVersion,
};
const users = config.data.users;
const users = config.users;

let auth = null;
//If they provide auth when their config has no users, ignore the auth
Expand Down Expand Up @@ -165,35 +99,62 @@ p.then(config => {
}
//We shouldn't get here. Fail closed.
res.send({ success: false, error: 'Something went wrong.' });
});

// For every other request, go to index.html. Let client-side handle the rest.
app.get('/*', function(req, res) {
res.sendFile(__dirname + '/index.html');
});

// Start the server.
app.listen(port);

console.log(`The dashboard is now available at http://localhost:${port}/`);
}, error => {
if (error instanceof SyntaxError) {
console.log('Your config file contains invalid JSON. Exiting.');
process.exit(1);
} else if (error.code === 'ENOENT') {
if (explicitConfigFileProvided) {
console.log('Your config file is missing. Exiting.');
process.exit(2);
} else {
console.log('You must provide either a config file or required CLI options (app ID, Master Key, and server URL); not both.');
process.exit(3);
}
} else {
console.log('There was a problem with your config. Exiting.');
process.exit(-1);
}
})
.catch(error => {
console.log('There was a problem loading the dashboard. Exiting.');
process.exit(-1);
});

serveConfigWrapper(req,res)
{
ParseDashboard.serveConfig(this.config,req,res);
}

app() {
var api = express();
// Serve public files.
api.use("/",express.static(path.join(__dirname,'public')));

// Serve the configuration.
api.get('/parse-dashboard-config.json',this.serveConfigWrapper.bind(this));

// For every other request, go to index.html. Let client-side handle the rest.
api.get('/*', function(req, res) {
res.sendFile(__dirname + '/index.html');
});
return api;
}

static runServer(config)
{
ParseDashboard.checkConfig(config);
var api = express();

// Serve public files.
api.use("/",express.static(path.join(__dirname,'public')));

// Serve the configuration.
api.get('/parse-dashboard-config.json',function(req,res)
{
ParseDashboard.serveConfig(config,req,res);
});

// For every other request, go to index.html. Let client-side handle the rest.
api.get('/*', function(req, res) {
res.sendFile(__dirname + '/index.html');
});

// Start the server.
api.listen(config.port);


console.log('The dashboard is now available at http://localhost:'+config.port);
}

static checkConfig(config)
{
config.apps.forEach(app => {
if (!app.appName) {
app.appName = app.appId;
}
});
}
}

module.exports = ParseDashboard;
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,35 @@ You can also manage apps that on Parse.com from the same dashboard. In your conf

You can set `appNameForURL` in the config file for each app to control the url of your app within the dashboard. This can make it easier to use bookmarks or share links on your dashboard.

## Use as Express middleware

Parse Dashboard can also be mounted as middleware in an express application.

A short example on how to accomplish this is shown below:

```
var express = require('express');
var app = express();
var ParseDashboard = require('parse-dashboard');

var dashboard = new ParseDashboard({
apps: [
{
serverURL: "https://example.com:17030/parse",
appId: "YOUR_APP_ID",
masterKey: "YOUR_MASTER_KEY",
appName: "MyApp"
}
]
});

app.use("/",dashboard.app());

app.listen(4040, function () {
console.log('Example Parse Dashboard express app listening on port 4040!');
});
```

## Deploying the dashboard

Make sure the server URLs for your apps can be accessed by your browser. If you are deploying the dashboard, then `localhost` urls will not work.
Expand Down
2 changes: 1 addition & 1 deletion bin/parse-dashboard
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/usr/bin/env node
require('../Parse-Dashboard');
require('../Parse-Dashboard/cli');
Loading