Merge pull request #980 from ParsePlatform/flovilmart.ParseServerClass
ParseServer is a class
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
var request = require('request');
|
var request = require('request');
|
||||||
var parseServerPackage = require('../package.json');
|
var parseServerPackage = require('../package.json');
|
||||||
var MockEmailAdapterWithOptions = require('./MockEmailAdapterWithOptions');
|
var MockEmailAdapterWithOptions = require('./MockEmailAdapterWithOptions');
|
||||||
|
var ParseServer = require("../src/index");
|
||||||
|
var express = require('express');
|
||||||
|
|
||||||
describe('server', () => {
|
describe('server', () => {
|
||||||
it('requires a master key and app id', done => {
|
it('requires a master key and app id', done => {
|
||||||
@@ -168,4 +170,62 @@ describe('server', () => {
|
|||||||
done();
|
done();
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('can create a parse-server', done => {
|
||||||
|
var parseServer = new ParseServer.default({
|
||||||
|
appId: "aTestApp",
|
||||||
|
masterKey: "aTestMasterKey",
|
||||||
|
serverURL: "http://localhost:12666/parse",
|
||||||
|
databaseURI: 'mongodb://localhost:27017/aTestApp'
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(Parse.applicationId).toEqual("aTestApp");
|
||||||
|
var app = express();
|
||||||
|
app.use('/parse', parseServer.app);
|
||||||
|
|
||||||
|
var server = app.listen(12666);
|
||||||
|
var obj = new Parse.Object("AnObject");
|
||||||
|
var objId;
|
||||||
|
obj.save().then((obj) => {
|
||||||
|
objId = obj.id;
|
||||||
|
var q = new Parse.Query("AnObject");
|
||||||
|
return q.first();
|
||||||
|
}).then((obj) => {
|
||||||
|
expect(obj.id).toEqual(objId);
|
||||||
|
server.close();
|
||||||
|
done();
|
||||||
|
}).fail((err) => {
|
||||||
|
server.close();
|
||||||
|
done();
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
it('can create a parse-server', done => {
|
||||||
|
var parseServer = ParseServer.ParseServer({
|
||||||
|
appId: "anOtherTestApp",
|
||||||
|
masterKey: "anOtherTestMasterKey",
|
||||||
|
serverURL: "http://localhost:12667/parse",
|
||||||
|
databaseURI: 'mongodb://localhost:27017/anotherTstApp'
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(Parse.applicationId).toEqual("anOtherTestApp");
|
||||||
|
var app = express();
|
||||||
|
app.use('/parse', parseServer);
|
||||||
|
|
||||||
|
var server = app.listen(12667);
|
||||||
|
var obj = new Parse.Object("AnObject");
|
||||||
|
var objId;
|
||||||
|
obj.save().then((obj) => {
|
||||||
|
objId = obj.id;
|
||||||
|
var q = new Parse.Query("AnObject");
|
||||||
|
return q.first();
|
||||||
|
}).then((obj) => {
|
||||||
|
expect(obj.id).toEqual(objId);
|
||||||
|
server.close();
|
||||||
|
done();
|
||||||
|
}).fail((err) => {
|
||||||
|
server.close();
|
||||||
|
done();
|
||||||
|
})
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
50
src/index.js
50
src/index.js
@@ -79,7 +79,9 @@ addParseCloud();
|
|||||||
// "javascriptKey": optional key from Parse dashboard
|
// "javascriptKey": optional key from Parse dashboard
|
||||||
// "push": optional key from configure push
|
// "push": optional key from configure push
|
||||||
|
|
||||||
function ParseServer({
|
export default class ParseServer {
|
||||||
|
|
||||||
|
constructor({
|
||||||
appId = requiredParameter('You must provide an appId!'),
|
appId = requiredParameter('You must provide an appId!'),
|
||||||
masterKey = requiredParameter('You must provide a masterKey!'),
|
masterKey = requiredParameter('You must provide a masterKey!'),
|
||||||
appName,
|
appName,
|
||||||
@@ -122,14 +124,14 @@ function ParseServer({
|
|||||||
DatabaseAdapter.setAdapter(databaseAdapter);
|
DatabaseAdapter.setAdapter(databaseAdapter);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (databaseURI) {
|
|
||||||
DatabaseAdapter.setAppDatabaseURI(appId, databaseURI);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (databaseOptions) {
|
if (databaseOptions) {
|
||||||
DatabaseAdapter.setAppDatabaseOptions(appId, databaseOptions);
|
DatabaseAdapter.setAppDatabaseOptions(appId, databaseOptions);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (databaseURI) {
|
||||||
|
DatabaseAdapter.setAppDatabaseURI(appId, databaseURI);
|
||||||
|
}
|
||||||
|
|
||||||
if (cloud) {
|
if (cloud) {
|
||||||
addParseCloud();
|
addParseCloud();
|
||||||
if (typeof cloud === 'function') {
|
if (typeof cloud === 'function') {
|
||||||
@@ -141,6 +143,7 @@ function ParseServer({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
const filesControllerAdapter = loadAdapter(filesAdapter, () => {
|
const filesControllerAdapter = loadAdapter(filesAdapter, () => {
|
||||||
return new GridStoreAdapter(databaseURI);
|
return new GridStoreAdapter(databaseURI);
|
||||||
});
|
});
|
||||||
@@ -157,7 +160,6 @@ function ParseServer({
|
|||||||
const userController = new UserController(emailControllerAdapter, appId, { verifyUserEmails });
|
const userController = new UserController(emailControllerAdapter, appId, { verifyUserEmails });
|
||||||
const liveQueryController = new LiveQueryController(liveQuery);
|
const liveQueryController = new LiveQueryController(liveQuery);
|
||||||
|
|
||||||
|
|
||||||
cache.apps.set(appId, {
|
cache.apps.set(appId, {
|
||||||
masterKey: masterKey,
|
masterKey: masterKey,
|
||||||
serverURL: serverURL,
|
serverURL: serverURL,
|
||||||
@@ -179,6 +181,7 @@ function ParseServer({
|
|||||||
appName: appName,
|
appName: appName,
|
||||||
publicServerURL: publicServerURL,
|
publicServerURL: publicServerURL,
|
||||||
customPages: customPages,
|
customPages: customPages,
|
||||||
|
maxUploadSize: maxUploadSize,
|
||||||
liveQueryController: liveQueryController
|
liveQueryController: liveQueryController
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -188,7 +191,15 @@ function ParseServer({
|
|||||||
}
|
}
|
||||||
|
|
||||||
Config.validate(cache.apps.get(appId));
|
Config.validate(cache.apps.get(appId));
|
||||||
|
this.config = cache.apps.get(appId);
|
||||||
|
hooksController.load();
|
||||||
|
}
|
||||||
|
|
||||||
|
get app() {
|
||||||
|
return ParseServer.app(this.config);
|
||||||
|
}
|
||||||
|
|
||||||
|
static app({maxUploadSize = '20mb'}) {
|
||||||
// This app serves the Parse API directly.
|
// This app serves the Parse API directly.
|
||||||
// It's the equivalent of https://api.parse.com/1 in the hosted Parse API.
|
// It's the equivalent of https://api.parse.com/1 in the hosted Parse API.
|
||||||
var api = express();
|
var api = express();
|
||||||
@@ -251,30 +262,33 @@ function ParseServer({
|
|||||||
if ( err.code === "EADDRINUSE" ) { // user-friendly message for this common error
|
if ( err.code === "EADDRINUSE" ) { // user-friendly message for this common error
|
||||||
console.log(`Unable to listen on port ${err.port}. The port is already in use.`);
|
console.log(`Unable to listen on port ${err.port}. The port is already in use.`);
|
||||||
process.exit(0);
|
process.exit(0);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
hooksController.load();
|
|
||||||
|
|
||||||
return api;
|
return api;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static ParseServer(options) {
|
||||||
|
let server = new ParseServer(options);
|
||||||
|
return server.app;
|
||||||
|
}
|
||||||
|
|
||||||
|
static createLiveQueryServer(httpServer, config) {
|
||||||
|
return new ParseLiveQueryServer(httpServer, config);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function addParseCloud() {
|
function addParseCloud() {
|
||||||
const ParseCloud = require("./cloud-code/Parse.Cloud");
|
const ParseCloud = require("./cloud-code/Parse.Cloud");
|
||||||
Object.assign(Parse.Cloud, ParseCloud);
|
Object.assign(Parse.Cloud, ParseCloud);
|
||||||
global.Parse = Parse;
|
global.Parse = Parse;
|
||||||
}
|
}
|
||||||
|
|
||||||
ParseServer.createLiveQueryServer = function(httpServer, config) {
|
let runServer = function(options) {
|
||||||
return new ParseLiveQueryServer(httpServer, config);
|
return ParseServer.ParseServer(options);
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
export { S3Adapter, GCSAdapter, FileSystemAdapter };
|
||||||
ParseServer: ParseServer,
|
export { runServer as ParseServer };
|
||||||
S3Adapter: S3Adapter,
|
|
||||||
GCSAdapter: GCSAdapter,
|
|
||||||
FileSystemAdapter: FileSystemAdapter
|
|
||||||
};
|
|
||||||
|
|||||||
Reference in New Issue
Block a user