123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- const jsonServer = require('json-server')
- const jsonDb = require('./static_db.json')
- const timezoneDb = require('./timezone.json')
- const server = jsonServer.create()
- const router = jsonServer.router() // for persistence : jsonServer.router('static_db.json')
- const middlewares = jsonServer.defaults()
- const GLOBAL_PORT = 3001
-
- function shuffle(array) {
- let currentIndex = array.length
- let randomIndex
-
- // While there remain elements to shuffle...
- while (currentIndex !== 0) {
- // Pick a remaining element...
- randomIndex = Math.floor(Math.random() * currentIndex)
- currentIndex -= 1
-
- // And swap it with the current element.
- ;[array[randomIndex], array[currentIndex]] = [array[currentIndex], array[randomIndex]]
- }
-
- return array;
- }
-
- server.use(middlewares)
- server.use(jsonServer.bodyParser)
-
- // res.jsonp(req.query)
- server.get('/echo', (req, res) => res.jsonp('gg'))
- server.get('/login', (req, res) => res.jsonp(jsonDb.login))
- server.get('/user_logged', (req, res) => res.jsonp(jsonDb.user_logged))
- server.delete('/deletenodata', (req,res) => res.status(204).jsonp(''))
- server.patch('/user', (req, res) => res.jsonp({lang: 'fr'}))
- // server.put('/api/data/raw_materials_vendors/:vendorid', (req, res) => {
- // res.jsonp(jsonVendorColorData.vendorVariableData)
- // console.log(req.body)
- // res.jsonp('gg')
- // })
-
- server.get('/lang', (req, res) => res.jsonp(jsonDb.lang))
-
- server.post('/user/login', (req, res) => {
- if (req.body.login !== '' && req.body.password !== '') return res.jsonp(jsonDb.user_logged)
- else return res.jsonp('error')
- })
-
- server.get('/app/config', (req, res) => res.jsonp(jsonDb.app_config))
-
- server.get('/user/is_logged_in', (req, res) =>
- // res.jsonp({"logged": false})
- res.jsonp(jsonDb.user_logged)
- )
-
- server.get('/user/:id/workspace', (req, res) => res.jsonp(jsonDb.workspace_list))
-
- server.get('/workspace/:id', (req, res) => res.jsonp(
- Object.assign(
- {},
- jsonDb.workspace_detail,
- {content: shuffle(jsonDb.workspace_detail.content.map(
- c => Object.assign({}, c, {workspace_id: req.params.id})
- ))},
- {id: req.params.id}
- )
- ))
-
- server.get('/workspace/:idws/folder/:idf', (req, res) => {
- switch (req.params.idf) {
- case '3':
- return res.jsonp(jsonDb.folder_content_3)
- case '11':
- return res.jsonp(jsonDb.folder_content_11)
- }
- })
-
- server.get('/user/:id/roles', (req, res) => res.jsonp(jsonDb.user_role))
-
- server.get('/timezone', (req, res) => res.jsonp(timezoneDb.timezone))
-
- server.get('/workspace/:idws/content/:idc', (req, res) => {
- switch (req.params.idc) {
- case '1': // pageHtml
- return res.jsonp(jsonDb.content_data_pageHtml)
- case '2':
- return res.jsonp(jsonDb.content_data_thread)
- }
- })
-
- server.get('/workspace/:idws/content/:idc/timeline', (req, res) => {
- switch (req.params.idc) {
- case '1': // pageHtml
- return res.jsonp(jsonDb.timeline)
- case '2':
- return res.jsonp([])
- }
- })
-
- server.use(router)
- server.listen(GLOBAL_PORT, () => {
- console.log('JSON Server is running on port : ' + GLOBAL_PORT)
- })
|