This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express') | |
const coherence = require('@oracle/coherence') | |
const path = require('path') | |
const uuid = require('uuid') | |
const {sse} = require('@toverux/expresse') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// setup Express | |
const port = process.env.PORT || 5000 | |
const api = express() | |
api.use(express.json()) | |
api.use(express.static(path.join(__dirname, '../java/server/src/main/web/react/build'))) // serve the react application |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// setup session to Coherence | |
const session = new Session() | |
const tasks = session.getMap('tasks') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
api.post('/api/tasks', (req, res, next) => { | |
const todo = createTask(req.body.description) | |
const id = todo.id | |
todos.set(id, todo) | |
.then(() => { | |
res.send(JSON.stringify(todo)) | |
}) | |
.catch(err => next(err)) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function createTask(description) { | |
const task = { | |
'@class': 'Task', | |
id: uuid.v4().substr(0, 6), | |
createdAt: Date.now(), | |
description: description, | |
completed: false | |
} | |
return task |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
api.get('/api/tasks/events', sse(), (req, res, next) => { | |
const listener = new MapListener() | |
listener.on('insert', data => { | |
res.sse.event('insert', data.newValue) | |
}).on('update', data => { | |
res.sse.event('update', data.newValue) | |
}).on('delete', data => { | |
res.sse.event('delete', data.oldValue) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
api.get('/api/tasks', (req, res, next) => { | |
const completed = req.query.completed | |
const filter = completed | |
? Filters.equal('completed', completed) | |
: Filters.always() | |
const toSend = [] | |
tasks.values(filter) | |
.then(async values => { | |
// copy values to array to be sent via express |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
api.delete('/api/tasks/:id', (req, res, next) => { | |
tasks.delete(req.params.id) | |
.then(oldValue => { res.sendStatus(oldValue ? 200 : 404)}) | |
.catch(err => next(err)) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
api.delete('/api/tasks', (req, res, next) => { | |
tasks.invokeAll(Filters.equal('completed', true), | |
Processors.conditionalRemove(Filters.always())) | |
.catch(err => next(err)) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
api.put('/api/tasks/:id', (req, res, next) => { | |
const description = req.body.description | |
const completed = req.body.completed | |
let processor = null | |
if (description) { | |
processor = Processors.update('description', description) | |
} | |
if (typeof(completed) !== 'undefined') { |
OlderNewer