Creating a blog engine

At first glance, it seemed like it would be easy. #

So a little bit of background first. I’m currently working on designing my own open source blog system right now, but it’s not yet completed. I thought it would be fun to write about my progress here until my actual site is done. It might be a while.

I’ve been learning Go in my free time on and off for the last few months. It’s a great programming language, and I’d highly recommend it if you’ve never tried it. Go makes a lot of things easy through its simple syntax and the amazing standard library. If there happens to be anything missing, the community also has an enormous volume of libraries. It’s a language that makes things nearly as simple as Python, but it’s compiled to machine code, so it’s really fast.

There are dozens of other reasons to love the language, but that’s not what this is about. I’m here to talk about everything else that makes designing a blog from the ground up much harder. I thought “Hey, I can pull some libraries together and get a back-end done in an hour or so!” And the front-end can just be simple HTML and CSS! Ha!…

Designing things correctly #

The easiest mistake is to get started too early. It’s an easy trap to fall into - especially while you’re still learning a language. Here are a couple of the design issues that I wasted time on:

Store my data in JSON files… No, Postgres… No, SQLite… #

I wanted to stay away from a heavy database at first, since I thought it would be easier to design, and easier to add information. After all, I didn’t think that I needed to scale THAT much! I decided to use JSON files. I started designing the system around an architecture where loading a page would read the necessary information from its corresponding JSON file. This was all going well until I realized that I’m going to need a homepage that lists all of the post titles and published dates! That’s a lot of IO for loading a homepage.

Turns out, databases are perfect for this kind of problem. Who knew! I can query for all of the information about a page when I need to load that page, or just query for the title and date of all the pages to load a homepage! Plus I already am familiar with Postgres. Problem solved, right?!

Eh, it worked fine, but I’m not an expert with deployments and configuration, and I was having a lot of problems with deploying my application to a Docker container and having it communicate with the Postgres container that I was running. I also wasn’t sure about how I was going to do backups. Luckily, this time around I designed the database layer well, and it was as easy as changing a few lines of code to switch to an SQLite database. I think that this solution is the best for now – I can focus my energy on other things.

Write posts in markdown, render to HTML #

This was a simple problem that I probably made too hard. I tried a couple of different approaches before settling on one. First, I thought that I’ll render the markdown on the front-end. I found an excellent rendering library in Javascript called ShowdownJs. It worked well, but I was indecisive…

My next thought was to render the markdown on the back-end, inject it into the page, and send it all completed to the user. I found another excellent rendering library, this time for Go, called Black Friday. I found that Go has support for templating HTML files, so I implemented that.

Then, I thought the back-end was getting too complicated. I switched to the current implementation of having two endpoints - one for getting the static HTML, and one for getting the page metadata (title, content, author, date, etc). I made small script in JS using the ES6 fetch API, and upon receiving a response, it injected the content into the HTML. This is pretty quick and greatly simplified the code.

How to add posts?! #

It sounds easy, but there are a few decisions that have to be made here. The hardest is whether or not to expose an endpoint that allows editing your blog’s content. It makes sense to have write and edit endpoints, but how to secure them?

This is something that I’m still working on. This is one of the main reasons that my own site isn’t up yet. Turns out, security is hard!

Configuring HTTPS #

It’s 2018 – your site should be using SSL.

If you haven’t heard of it yet, Let’s Encrypt is a 100% free Certificate Authority. It’s supported by big names like Mozilla, Google, Facebook, and the EFF.

Go makes it super easy to start a server using HTTPS, you just need to point it to the certificate files.

But where to host it? #

This was actually the easiest question to answer. Google Cloud Platform has a lifetime free-tier that will meet the needs of any low-volume website. I pay $0 for a debian server that I have full access to, with the backing of Google’s lightning-fast network. They also have a free trial, and I recommend giving them a shot.

So what now? #

I’m going to keep working on my own blog engine and website, but it’s probably going to take a while. Until then, I’ll be hosting this site here on Svbtle!

 
6
Kudos
 
6
Kudos

Now read this

Go has some weird built-in functions

Go is generally an exceptionally simple language, and that’s reflected through it’s very small number of keywords – one of the lowest of any modern language (source). There are also some built-in functions that I’m sure you use every day... Continue →