Great teachers on the internet preach that in order to learn a new language effectively, you have to build something with it. Also, those great teachers are pretty much tech Twitter, which can be toxic and polarizing. So take their lessons with a grain of salt.
Anyway, I’ve been trying to learn Golang for a while, done some courses, taken notes on it, and even got stuck in tutorial hell for some time until the problem to practice the language presented itself.
What is my kid going to have for lunch/breakfast today?
As a software engineer, I started the journey to see how to even answer that question, which led me to finding the school site that basically has a calendar with all the options available for kids to pick from and for parents to be aware of their kids’ diet… that could’ve been it… no need to do anything else and move on with my life, but there’s no automation or a way for me to get that information delivered every day at specific times. I’m not a fan of logging into something just to get a glimpse of some data.
I almost gave up, but of course, knowing how the web works, I figured, what is feeding this page? Is there some sort of API underneath? If not, maybe I can do some good ol’ scraping?
An API was hiding in there after all; I found it by checking the Network tab while loading the page. I found an endpoint that basically feeds the entire site, and you can filter down to specific days or school districts. The endpoint looks something like this
You can then pass specific parameters to filter your specific school from the district and couple calls later you end up with a response that contains a lot of data, things like calories and nutritional information which is pretty nice, but what I’m after is the block that contains what is actually for both: Breakfast and Lunch.
Knowing how the data looks like and what we need from it, we can then start doing some Go!
The design for this mini project is quite easy:
- Query the API filtering down the school and date.
- Parse the response from the API
- Form a new structure on how I want the data to look like
- Print out the end structure
That’s pretty much it, yes?. A simple Golang script that does an HTTP request and prints out something…which is a good starting point but of course that doesn’t solve the automation and notification portion and since I like to overengineer my life we will also turn this into a Kubernetes cronjob that posts a message to my telegram channel. So we will also need the following:
- Kubernetes Namespace and Cronjob to launch it on a schedule
- Dockerfile to build the image for the script/app
- CICD to upload the image to an image registry (DockerHub)
- Code to include sending data to Telegram
- Secrets to obscure details like my kids school name and the tokens for telegram (Doppler)
Build stuff time
TLDR: If you just want to see the finished thing check out the repos meal-notifier and k8s-manifest
So we start by simply building the URL with the parameters needed.
Note: on the API you need to pass the date range, so it knows when to pull from and since I only need today’s meal information we provide it twice.
Send the request and parse the response, so it matches a struct. In this case I used online tools that generate a struct out of a JSON. The actual response was huge, so I removed a lot of things that were not needed, so we end up with a pretty slim object/type.
Once we have our response in a Golang struct we can then start the madness and iterate over it, since the API returns a pretty extensive object with a lot of nested lists, several loops were required to get to the information we are after. I’m sure there are more efficient and probably better ways to achieve this, but my primitive brain just went with something simple.
With the payload built we can simply then just pass this along to another function that sends the message to Telegram.
That pretty much covers the application. Like I mentioned, it’s a pretty simple get/post request script. This could’ve been done in bash am I right!?!?!.
Some Devops’ing?
With the product built we then proceed to make sure this runs on a schedule and that it can pull secrets from my preferred provider Doppler.
First thing we need is to build the container image, which is pretty easy, but we are adding the Doppler CLI on it so when the container boots it can connect to Doppler using a service account token and inject those secrets at.
Now that the image can be built, we do some CI/CD to make sure this gets built and published to DockerHub. The action will be pretty simple as it will just install docker, build the image and push it out. Do note your repo will need to have the secrets, so it knows how to connect to the hub.
The final step is to make this run in Kubernetes, because we like to complicate our life right… this could’ve been in a cronjob on one of my Homelab machines but NO we like the Kubes!. The manifest will simply generate the namespace and define the cronjob to run and when to do it.
Key thing is that the container needs to know the Doppler service account token, so it can retrieve those secrets, so that token resides within the cluster and gets pulled down by the cronjob.
With everything said and done, the cronjob will execute and now every Monday through Friday I will have a nice message on Telegram that looks like this:
Notification from Telegram
Cronbjob Execution.
Conclusion
Learn by doing is the way to go!. And this was my first “real” project using Go so building something so simple took me couple hours just to understand how Go manages things and what methods were the ones I needed to use. I’m quite happy with how it turned out, and it gave me the fuel needed to trying more things with the language.
So now I’m waiting for the next “problem” to present itself, so I can smack it with some Go!.
Hope you liked it. See ya on the next one.
Adios 👋