Skip to content

Commit 8934bf5

Browse files
authored
Merge pull request #24 from datapartyjs/venue-hosting
Venue hosting
2 parents 841a879 + bddc49b commit 8934bf5

14 files changed

Lines changed: 306 additions & 32 deletions

src/party/document-factory.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,36 @@ const Ajv = require('ajv')
22
const debug = require('debug')('dataparty.document-factory')
33
const IDocument = require('./idocument')
44

5+
class DocumentValidationError extends Error {
6+
constructor(ajvErrors){
7+
super()
8+
9+
/*[
10+
{
11+
"keyword": "required",
12+
"dataPath": "",
13+
"schemaPath": "#/required",
14+
"params": {
15+
"missingProperty": "name"
16+
},
17+
"message": "should have required property 'name'"
18+
}
19+
]
20+
*/
21+
22+
this.message='Validation failure\n'
23+
24+
for(let i=0; i<ajvErrors.length; i++){
25+
const error = ajvErrors[i]
26+
this.message += error.message + 'at data.'+error.dataPath
27+
}
28+
29+
this.stack=''
30+
this.name='DocumentValidationError'
31+
this.code=this.name
32+
}
33+
}
34+
535
/**
636
* @class
737
*/
@@ -111,6 +141,7 @@ class DocumentFactory {
111141
* @param {*} data
112142
*/
113143
validate(type, data){
144+
debug('validate',type)
114145
return new Promise((resolve, reject)=>{
115146

116147
if(!this.validators[type]){
@@ -122,7 +153,8 @@ class DocumentFactory {
122153

123154
if(!valid){
124155
let errors = this.validators[type].errors
125-
return reject({error: errors})
156+
157+
return reject(new DocumentValidationError(errors))
126158
}
127159

128160
return resolve(data)

src/party/idocument.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ class IDocument extends EventEmitter {
114114
* @returns {object}
115115
*/
116116
async mergeData(input){
117-
return this.setData(Object.assign({}, this.data, input))
117+
return await this.setData(Object.assign({}, this.data, input))
118118
}
119119

120120
/**
@@ -124,7 +124,9 @@ class IDocument extends EventEmitter {
124124
* @returns {object}
125125
*/
126126
async setData(input){
127+
debug('setData start')
127128
let valid = await this.party.factory.validate(this.type, input)
129+
debug('setData done')
128130
this._data = valid
129131
}
130132

@@ -138,9 +140,13 @@ class IDocument extends EventEmitter {
138140
delete value.$meta.version
139141
delete value.__v
140142

143+
debug('asign data')
141144
await this.setData(value)
145+
debug('data set')
142146
await this.party.update(value)
147+
debug('doc updated')
143148
await this.pull()
149+
debug('doc pulled')
144150
}
145151

146152

@@ -298,7 +304,7 @@ class IDocument extends EventEmitter {
298304
const newMsg = this.party.cache.findById(this.type, this.id)
299305
const oldMsg = Object.assign({}, this.getData())
300306

301-
debug('new message', newMsg)
307+
debug('new message', event.event, newMsg)
302308

303309
switch (event.event){
304310
case 'remove':

src/party/local/loki-db.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ module.exports = class LokiDb extends EventEmitter {
3939
autoload: true,
4040
autoloadCallback : resolve,
4141
autosave: true,
42-
autosaveInterval: 10000
42+
autosaveInterval: 1000
4343
}
4444
)
4545

src/service/iservice.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const gitRepoInfo = require('git-repo-info')
77
const BouncerDb = require('@dataparty/bouncer-db')
88
const mongoose = BouncerDb.mongoose()
99
const json2ts = require('json-schema-to-typescript')
10+
const { build } = require('@hapi/joi')
1011
const debug = require('debug')('dataparty.service.IService')
1112

1213
module.exports = class IService {
@@ -59,6 +60,9 @@ module.exports = class IService {
5960
}
6061
}
6162

63+
importBuild(buildOutput){
64+
this.compiled = buildOutput
65+
}
6266

6367
/**
6468
*
@@ -83,7 +87,7 @@ module.exports = class IService {
8387
}
8488

8589
addEndpoint(endpoint_path){
86-
debug('andEndpoint', endpoint_path)
90+
debug('addEndpoint', endpoint_path)
8791
const endpoint = require(endpoint_path)
8892
const name = endpoint.Name
8993

@@ -141,7 +145,7 @@ module.exports = class IService {
141145
fs.writeFileSync(schemaOutput, JSON.stringify({
142146
package: this.compiled.package,
143147
...this.compiled.schemas
144-
}))
148+
}, null, 2))
145149
}
146150

147151
return this.compiled

src/service/service-runner.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,14 @@ class ServiceRunner {
2828
debug('starting endpoints')
2929

3030
const eps = Hoek.reach(this.service, 'compiled.endpoints')
31-
const endpointsLoading = []
31+
//const endpointsLoading = []
3232
for(let name in eps){
3333
debug('\t',name)
34-
endpointsLoading.push( this.loadEndpoint(name) )
34+
await this.loadEndpoint(name)
35+
//endpointsLoading.push( this.loadEndpoint(name) )
3536
}
3637

37-
await Promise.all(endpointsLoading)
38+
//await Promise.all(endpointsLoading)
3839
debug('endpoints ready:')
3940
for(let name in this.endpoint){
4041
debug('\t', Path.join('/', name))
@@ -86,6 +87,7 @@ class ServiceRunner {
8687

8788
async loadMiddleware(name, type='pre'){
8889
if(this.middleware[type][name]){
90+
debug('cached',type,'middleware',name)
8991
return this.middleware[type][name]
9092
}
9193

@@ -107,7 +109,7 @@ class ServiceRunner {
107109
this.middleware[type][name] = runner
108110

109111
dt.end()
110-
debug('loaded middleware',name,'in',dt.deltaMs,'ms')
112+
debug('loaded',type,'middleware',name,'in',dt.deltaMs,'ms')
111113

112114
return runner
113115
}
@@ -150,7 +152,7 @@ class ServiceRunner {
150152
endpointHandler(endpoint){
151153
return async (event)=>{
152154

153-
debug('event',event.method, event.pathname)
155+
debug('event',event.method, event.pathname, event.request.ip, event.request.ips)
154156

155157

156158
const context = new EndpointContext({
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
const Joi = require('@hapi/joi')
2+
const Hoek = require('@hapi/hoek')
3+
const {Message, Routines} = require('@dataparty/crypto')
4+
const debug = require('debug')('dataparty.endpoint.create-service')
5+
6+
const IEndpoint = require('../../service/iendpoint')
7+
const { exist } = require('@hapi/joi')
8+
9+
module.exports = class CreateSrvEndpoint extends IEndpoint {
10+
11+
static get Name(){
12+
return 'create-service'
13+
}
14+
15+
16+
static get Description(){
17+
return 'Create venue service'
18+
}
19+
20+
static get MiddlewareConfig(){
21+
return {
22+
pre: {
23+
decrypt: false,
24+
validate: Joi.object().keys(null)
25+
},
26+
post: {
27+
encrypt: false,
28+
validate: Joi.object().keys(null).description('any output allowed')
29+
}
30+
}
31+
}
32+
33+
static async run(ctx){
34+
35+
ctx.debug('hello')
36+
debug('echo')
37+
ctx.debug('ctx.input', ctx.input)
38+
39+
const compiledSrv = JSON.parse(ctx.input.service)
40+
const serviceId = compiledSrv.package.name + '-' + compiledSrv.package.version
41+
debug('addService', serviceId)
42+
43+
let srvDoc = (await ctx.party.find()
44+
.type('venue_srv')
45+
.where('name').equals(compiledSrv.package.name)
46+
.exec())[0]
47+
48+
49+
50+
if(!srvDoc){
51+
debug('creating service')
52+
srvDoc = await ctx.party.createDocument('venue_srv', {
53+
name: compiledSrv.package.name,
54+
'created': (new Date()).toISOString(),
55+
package: compiledSrv.package,
56+
schemas: compiledSrv.schemas,
57+
endpoints: compiledSrv.endpoints,
58+
midddleware: compiledSrv.middleware,
59+
middleware_order: compiledSrv.middleware_order
60+
})
61+
62+
debug('service created')
63+
}
64+
else{
65+
66+
67+
try{
68+
69+
debug('updating service')
70+
debug(srvDoc.data)
71+
await srvDoc.mergeData({
72+
package: compiledSrv.package,
73+
schemas: compiledSrv.schemas,
74+
endpoints: compiledSrv.endpoints,
75+
midddleware: compiledSrv.middleware,
76+
middleware_order: compiledSrv.middleware_order
77+
})
78+
79+
//debug(srvDoc.data)
80+
81+
debug('saving doc')
82+
83+
84+
await srvDoc.save()
85+
}
86+
catch(err){
87+
console.log(err)
88+
}
89+
debug('updated service')
90+
}
91+
92+
return {srv:srvDoc.data}
93+
}
94+
}

src/venue/schema/venue_service.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
'use strict'
2+
3+
const Hoek = require('hoek')
4+
//const BouncerDb = require('@dataparty/bouncer-db')
5+
/*
6+
require('mongoose-schema-jsonschema')(BouncerDb.mongoose())
7+
BouncerDb.mongoose().plugin(require("mongoose-ajv-plugin"))
8+
*/
9+
const debug = require('debug')('venue.venue_srv')
10+
11+
const Dataparty = require('../../service')
12+
13+
const Utils = Dataparty.ISchema.Utils
14+
15+
16+
class VenueSrv extends Dataparty.ISchema {
17+
18+
static get Type () { return 'venue_srv' }
19+
20+
static get Schema(){
21+
return {
22+
//project: Utils.actor(['venue_project']),
23+
name: {type: String, required: true},
24+
created: Utils.created,
25+
//owner: Utils.actor(['user']),
26+
package: {
27+
name: String,
28+
version: String,
29+
githash: String,
30+
branch: String
31+
},
32+
schemas: {
33+
Package: {},
34+
IndexSettings: {},
35+
JSONSchema: {},
36+
Permissions: {}
37+
},
38+
endpoints: {},
39+
middleware: {
40+
pre: {},
41+
post: {}
42+
},
43+
middleware_order: {
44+
pre: [String],
45+
post: [String]
46+
}
47+
}
48+
}
49+
50+
static setupSchema(schema){
51+
schema.index({ name: 1 }, {unique: true})
52+
return schema
53+
}
54+
55+
static permissions (context) {
56+
return {
57+
read: true,
58+
new: true,
59+
change: true
60+
}
61+
}
62+
}
63+
64+
65+
module.exports = VenueSrv

0 commit comments

Comments
 (0)