Skip to content

Instantly share code, notes, and snippets.

@Aschen
Last active March 8, 2020 10:06
Show Gist options
  • Select an option

  • Save Aschen/53ea1b220b3772ef03335c343021f083 to your computer and use it in GitHub Desktop.

Select an option

Save Aschen/53ea1b220b3772ef03335c343021f083 to your computer and use it in GitHub Desktop.
Kuzzle in framework mode
const
S3 = require('kuzzle-plugin-s3'),
Cloudinary = require('kuzzle-plugin-cloudinary'),
AssetController = require('./assetController'),
securities = require('./securities.json'),
{ Backend } = require('kuzzle');
const mappings = {
'nyc-open-data': {
'yellow-taxi': {
name: { type: 'keyword' }
}
}
};
/* Instantiate new Kuzzle app */
const backend = new Backend('omniscient');
/* Use complete plugins from NPM */
backend.use(S3);
backend.use(Cloudinary);
/* Use custom controller */
backend.use(AssetController);
/* Register some pipes and hooks on events*/
backend.registerHook('document:create:before', req => console.log(req));
backend.registerPipe('s3/upload:get:before', async req => {
if (! req.context.user.isAnonymous) {
return req;
}
throw Error('Unauthorized');
});
/* Apply default mappings and securities */
backend.applyMappings(mappings)
backend.applySecurities(securities)
/* Overwrite config from kuzzlerc */
backend.addConfig({ port: 4242 });
backend.config.server.limits.documentWriteCount = 42000;
/* Start Kuzzle server */
backend.start();
const { BaseController } = require('kuzzle');
class AssetController extends BaseController {
constructor () {
super('asset');
this.actions = {
'list': {
url: '/assets'
},
'add'{
url: '/assets'
},
'remove':{
url: '/assets/:id'
}
};
}
async init (context, config) {
}
async list (request) {
const customer = this.stringParam(request, 'customer');
const results = await this.sdk.document.search(customer, 'assets', {});
return results.hits;
}
async add (request) {
// ...
}
async remove (request) {
// ...
}
}
module.exports = AssetController;
const
ImageController = require('./imageController'),
S3Plugin = require('kuzzle-plugin-s3'),
{ BasePlugin } = require('kuzzle');
class CloudinaryPlugin extends BasePlugin {
constructor () {
super('cloudinary');
/* Use complete plugins from NPM */
this.use(S3Plugin);
/* Use custom controller */
this.use(ImageController);
/* Register some hooks*/
this.registerHook('document:create:before', req => console.log(req));
}
async init () {
// ...
}
}
module.exports = CloudinaryPlugin;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment