Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deno.serve QRCode Server Example for All #17

Open
suchislife801 opened this issue Oct 25, 2023 · 4 comments
Open

Deno.serve QRCode Server Example for All #17

suchislife801 opened this issue Oct 25, 2023 · 4 comments

Comments

@suchislife801
Copy link

suchislife801 commented Oct 25, 2023

The following code snippet serves a QRCode using Deno.

URL Params Example:

http://localhost:4000/?d=Hello+World!&v=5&e=L&s=250&m=Byte

import { qrcode } from "https://deno.land/x/qrcode/mod.ts";
import { decodeBase64 } from "https://deno.land/[email protected]/encoding/base64.ts";

// Deno.serve() handler
async function handler(request: Request, connInfo: Deno.ServeHandlerInfo): Promise<Response> {

  // Information for a HTTP request.
  const remoteAddr = connInfo.remoteAddr;

  const url = new URL(request.url);
  // Data to encode...
  const d = url.searchParams.get("d") || "Hello World!";
  // Version of barcode 0 (Automatic type number) - 40
  // https://www.qrcode.com/en/about/version.html
  const v = Math.min(40, Math.max(0, Number(url.searchParams.get("v")) || 0));
  // Error Correction L = 7% | M = 15% | Q = 25% | H = 30%
  const e = ["L", "M", "Q", "H"].includes(url.searchParams.get("e")) ? url.searchParams.get("e") : "M";
  // Size of barcode 250 - 500
  const s = Math.min(500, Math.max(250, Number(url.searchParams.get("s")) || 250));
  // Mode = 'Numeric' | 'Alphanumeric' | 'Byte' | 'Kanji'
  const m = ['Numeric', 'Alphanumeric', 'Byte', 'Kanji'].includes(url.searchParams.get("m")) ? url.searchParams.get("m") : "Byte";

  const base64Image = await qrcode(d, { typeNumber: v, errorCorrectLevel: e, size: s, mode: m }); // data:image/gif;base64,...
  const binaryImage = new Uint8Array(decodeBase64(base64Image.split(",")[1]));

  return new Response(binaryImage, {
    status: 200,
    headers: {
      "Server": `Deno v${Deno.version.deno}`,
      "Allow": "HEAD, GET, OPTIONS",
      "Cache-Control": "no-store",
      "Content-Language": "en-US",
      "Access-Control-Allow-Origin": "*",
      "Access-Control-Allow-Methods": "HEAD, GET, OPTIONS",
      "Access-Control-Allow-Headers": "Content-Type",
      "Access-Control-Allow-Credentials": "false",
      "Content-Type": "image/gif",
    },
  })

}

// Start the QRCode server of Port 4000
Deno.serve({
  port: 4000,
  hostname: "0.0.0.0",
  onListen({ port, hostname }) {
    console.log(`Application..... QR Code - Model 2 - A QR Code Model 2 Server
Deno v${Deno.version.deno} : Typescript v${Deno.version.typescript} : V8 v${Deno.version.v8}
Permissions..... --allow-net
Gateway URL..... http://${hostname}:${port}\n\n`);
  },
  onError(error: unknown) {
    console.error(error);
    return new Response("HTTP 500: Internal Server Error", {
      status: 500,
      headers: { "Content-Type": "text/plain" },
    });
  },
}, handler);

// deno run --allow-net qrServe.ts

Hello World!

sample

@JJ
Copy link
Collaborator

JJ commented Oct 25, 2023

So you want to create a PR for this?

@suchislife801
Copy link
Author

Hmmm... well, I'm not sure the code I'm showcasing "can be" part of this library as it is a server after all.

I just saw a few posts wondering what can be done with this library, some looking for documentation even so I took a dive into the source code and eventually concluded most people are going want this as a micro-service.

What are your thoughts?

@JJ
Copy link
Collaborator

JJ commented Oct 27, 2023

Well, it can be somewhere else, in an examples directory, for instance.

@suchislife801
Copy link
Author

suchislife801 commented Oct 27, 2023

Sounds good. I'll polish a little more and post a url safe working example.

I'm working on an HTMX example as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants