diff --git a/mock-backend/package-lock.json b/mock-backend/package-lock.json index 5a3378f..2d070b7 100644 --- a/mock-backend/package-lock.json +++ b/mock-backend/package-lock.json @@ -9,6 +9,7 @@ "version": "1.0.0", "license": "ISC", "dependencies": { + "cors": "^2.8.5", "json-server": "^0.17.4", "uuid": "^9.0.1" }, diff --git a/mock-backend/package.json b/mock-backend/package.json index 7cfab8c..9540fa0 100644 --- a/mock-backend/package.json +++ b/mock-backend/package.json @@ -12,6 +12,7 @@ "author": "", "license": "ISC", "dependencies": { + "cors": "^2.8.5", "json-server": "^0.17.4", "uuid": "^9.0.1" }, diff --git a/mock-backend/src/server.js b/mock-backend/src/server.js index f37b5d9..ad65d9a 100644 --- a/mock-backend/src/server.js +++ b/mock-backend/src/server.js @@ -1,7 +1,7 @@ import jsonServer from "json-server"; import routes from "./mocks/routes.json" assert { type: "json" }; import { v4 as uuid } from "uuid"; - +import cors from 'cors' const server = jsonServer.create(); const router = jsonServer.router("./src/mocks/db.json"); const middlewares = jsonServer.defaults(); @@ -9,7 +9,7 @@ const middlewares = jsonServer.defaults(); const localStorage = []; server.use(middlewares); - +server.use(cors({ origin: '*' })) server.use(jsonServer.rewriter(routes)); server.use((req, res, next) => { @@ -49,16 +49,41 @@ router.render = (req, res) => { } else { res.status(404).jsonp({}); } - } else if (req.url === "/stream") { - res.status(200).jsonp({ - data: "The answer is 42", - sources: [ - "https://en.wikipedia.org/wiki/42_(number)", - "https://en.wikipedia.org/wiki/42_(number)", - ], - conversation_id: "1234", + } else if (req.url === "/stream" && req.method === "POST") { + console.log('pinged !') + res.writeHead(200, { + 'Content-Type': 'text/event-stream', + 'Cache-Control': 'no-cache', + 'Connection': 'keep-alive' }); - } else { + const message = ('Hi, How are you today?').split(' '); + let index = 0; + const interval = setInterval(() => { + if (index < message.length) { + res.write(`data: {"answer": "${message[index++]} "}\n`); + } else { + res.write(`data: {"type": "id", "id": "65cbc39d11f077b9eeb06d26"}\n`) + res.write(`data: {"type": "end"}\n`) + clearInterval(interval); // Stop the interval once the message is fully streamed + res.end(); // End the response + } + }, 500); // Send a word every 1 second + } + else if (req.url === '/search' && req.method === 'POST') { + res.status(200).json( + [ + { + "text": "\n\n/api/answer\nIt's a POST request that sends a JSON in body with 4 values. It will receive an answer for a user provided question.\n", + "title": "API-docs.md" + }, + { + "text": "\n\nOur Standards\n\nExamples of behavior that contribute to a positive environment for our\ncommunity include:\n* Demonstrating empathy and kindness towards other people\n", + "title": "How-to-use-different-LLM.md" + } + ] + ) + } + else { res.status(res.statusCode).jsonp(res.locals.data); } };