Andrew Katsikas - Software Engineer

Building a personal web service in Go, TDD Style: Part 5 - The End

Bringing it all together

Now that the logic of my application was working well, I decided to try to tackle the problem I shelved at the end of my 3rd blog post of this series - I had no hostname to call my server’s own. I did briefly consider if I could somehow add a 3rd Virtual Host to my Apache HTTP Server and somehow route that to my Go web service, but that felt like a bit of a hack and looked unnecessarily complex (not to mention it would add latency to incoming requests). I decided to bring my static content out of Apache and run everything in the same Go web service.

Three sites for the price of one

I was using chi to accomplish routing, and found they also offered a package called hostrouter which would allow me to route multiple domains into the same web service. I was paying for 2 domains already, and decided on splitting it up like this:

I kept my frontend JavaScript code in the New Hell Studios static Apache folder, but that also meant I had to solve for a new issue - implementing the Cross-origin resource sharing W3 specification so that my web browser would allow API calls to be made to https://api.newhellstudios.com from https://newhellstudios.com. I found a package that accomplished exactly that for my Go web service by Olivier Poitrey, simply called cors.

Although all 3 domains would be served out of the same server, I did not want to mix codebases. Fortunately, Go lets you use GitHub URLs as Go modules, so it was as easy as creating a new repository for all my websites and adding my backend service as a dependency in my go.mod file.

That’s all folks

Now my web service was working beautifully and all my domains could happily share the same backend. To bring things to a meta conclusion, I will explain how I got the blog post you are reading into the mix.

I added another route/domain called https://katsikas-dev.newhellstudios.com which I decided would serve my personal website and blog. While I have a good working understanding of CSS and HTML I am not much of a web designer. My friend had suggested a static site builder called Hugo which is ultra-fast, written in Go and has a lot of cool themes that you can plug in. I was pretty fond of the bearblog theme so I went with that. I added a few commands to some Makefiles in my various repositories so I could easily build and deploy my new blog posts and code changes as they came!

#golang #go #hugo #apache #javascript #cors #github