diff --git a/gulpfile.js b/gulpfile.js index 523378fc8..56290d15d 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -9,6 +9,8 @@ var fs = require('fs'); var webpack = require('webpack'); var WebpackDevServer = require('webpack-dev-server'); +var services = require("./gulpfiles/services.json"); + // Configuration var srcDir = './client'; @@ -32,11 +34,27 @@ gulp.task('client', ['clean'], function() { return gulp.start('watch', 'routes', 'locales', 'webpack-dev-server'); }); -gulp.task('services', function() { +gulp.task('copy', function() { + var streams = []; + for (i=0; i < services.services.length; i++) { + var service = services.services[i]; + for (j=0; j < services.files.length; j++) { + var file = services.files[j]; + streams.push(gulp.src("./services/service/models/" + file) + .pipe(gulp.dest(service + "/common/models/"))); + } + } + return merge(streams); +}); + +gulp.task('services', ['copy'], function() { require('./services/auth/server/server.js').start(); - require('./services/client/server/server.js').start(); require('./services/salix/server/server.js').start(); require('./services/mailer/server.js').start(); + + for (i=0; i < services.services.length; i++) { + require(services.services[i] + "/server/server.js").start(); + } }); gulp.task('clean', function() { diff --git a/gulpfiles/services.json b/gulpfiles/services.json new file mode 100644 index 000000000..7c183dfad --- /dev/null +++ b/gulpfiles/services.json @@ -0,0 +1,14 @@ +{ + "services": [ + "./services/client", + "./services/production" + ], + "files":[ + "account.json", + "my-model.js", + "my-model.json", + "user.json" + ] + +} + diff --git a/services/production/common/models/account.json b/services/production/common/models/account.json new file mode 100644 index 000000000..7b3fd0739 --- /dev/null +++ b/services/production/common/models/account.json @@ -0,0 +1,46 @@ +{ + "name": "Account", + "base": "PersistedModel", + "validateUpsert": true, + "properties": { + "id": { + "type": "number", + "required": true + }, + "name": { + "type": "string", + "required": true + }, + "password": { + "type": "string", + "required": true + }, + "active": { + "type": "boolean" + }, + "email": { + "type": "string", + "required": true + }, + "created": { + "type": "date" + }, + "updated": { + "type": "date" + } + }, + "acls": [ + { + "accessType": "*", + "principalType": "ROLE", + "principalId": "$everyone", + "permission": "DENY" + }, + { + "accessType": "*", + "principalType": "ROLE", + "principalId": "root", + "permission": "ALLOW" + } + ] +} diff --git a/services/production/common/models/my-model.js b/services/production/common/models/my-model.js new file mode 100644 index 000000000..009b025f3 --- /dev/null +++ b/services/production/common/models/my-model.js @@ -0,0 +1,125 @@ + +module.exports = function(self) { + self.setup = function() { + self.super_.setup.call(this); + + let disableMethods = { + 'create': true, + 'replaceOrCreate': true, + 'patchOrCreate': true, + 'upsert': true, + 'updateOrCreate': true, + 'exists': true, + 'find': true, + 'findOne': true, + 'findById': true, + 'deleteById': true, + 'replaceById': true, + 'updateAttributes': false, + 'createChangeStream': true, + 'updateAll': true, + 'upsertWithWhere': true, + 'count': true + }; + + for(let method in disableMethods) { + //this.disableRemoteMethod(method, disableMethods[method]); + } + } + + self.installMethod = function(methodName, filterCb) { + this.remoteMethod(methodName, { + description: 'List items using a filter', + accessType: 'READ', + accepts: [ + { + arg: 'filter', + type: 'object', + required: true, + description: 'Filter defining where', + http: function(ctx) { + return ctx.req.query; + } + } + ], + returns: { + arg: 'data', + type: [this.modelName], + root: true + }, + http: { + verb: 'get', + path: `/${methodName}` + } + }); + + this[methodName] = (params, cb) => { + let filter = removeEmpty(filterCb(params)); + var response = {} + + function returnValues(){ + if(response.instances !== undefined && response.count !== undefined) + cb(null, response); + } + + function error(){ + cb(null, response); + } + + this.find(filter, function(err, instances) { + if(!err){ + response.instances = instances; + returnValues(); + } + else{ + error(); + } + + }) + + this.count(filter.where, function(err, totalCount){ + if(!err){ + response.count = totalCount; + returnValues(); + } + else{ + error(); + } + + }) + }; + + }; +} + +function removeEmpty(o) { + if(Array.isArray(o)) { + let array = []; + for(let item of o) { + let i = removeEmpty(item); + if(!isEmpty(item)) + array.push(item); + }; + if(array.length > 0) + return array; + } + else if (typeof o === 'object') { + let object = {}; + for(let key in o) { + let i = removeEmpty(o[key]); + if(!isEmpty(i)) + object[key] = i; + } + if(Object.keys(object).length > 0) + return object; + } + else if (!isEmpty(o)) + return o; + + return undefined; +} + +function isEmpty(value) { + return value === undefined || value === ""; +} + diff --git a/services/production/common/models/my-model.json b/services/production/common/models/my-model.json new file mode 100644 index 000000000..7a0a975bb --- /dev/null +++ b/services/production/common/models/my-model.json @@ -0,0 +1,4 @@ +{ + "name": "MyModel", + "base": "PersistedModel" +} diff --git a/services/production/common/models/user.json b/services/production/common/models/user.json new file mode 100644 index 000000000..f13d4c168 --- /dev/null +++ b/services/production/common/models/user.json @@ -0,0 +1,11 @@ +{ + "name": "user", + "base": "User", + "properties": { + "id": { + "id": true, + "type": "Number", + "forceId": false + } + } +} \ No newline at end of file diff --git a/services/production/server/config.json b/services/production/server/config.json index e5eff2465..f85f9904c 100644 --- a/services/production/server/config.json +++ b/services/production/server/config.json @@ -1,7 +1,7 @@ { "restApiRoot": "/api", "host": "0.0.0.0", - "port": 3000, + "port": 3004, "remoting": { "context": false, "rest": { diff --git a/services/service/models/account.json b/services/service/models/account.json new file mode 100644 index 000000000..7b3fd0739 --- /dev/null +++ b/services/service/models/account.json @@ -0,0 +1,46 @@ +{ + "name": "Account", + "base": "PersistedModel", + "validateUpsert": true, + "properties": { + "id": { + "type": "number", + "required": true + }, + "name": { + "type": "string", + "required": true + }, + "password": { + "type": "string", + "required": true + }, + "active": { + "type": "boolean" + }, + "email": { + "type": "string", + "required": true + }, + "created": { + "type": "date" + }, + "updated": { + "type": "date" + } + }, + "acls": [ + { + "accessType": "*", + "principalType": "ROLE", + "principalId": "$everyone", + "permission": "DENY" + }, + { + "accessType": "*", + "principalType": "ROLE", + "principalId": "root", + "permission": "ALLOW" + } + ] +} diff --git a/services/service/models/my-model.js b/services/service/models/my-model.js new file mode 100644 index 000000000..009b025f3 --- /dev/null +++ b/services/service/models/my-model.js @@ -0,0 +1,125 @@ + +module.exports = function(self) { + self.setup = function() { + self.super_.setup.call(this); + + let disableMethods = { + 'create': true, + 'replaceOrCreate': true, + 'patchOrCreate': true, + 'upsert': true, + 'updateOrCreate': true, + 'exists': true, + 'find': true, + 'findOne': true, + 'findById': true, + 'deleteById': true, + 'replaceById': true, + 'updateAttributes': false, + 'createChangeStream': true, + 'updateAll': true, + 'upsertWithWhere': true, + 'count': true + }; + + for(let method in disableMethods) { + //this.disableRemoteMethod(method, disableMethods[method]); + } + } + + self.installMethod = function(methodName, filterCb) { + this.remoteMethod(methodName, { + description: 'List items using a filter', + accessType: 'READ', + accepts: [ + { + arg: 'filter', + type: 'object', + required: true, + description: 'Filter defining where', + http: function(ctx) { + return ctx.req.query; + } + } + ], + returns: { + arg: 'data', + type: [this.modelName], + root: true + }, + http: { + verb: 'get', + path: `/${methodName}` + } + }); + + this[methodName] = (params, cb) => { + let filter = removeEmpty(filterCb(params)); + var response = {} + + function returnValues(){ + if(response.instances !== undefined && response.count !== undefined) + cb(null, response); + } + + function error(){ + cb(null, response); + } + + this.find(filter, function(err, instances) { + if(!err){ + response.instances = instances; + returnValues(); + } + else{ + error(); + } + + }) + + this.count(filter.where, function(err, totalCount){ + if(!err){ + response.count = totalCount; + returnValues(); + } + else{ + error(); + } + + }) + }; + + }; +} + +function removeEmpty(o) { + if(Array.isArray(o)) { + let array = []; + for(let item of o) { + let i = removeEmpty(item); + if(!isEmpty(item)) + array.push(item); + }; + if(array.length > 0) + return array; + } + else if (typeof o === 'object') { + let object = {}; + for(let key in o) { + let i = removeEmpty(o[key]); + if(!isEmpty(i)) + object[key] = i; + } + if(Object.keys(object).length > 0) + return object; + } + else if (!isEmpty(o)) + return o; + + return undefined; +} + +function isEmpty(value) { + return value === undefined || value === ""; +} + diff --git a/services/service/models/my-model.json b/services/service/models/my-model.json new file mode 100644 index 000000000..7a0a975bb --- /dev/null +++ b/services/service/models/my-model.json @@ -0,0 +1,4 @@ +{ + "name": "MyModel", + "base": "PersistedModel" +} diff --git a/services/service/models/user.json b/services/service/models/user.json new file mode 100644 index 000000000..f13d4c168 --- /dev/null +++ b/services/service/models/user.json @@ -0,0 +1,11 @@ +{ + "name": "user", + "base": "User", + "properties": { + "id": { + "id": true, + "type": "Number", + "forceId": false + } + } +} \ No newline at end of file