Ludovic Frank - Freelance developer

A microservice for generating QR codes with a simple HTTP link

ionicons-v5-k Ludovic Frank Apr 1, 2024
78 reads Level:

Hey there 😁,

Thank you for clicking on this article, I hope you like it 😉.

Well, uh... aren't we supposed to be in spring right now? No, because it's cold in the Far East...

Ah yes, that's right, we're still not here for it 😮, guess I'm doing it on purpose huh 😛 (yes, I'm totally doing it on purpose).

This week it's going to be a cool little article, the big articles will come back, don't worry, but this week, we're just going to go for a microservice that can be a "deal breaker" for your applications.

We're simply going to set up a microservice that can be used by any of our applications to generate QR codes.

Why do I need to generate QR codes?

As always, every article comes from something that's been going on in my life 🙂 QR codes have gone from being something confined to the technophile world to something everyone uses since 2020.

As far as I'm concerned, I've experimented with QR codes for lots of things, but overall nothing that's stuck for long except for ... the reservation app 😮.

It currently displays the restaurant's menu in several languages, so customers have a QR code for each language, and then they get the menu directly in their language...

But that's not where this article comes from...

As you may already know, I'm currently adapting my reservation application to offer it to other restaurants... and I've made QR codes a central feature.

You have to understand that in the world of people who aren't like us, i.e. who aren't into technology, things that we find simple aren't so obvious to them, and that's normal.

With this in mind, the reservation application allows you to download a QR code, which is in fact a link to the reservation process, and then simply integrate it into a Word document or any other graphic creation, and off you go: customers passing by the restaurant can make a reservation or consult the menu.

But the thing is, I didn't think it was simple enough to create a Word document, which takes time... what if we created a PDF ready to print, with a clear text explaining that by scanning the QR code you can make a reservation? What's more, we could add the restaurant's logo...

That's it, I like it, it's simple and functional, no frills, just a click... For the PDF, I used " WKHTMLToPdf" with the"KnpSnappyBundle" bundle. And of course, for the QR code, the microservice I'm talking about in this article.

For those of you who are curious, I've put together a short video on my LinkedIn page.

QRServe, the microservice

Without further ado, here's the microservice I use: QRServe.

It's GO (like Docker 😛), so, static binaries, it runs anywhere... we'll see how I deploy it in the next paragraph, but for now, how does it work?

All you need to do is make a simple HTTP request in an HTML "img" tag, for example, with three parameters:

  • Data: "?data=URL" knowing that URL, you'll have to use an "urlencode()" on it, well yes if you put an & directly in your URL, you can imagine that there's going to be a problem... you're going to pass your argument directly to the microservice.
  • Size: "?size=500", the size of the QR code in pixels. 600 isn't bad for a QR code that's going to be printed.
  • Q: "?q=l" The level of error correction (QR codes have error correction systems, for example if part of the QR code is destroyed), it's possible to set this value to l, m or H, for low, medium or high.

I can see where you're coming from, you're the type who wants more ... so I've deployed the microservice here.
If you scan this QRCode, you'll be redirected to the Hacker's Journal site 😁.

Use this instance for fun and testing, but don't use it in production in your applications: deploy your own 🙂.

Dockerize QRServe

Well of course... GO running via a program in GO, that's GOCeption ... Or maybe it's just that it's very convenient 😁.

The Dockerfile

So, my Dockerfile is a bit old (is even more so if you're reading this article in the future), so, consider updating it, at least it'll serve you as a base 🙂 :

Remarkably simple, isn't it? Technology is beautiful 😍.

The docker-compose.yml

Well, while we're at it, I'll also give you the docker-compose.yml 🙂 :

So, yes the "silentk/qrserve" image does exist on DockerHub, but it's out of date, so you'll know 😛.

Also, here I'm assuming that on your infra, there's a "nginx-proxy", so the real ones will have to adapt it to use "Traefik", it's up to you 😁.

Conclusion

There you go, now you can use QR Code in all your applications...

That was easy, wasn't it?

As far as I'm concerned, I'll see you next time 🙂 and thank you for reading this far 😛.