If Venom told you about this site she's full of shit~

Making an API in Go

Update! 2022-11-29

I am now a lot more into rust than go so I haven't explored this topic any further. Making an API in rust is actually pretty sweet and I might revisit this topic in rust.

Fun thing: to get this line seperating I had to write it as:
<h1 class="post-title"></h1></code>

Why make an API?

In my case it's to log the temp of my pool. Me and my old man have been working on custom pool heating and we wanted to automate it a little.

I've been working on an arduino to measure the temp and to automatically shut off the heat pump when it is at X temp or it is X-PM in the arvo.

The arduino I'm using has an ESP 8266 wifi chip, so I'm gonna use that to access the API.


I made the API in go so it could be light wight and ultra performant!

It has two very simple functions;

Log temp to a CSV

By going to http://localhost:8080/log/{temp} it saves whatever is after log/ as the temp along with the current time and date inside of a CSV file

Display all entries inside the CSV as a HTML table

By going to http://localhost:8080/list/ it simple displays all entries as a table for easy checking

List of all temps


It's actually extremely simple;


using the mux http routing library I can easily get the temp variable just by listening to /log/{temp}

    // create a new instance of a mux router
 r := mux.NewRouter()

 // treat anything after /log/ as the temp variable and call handleTemp
 r.HandleFunc("/log/{temp}", handleTemp)

the handleTemp function simply returns 200 OK to the user and calls aother function

    func handleTemp(w http.ResponseWriter, r *http.Request) {
     // read temp from request
     vars := mux.Vars(r)

     // reply with 200 OK

     // log temp

logTemp only does a couple of things, it generates the date and time and then creates an entry in the csv file

    // Get current date and time
 dt := time.Now()

    // Create new record
 csvTitles := []string{dt.Format("01-02-2006 15:04:05"), temp}

 // Append record to CSV
 if err := w.Write(csvTitles); err != nil {
  log.Fatalln("error writing record to file", err)


When you go to /list/ it just lists through the CSV and quickly generates some basic HTML

    htmlResponse := "<html><head><style>table, th, td {\n  border: 1px solid black;\n}</style></head><body><table><tr><th>Date</th><th>Temperature</th></tr>"

 for _, record := range records[1:] {
  htmlResponse += "<tr><td>" + record[0] + "</td><td>" + record[1] + "</td></tr>"

 htmlResponse += "</table></body></html>"


Writing a super basic API/Website in go is actually super easy! This was an enjoyable learning experince and went super smoothly.

I'm excited to learn more about Go APIs in the future!

Learning HUGO

Update! 2022-11-29

I have now moved over to Zola, A rust based static site generator. It is pretty simalar to hugo but I will eventually make a post like this about Zola and it's differences.

Fun thing: to get this line seperating I had to write it as:
<h1 class="post-title"></h1></code>
HUGO logo

Learning HUGO was interesting

So right off the bat I decided to make something less then normal to learn some basics and it was pretty fun, I then moved to actually learning how to create this main website.

Dates are weird

A major issue I had while making this very page was adding a date, I tried to give it the date 2021-12-09T17:16:00 which was the exact time I started to write the page after consulting Heap about the date issue, They informed me I cannot make dates that are in the future.

I'm Australian so all my dates are in the futue, I have the UTC offset of +10 but even setting the offset to that 2021-12-09T17:16:00+10:00 it would still have an issue and not appear on my page, AS IT TURNS OUT I need to tell it I'm half way between east australia and New Zealand and set my UTC offset to +11 2021-12-09T17:16:00+11:00

After that it was smooth sailing, here is the header info from the .md file making up this page:

title: Learning HUGO
description: HUGO is an interesting tool.
- learning
- open-source
- code
date: 2021-12-09T17:16:00+11:00
slug: learning-hugo
image: /img/vendor/hugo.svg

Pretty simple right?

Adding an image to the preview render has been a pain in my butt :triumph:

Preview of Facebook share Preview of Twitter share

The theme in use on this website provides the ability to have Open Graph tags for each page, a slight issue is that it only allows you to use global image instead of an image for each specific post which is kinda sad.

In order to have the image above (Art by Weaves) I had to set a global image inside of my HUGO config file:

    og_image = "https://wip.buymymojo.net/img/og-image.png"

I'm going to put a request into the theme's repo for support for a per post image but ofc if it doesnt add it that's fine. It's an open source theme for an open source tool, if I eventually learn HUGO more intametly I will make my own fork and link it here!

One final hiccup

In this theme if there is a folder /pages and a folder /posts it will only display the posts inside of /pages on the front page, if you remove it then it shows the files in /pages so I've moved to only using the pages folder

So the current strcture is:

├── archtypes/
│   └── default.md
├── content/
│   └── posts/
│       ├── 2021-12-09-learning-hugo.md
│       └── about.md
├── static/
│   ├── img/
│   │   ├── screencaps/
│   │   │   ├── share-preview-20211209-1912.png
│   │   │   └── share-preview-20211209-191348.png
│   │   ├── vendor/
│   │   │   ├── hugo.png
│   │   │   └── hugo.svg
│   │   └── og-image.png
│   ├── android-chrome-192x192.png
│   ├── android-chrome-512x512.png
│   ├── apple-touch-icon.png
│   ├── favicon-16x16.png
│   ├── favicon-32x32.png
│   ├── favicon.ico
│   └── site.webmanifest
├── themes/
│   └── archie
├── .gitignore
├── .gitlab-ci.yml
├── config_pages.toml
├── config.toml
└── README.md

Go check out the source for this website and the theme used here

Credit to Ana Ulin for their raw HTML shortcode example I am now using

follow me into the Fediverse