Jan 4, 2019

Up and running

Adventures with writing a framework in Go

Welcome to zortac.net!

Let me introduce myself: I am Moritz, at the moment of writing seventeen years old and a student in my hometown of Wunstorf, Germany. I also am a network administrator (in school), a PC enthusiast, an entrepreneur and, most of all, a software developer. I’ve been writing software for the larger part of my life now, starting with Delphi Pascal, moving to C++ and now mainly Go. But so far, I’ve never gotten to actually finishing a big project. I’ve had many ideas, sure, but I didn’t pursue any of them to completion, either due to time constraints, losing interest or discovering that someone had already written a better solution. I decided to change this.

So, I decided to write my own web framework using Go. Aren’t there enough web frameworks out there already?, you ask, and most likely, I’d answer that question with Yes, but writing a framework is an excellent practice project and that’s what I intended this project to be.

Writing a framework is actually quite easy in Go, as the web is really what Go was made for. So, here are some lessons learned:

1. Write wrappers

Writing wrappers around libraries, especially core parts of your application, will save you a lot of work in the future. Writing wrappers might seem like unnecessary overhead, especially in a language like Go, where you don’t have to deal with different code conventions like in C++. But wrappers allow for one thing absolutely necessary in larger applications: modularity.

When I started writing this framework, I used Go’s default net/http HTTP-router, because I thought is was sufficient for my use case. I wrote a wrapper around it, not because thought about changing the server in the future, but because I wanted to add logging to specific actions such as creating a handler. But after I’ve started working on the “Blog”-part of this framework, I noticed that net/http doesn’t support pattern matching in URLs, something that I needed for serving post-specific pages. I decided to switch to the excellent gorilla/mux HTTP-router, something that would have been a lot of work, had I not written a wrapper around my HTTP-server. With the wrapper however, the switch was as easy as maybe changing 10 lines of code, not needing to touch any of the stuff that depends on the HTTP-server.

2. Docker and docker-compose

This was my first project with Docker. I’ve looked at Docker a bit and quite recently decided to move all the things running on my server into Docker containers, for easier backup and maintenance. This switch is almost complete by now and I will probably write a post about it in the future.

So when I started writing this framework, I knew it would be running in a Docker container and decided to design the framework with Docker in mind from the beginning. After some difficulties in the beginning I got my Dockerfile to a functioning stage and after that development continued without problems. And, I must say, I like Docker. Having an app encapsulated in a container saves you a lot of headaches, port availability and dependencies probably being the largest of them.

What really made Docker awesome however, was docker-compose. For those of you that don’t know: docker-compose is a utility that allows you to configure and deploy multiple containers in a system. I was worried about having to manually configure the connection between the Go framework and the database, making backups of the database etc. but with docker-compose all this (networks and Docker volumes) can be configured once, in a single file and ported to almost any system.

(In the end, I still had to manually configure the networking and volumes because I use Portainer as container management and I had some trouble loading the docker-compose file, but more on that in a later post.)

3. The Go Templating Engine

I didn’t do very much web programming before this framework, so I don’t know a whole lot about the different templating engines out there, but I was surprised (again) by the power of the Go standard library. I used Go’s html/template a few times before, never doing anything complicated. But during this project it quickly became apparent that Go’s templates are quite powerful and can pretty much do anything your heart desires. I’d recommend you to give it a try, it you haven’t already.