Skip to content

Commit

Permalink
Added a mock-server for api with file dbstorage through json server r…
Browse files Browse the repository at this point in the history
…un with npm run mock
  • Loading branch information
bfritscher committed Feb 11, 2018
1 parent 6cfd713 commit 7e69410
Show file tree
Hide file tree
Showing 10 changed files with 2,164 additions and 294 deletions.
15 changes: 15 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Debug mock server",
"cwd": "${workspaceFolder}/frontend",
"program": "${workspaceFolder}/frontend/build/mock-server.js"
},
]
}
3 changes: 3 additions & 0 deletions frontend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ npm install
# serve with hot reload at localhost:8080
npm run dev

# serve a mock api server emulating backend
npm run mock

# build for production with minification
npm run build

Expand Down
69 changes: 69 additions & 0 deletions frontend/build/mock-server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const jsonServer = require('json-server')
const server = jsonServer.create()
const router = jsonServer.router('static/cocktail.json')
const middlewares = jsonServer.defaults()
const SSE = require('express-sse')
const status = {
carrierDistMm: 0,
payloadWeightGr: 0
}
const sse = new SSE()

server.get('/api/sse/status', sse.init)
setInterval(() => {
sse.send(status)
status.carrierDistMm = Math.round(Math.random() * 500)
status.payloadWeightGr += Math.round(Math.random() * 10)
if (status.payloadWeightGr > 500) {
status.payloadWeightGr = 0
}
}, 1000)

// Set default middlewares (logger, static, cors and no-cache)
server.use(middlewares)
server.use(jsonServer.bodyParser)

// Add custom routes before JSON Server router
server.post('/api/command/order', (req, res) => {
console.log(req.body)
if (Array.isArray(req.body) &&
req.body.every((i) => i.hasOwnProperty('ingredientId') && i.hasOwnProperty('ml'))) {
// 409 if busy
Math.random() > 0.5 ? res.send('ok') : res.sendStatus(409)
} else {
res.sendStatus(500)
}
})

server.post('/api/command/stop', (req, res) => {
res.send('ok')
})

server.get('/api/debug/move/:mm', (req, res) => {
console.log(req.params)
res.send(true)
})

server.get('/api/debug/servo/:servoId/:aperture', (req, res) => {
console.log(req.params)
res.send(true)
})

server.get('/api/debug/fill/:mm/:servoId/:aperture', (req, res) => {
console.log(req.params)
res.send(true)
})

server.get('/api/debug/tare', (req, res) => {
res.send('not implemented')
})

server.get('/api/debug/test/:message', (req, res) => {
res.send('ok')
})

// Use default router
server.use('/api', router)
server.listen(3000, () => {
console.log('JSON Server is running')
})
4 changes: 2 additions & 2 deletions frontend/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ module.exports = {
assetsPublicPath: '/',
proxyTable: {
'/api': {
target: 'http://10.10.0.99:8080',
changeOrigin: true,
target: 'http://localhost:3000',
changeOrigin: true
}
},
// CSS Sourcemaps off by default because relative paths are "buggy"
Expand Down
Loading

0 comments on commit 7e69410

Please sign in to comment.