index.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Require the framework and instantiate it
  2. const fastify = require("fastify")({ logger: true });
  3. const cors = require("@fastify/cors");
  4. const axios = require("axios");
  5. const fs = require("fs");
  6. fastify.register(cors, {
  7. // put your options here
  8. });
  9. fastify.route({
  10. method: "GET",
  11. url: "*",
  12. schema: {
  13. querystring: {
  14. name: { type: "string" },
  15. excitement: { type: "integer" },
  16. },
  17. response: {
  18. 200: {
  19. type: "object",
  20. properties: {
  21. hello: { type: "string" },
  22. },
  23. },
  24. },
  25. },
  26. handler: function (request, reply) {
  27. console.log(request.url);
  28. if (request.url === "/parsecd") {
  29. const buffer = fs.readFileSync(
  30. "./parsecd.wasm"
  31. );
  32. reply.type("application/wasm");
  33. reply.send(buffer);
  34. return;
  35. } else if (request.url === "/") {
  36. axios.get(`https://web.parsec.app${request.url}`).then((res) => {
  37. let html = res.data.replace(
  38. "</body>",
  39. fs.readFileSync("./keyboard.html") + "</body>"
  40. );
  41. for (let key in res.headers) {
  42. reply.header(key, res.headers[key]);
  43. }
  44. reply.send(html.toString());
  45. });
  46. return;
  47. }
  48. axios
  49. .get(`https://web.parsec.app${request.url}`, {
  50. // headers: request.headers,
  51. })
  52. .then((res) => {
  53. reply.headers = res.headers;
  54. for (let key in res.headers) {
  55. reply.header(key, res.headers[key]);
  56. }
  57. reply.send(res.data);
  58. })
  59. .catch((e) => {
  60. console.log(e);
  61. });
  62. },
  63. });
  64. fastify.listen({ port: 3000 }, function (err, address) {
  65. if (err) {
  66. fastify.log.error(err);
  67. process.exit(1);
  68. }
  69. // Server is now listening on ${address}
  70. });