Back to basics

Not sure why but uploading files kinda bothered me. I know it's a simple thing but for some reason in my head I built it up quite a bit. But I had to face my discomfort yesterday. I had gotten as far as I could by doing everything else and now the upload section had to be done. Day one is always not the best. I tried to take the easy way out and look for solutions out there. Safe to say that I spent all of yesterday trying to wrestle with others interpretation of the solution.

Today I decided to actually understand it. Build the most basic upload feature end to end. This meant two steps:

  1. Uploading a file from the UI and sending it to the server
  2. Open the file on the server

The rest of the steps did not really bother me and I knew I could do it. It was just upload the file from the server to S3 and save the details in the database. Not hard.

Getting file from the user

This doc in the Htmx website saved me a lot of time. Also, felt glad I was using Htmx. It allows me to lean on the browser to do all the difficult stuff. All I had to do was look up the other events for an xhr request from Htmx to let the user know what is happening with their file. Just like that I had a working proof of concept. The UI was the scary bit for me. I knew the server would be easier

Getting the file on the server

Even here thanks to Golangs http library, it was actually quite easy. Turns out getting files from the http.Request object in Go is super simple. All I needed was a couple of lines:

	err = r.ParseMultipartForm(500 << 20) // limiting to 500mb for now
	...

	fileHeaders := r.MultipartForm.File["files"]

The rest took hardly any time at all. The S3 API expects an io.Reader and some other details which is quite easy. I was surprised when the whole thing worked first try. After I had the file on the server.

Make it better (later)

I re-learned a huge lesson today. When I'm working on something I have no idea about, get it work in the simplest and ugliest way possible first. The rest is surprisingly quite easy.

It's a simple and quite obvious lesson, which I was aware of. But somehow when my premonitions get involved, I tend to forget the simplest things.

Grokking is fun

I've been quite enjoying this journey of building things with the bare minimum. It's one of the lessons I've learned from the browser project that I have been working on for a while now. There is a lot of value in understanding how things work. At first they seem time consuming but the feeling I get when I understand things is indescribable.

Reading this article on grokking vi early on in my career has been extremely valuable. It has and continues to effect my programming half a decade later after having read it.