Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard Chin committed Oct 2, 2017
0 parents commit c8d4e84
Show file tree
Hide file tree
Showing 11 changed files with 639 additions and 0 deletions.
75 changes: 75 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Created by .ignore support plugin (hsz.mobi)
### Node template
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Typescript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env


/config.js
/instance.json
package-lock.json
public/javascripts/
/count.json
/route-proxy.json
/config.json
/configs/
/appServers.json
/dashboards.json
/logs/
/cloudcode/
/multiple-apps-parse-server.iml
100 changes: 100 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# Multiple Apps Parse Server

## Objective
* run and manage multiple parse apps (instances) in **a server** and using **a single port**.
* one code, one database, create a parse app in one second.
* parse dashboard integrated, each app's manager can log into parse dashboard to manage their app.
* one admin account in parse dashboard to manage all apps


## Prerequisites
* Node.JS 4.8.0 or above
* MongoDB (if run in local)
* pm2

## Architecture
* use Node.JS + Express
* [Parse Server](https://github.com/parse-community/parse-server)
* [Parse Server Dashboard](https://github.com/parse-community/parse-dashboard)
* each parse app has different port and use [node-http-proxy](https://github.com/nodejitsu/node-http-proxy) to handle port route.


## Install
* add config.json (can copy from config_sample.json)

```json
{
"portFrom":1337, //the port of the first app will be started from 1337
"maxInstances":100, //maxiumum number of parse apps (<100 is suggested)

"parseDashboardURI":"https://app.xxx.com:4040", //parse server dashboard public uri
"globalURI":"https://app.xxx.com", //parse server public uri
"databaseURI":"mongodb://localhost:27017/", //parse server mongodb uri
"cloudCodeFolder":"./cloudcode/", //parse server cloud code folder, the main.js will be auto created in the sub-folder(named by customer)
"internalProxyPort":3000,
"S3FilesAdapter":{ //parse server s3 file adapter
"bucket":"xxxx",
"accessKey":"xxx",
"secretKey":"xxxx"
},
"parseDashboard":{ //parse server dashboard settings
"port":4040
}
}
```

* install node modules and pm2

```sh
npm install
```

```sh
sudo npm install pm2 -g
```

* create the first parse app
```sh
customer=firstapp node newInstance.js

```
newInstance will auto start several processes by using pm2.
If successfully, you will see
```sh
This is firstly start parse dashboard, admin is created.
---------------------------------------------------------
dashboard URL: https://app.xxx.com:4040
username: admin
password: *******************
---------------------------------------------------------
No.1 parse server instance is created!
The customer information is shown as following, you can copy & paste to your customer
---------------------------------------------------------
api URL: https://app.xxx.com/firstapp
application id: ***rEIIPpggXbKJCy***
master key: ***ZepMVqJMRGuuXd*** //please keep master key in secret
dashboard URL: https://app.xxx.com:4040
dashboard username:firstapp
dashboard password: ********** //please keep this password in secret
---------------------------------------------------------
```
if you want to test locally, try `http://localhost:3000/firstapp` for the first app parse server and
`http://localhost:4040` for parse dashboard.

you can create the second app by `customer=secondapp node newInstance.js`, and so on.

* cloud code monitoring

we use pm2 watch method to monitor the change of cloud code folder, therefore,
once the main.js or other files in the folder are modified, the corresponding parse app will be restart.
Each app developer can deploy their cloud code by using git server(not include here).
* set load balance or dns server to your own domain, then enjoy!

## Clean environment
if you are in development stage, and need to have a clean environment.
previous generated apps are not needed anymore. you can do
```sh
./cleanenv.sh
```

all the process will be deleted and all the parse apps are also delete. (the data in mongodb still be remained)
21 changes: 21 additions & 0 deletions app-dashboard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
var express = require('express');
var config = require('./config.json');
var dashboards={};
try{
dashboards=require('./dashboards.json');
} catch(err){
return false;
}
var fs = require('fs');
// parse server dashbaord
var allowInsecureHTTP = true;
var ParseDashboard = require('parse-dashboard');

var dashboard = new ParseDashboard(dashboards,allowInsecureHTTP);

var appParseDashboard = express();

appParseDashboard.use('/', dashboard);

var httpServer = require('http').createServer(appParseDashboard);
httpServer.listen(config.parseDashboard.port);
41 changes: 41 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
var express = require('express');

// config
var config = {};
if(process.env.customer) {
config = require('./configs/'+process.env.customer);
}
if(!config.enable) {
console.log(config.customer+" is disabled!");
return false;
}
var globalConfig = require('./config.json');

// parse server s3 adapter
var S3Adapter = require('parse-server-s3-adapter');

// parse server
var ParseServer = require('parse-server').ParseServer;
var appParseServer = express();

var api = new ParseServer({
appName:config.customer,
databaseURI: config.parseServer.databaseURI, // Connection string for your MongoDB database
collectionPrefix:config.customer,
cloud: config.parseServer.cloud, // Absolute path to your Cloud Code
appId: config.parseServer.appId,
masterKey: config.parseServer.masterKey, // Keep this key secret!
serverURL: config.serverURL+":"+config.parseServer.port+"/", // Don't forget to change to https if needed
publicServerURL: config.publicServerURL+"/",
logsFolder:'./logs/'+config.customer+"/",
filesAdapter: (process.env.AWS_ACCESS_KEY_ID ? new S3Adapter(globalConfig.S3FilesAdapter.bucket) : null)
});


appParseServer.use('/', api);

appParseServer.listen(config.parseServer.port, function() {
console.log('parse-server running on port '+config.parseServer.port);
});

module.exports = appParseServer;
15 changes: 15 additions & 0 deletions cleanenv.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
read -r -p "This action will remove all pm2 services and instance configs, are you sure? [y/N] " response
case "$response" in
[yY][eE][sS]|[yY])
pm2 delete all
rm ./configs/*
rm -rf ./logs/*
rm -rf ./cloudcode/*
rm ./appServers.json
rm ./count.json
rm ./route-proxy.json
rm ./dashboards.json
;;
*)
;;
esac
18 changes: 18 additions & 0 deletions config_sample.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"portFrom":1337,
"maxInstances":100,

"parseDashboardURI":"https://app.xxx.com:4040",
"globalURI":"https://app.xxx.com",
"databaseURI":"mongodb://localhost:27017/",
"cloudCodeFolder":"./cloudcode/",
"internalProxyPort":3000,
"S3FilesAdapter":{
"bucket":"xxxx",
"accessKey":"xxx",
"secretKey":"xxxx"
},
"parseDashboard":{
"port":4040
}
}
11 changes: 11 additions & 0 deletions initial.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"apps" : [{
"name" : "parse-route",
"script" : "./routeProxy.js",
"watch" : false
},{
"name" : "parse-dashboard",
"script" : "./app-dashboard.js",
"watch" : false
}]
}
8 changes: 8 additions & 0 deletions multiple-apps-parse-server.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
Loading

0 comments on commit c8d4e84

Please sign in to comment.