Skip to content

Commit

Permalink
A basic data CRUD access from ES and read config in kibana (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
mw-ding authored Jun 14, 2018
1 parent 88829e1 commit 43475a4
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 3 deletions.
10 changes: 9 additions & 1 deletion kibana-extra/castro/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import exampleRoute from './server/routes/example';
import personRoute from './server/routes/person';

import mappings from './mappings';

export default function (kibana) {
return new kibana.Plugin({
Expand All @@ -14,19 +17,24 @@ export default function (kibana) {

hacks: [
'plugins/castro/hack'
]
],

mappings
},

config(Joi) {
return Joi.object({
enabled: Joi.boolean().default(true),
index: Joi.string().default('.castro'),
fromyaml: Joi.string().default('empty')
}).default();
},


init(server, options) {
// Add server routes and initialize the plugin here
exampleRoute(server);
personRoute(server);
}


Expand Down
12 changes: 12 additions & 0 deletions kibana-extra/castro/mappings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"person": {
"properties": {
"firstName": {
"type": "text"
},
"lastName": {
"type": "text"
}
}
}
}
5 changes: 5 additions & 0 deletions kibana-extra/castro/proto/Example.proto
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,8 @@ message Entry {
string blob = 2;
bool isBinary = 3;
}

message Person {
string firstName = 1;
string lastName = 2;
}
3 changes: 1 addition & 2 deletions kibana-extra/castro/server/routes/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,5 @@ export default function (server: Hapi.Server) {
handler(req: Hapi.Request, reply: any) {
getHeadCommit().then((result: Commit) => reply(result))
}
})

});
}
106 changes: 106 additions & 0 deletions kibana-extra/castro/server/routes/person.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import * as Hapi from "hapi";
import { Person } from '../../common/proto';

export default function (server: Hapi.Server) {
// Create
server.route({
path: '/api/castro/person/{id}',
method: 'POST',
handler: async function (req: Hapi.Request, reply: any) {
const id = req.params.id;
const person = Person.fromObject(req.payload);

const es = req.server.plugins.elasticsearch.getCluster("data");
// const client = req.getSavedObjectsClient();
const {callWithRequest} = es;

const config = req.server.config();
const index = config.get('castro.index');

callWithRequest(req, 'create', {
index,
type: 'person',
id,
body: person.toJSON()
}).then(function (response: any) {
reply(response);
});
}
});

// Read
server.route({
path: '/api/castro/person/{id}',
method: 'GET',
handler: async function (req: Hapi.Request, reply: any) {

const config = req.server.config();
const index = config.get('castro.index');
console.log("## castro config: ");
console.log(config.get('castro.index'));
console.log(config.get('castro.fromyaml'));

const id = req.params.id;
const es = req.server.plugins.elasticsearch.getCluster("data");
// const client = req.getSavedObjectsClient();
const {callWithRequest} = es;
callWithRequest(req, 'get', {
index,
type: 'person',
id
}).then(function (response) {
reply(response)
});
}
});

// Update
server.route({
path: '/api/castro/person/{id}',
method: 'PUT',
handler: async function (req: Hapi.Request, reply: any) {
const id = req.params.id;
const body = req.payload;

const es = req.server.plugins.elasticsearch.getCluster("data");
// const client = req.getSavedObjectsClient();
const {callWithRequest} = es;

const config = req.server.config();
const index = config.get('castro.index');

callWithRequest(req, 'update', {
index,
type: 'person',
id,
body
}).then(function (response) {
reply(response)
});

}
});

// Delete
server.route({
path: '/api/castro/person/{id}',
method: 'DELETE',
handler: async function (req: Hapi.Request, reply: any) {
const id = req.params.id;
const es = req.server.plugins.elasticsearch.getCluster("data");
// const client = req.getSavedObjectsClient();
const {callWithRequest} = es;

const config = req.server.config();
const index = config.get('castro.index');

callWithRequest(req, 'delete', {
index,
type: 'person',
id
}).then(function (response) {
reply(response)
});
}
});
}

0 comments on commit 43475a4

Please sign in to comment.