index.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { serve } from "https://deno.land/std@0.185.0/http/server.ts";
  2. import init, {render, JsResponse} from "./pkg/deno_wasm.js";
  3. const handler = async (request: Request): Promise<Response> => {
  4. await init();
  5. let url = new URL(request.url);
  6. if (url.pathname === '/pkg/deno_wasm.js') {
  7. const file = await Deno.open("./pkg/deno_wasm.js", {read: true});
  8. const readableStream = file.readable;
  9. const response = new Response(readableStream, {
  10. headers: {
  11. 'content-type': 'text/javascript'
  12. }
  13. });
  14. return response;
  15. }
  16. if (url.pathname === '/pkg/deno_wasm_bg.wasm') {
  17. const file = await Deno.open("./pkg/deno_wasm_bg.wasm", {read: true});
  18. const readableStream = file.readable;
  19. const response = new Response(readableStream, {
  20. headers: {
  21. 'content-type': 'application/wasm'
  22. }
  23. });
  24. return response;
  25. }
  26. if (url.pathname === '/favicon.ico') {
  27. return new Response (null, {
  28. status: 404
  29. });
  30. }
  31. else {
  32. try {
  33. const response = await render(request);
  34. let body_response = response.get().body_response;
  35. return new Response(body_response.body, {
  36. status: body_response.status_code,
  37. headers: {
  38. "content-type": body_response.content_type
  39. }
  40. });
  41. } catch (e) {
  42. const body = `ERROR: ${e}`
  43. return new Response(body, {
  44. status: 500,
  45. headers: {
  46. "content-type": "text/html; charset=utf-8"
  47. }
  48. });
  49. }
  50. }
  51. }
  52. serve(handler, {port: 3000, host: "192.168.100.29"});