From bc4cb79f116ed948cf957b7df32f14b8db0f9f5f Mon Sep 17 00:00:00 2001 From: ale Date: Sun, 7 Apr 2019 19:38:07 +0200 Subject: [PATCH] api rest with ES --- web/index.js | 107 +++++++++++++++++++++++++++++++++++++++++++---- web/package.json | 1 + web/yarn.lock | 2 +- 3 files changed, 102 insertions(+), 8 deletions(-) diff --git a/web/index.js b/web/index.js index 7d8eee2..b5706d5 100644 --- a/web/index.js +++ b/web/index.js @@ -1,10 +1,103 @@ 'use strict' -const finalhandler = require('finalhandler'), - http = require('http'), - serveStatic = require('serve-static'), - serve = serveStatic(__dirname + '/public', { 'index': ['index.html'] }), - server = http.createServer(function onRequest(req, res) { - serve(req, res, finalhandler(req, res)) - }).listen(8080, () => { +const http = require('http'), + es = require('elasticsearch'), + express = require('express'), + bodyParser = require('body-parser'), + client = new es.Client({ host: 'http://192.168.1.130:9200' }), + app = express(), + router = express.Router(), + server = http.createServer(app).listen(8080, () => { + client.indices.get({ index: '_all' }).then(indices => { + if (indices.forms) { + client.indices.delete({ + index: 'forms' + }) + } + client.indices.create({ + index: 'forms', + body: { + mappings: { + form: { + properties: { + schema: { + type: 'object' + }, + options: { + type: 'object' + }, + data: { + type: 'object' + } + } + } + } + } + }) + }) console.log(`Serving on ${server.address().address}:${server.address().port}`) }) + + +app.use(bodyParser.json()) + .use('/api', router) + .use(express.static(__dirname + '/public')) + +router.param('id', (req, res, next, id) => { + if (id && typeof id === 'string') { + next() + } + else { + res.sendStatus(403) + } +}) + +router.get('/load/:id', (req, res, next) => { + if (req.params.id) { + client.search({ + index: 'forms', + type: 'form', + body: { + query: { + term: { + _id: req.params.id + } + } + } + }).then(form => { + if (form.hits.hits.length > 0) { + res.json(form.hits.hits[0]) + } else res.sendStatus(404) + }) + .catch(error => { + res.json(error) + }) + } else next() +}) + +router.post('/save/:id', (req, res, next) => { + if (req.params.id && req.body.schema && req.body.options && req.body.data) { + client.create({ + index: 'forms', + type: 'form', + id: req.params.id, + body: { + schema: req.body.schema, + options: req.body.options, + data: req.body.data + } + }).then(form => res.json({ result: form.result })) + .catch(next) + } else res.sendStatus(204) +}) + +router.get('/all', (req, res, next) => { + client.search({ + index: 'forms', + type: 'form' + }).then(forms => { + if (forms.hits.hits.length > 0) { + res.json(forms.hits.hits[0]) + } else res.sendStatus(404) + }) + .catch(next) +}) \ No newline at end of file diff --git a/web/package.json b/web/package.json index e1d2e34..2576335 100644 --- a/web/package.json +++ b/web/package.json @@ -11,6 +11,7 @@ "ace-builds": "^1.4.3", "alpaca": "^1.5.24", "blueimp-file-upload": "^9.28.0", + "body-parser": "^1.18.3", "bootstrap": "^3.4.1", "bootstrap-multiselect": "^0.9.13-1", "ckeditor": "^4.11.3", diff --git a/web/yarn.lock b/web/yarn.lock index bf76e98..799052d 100644 --- a/web/yarn.lock +++ b/web/yarn.lock @@ -103,7 +103,7 @@ blueimp-tmpl@3.6.0: resolved "https://registry.yarnpkg.com/blueimp-tmpl/-/blueimp-tmpl-3.6.0.tgz#a4910975d042e2bc03ba77f0e62d04f1548a524c" integrity sha1-pJEJddBC4rwDunfw5i0E8VSKUkw= -body-parser@1.18.3: +body-parser@1.18.3, body-parser@^1.18.3: version "1.18.3" resolved "https://registry.yarnpkg.com/body-parser/-/body-parser-1.18.3.tgz#5b292198ffdd553b3a0f20ded0592b956955c8b4" integrity sha1-WykhmP/dVTs6DyDe0FkrlWlVyLQ=