Building a Web Server in Go: Handling Page Requests

Background

Over winter break last year (December 15, 2013 to January 12, 2014) I worked with Robb Seaton, a friend of mine since middle school, to create a webserver in the programming language Go. We spent about a week working through A Tour of Go before beginning working on the website/web server (for ~2 weeks), in other words we were not very experienced Go Programmers. The idea was to learn to program in Go by working through the tour and just jumping into a pretty intense project.

Both Robb and I had prior experience programming. I had/have experience with C, C++, Python, Ruby, BASIC, and Java and Robb had/has experience with C, Ruby, Haskell, R and probably other languages I do not know (he knows). We at this point had both been programming for upwards of 5 years so we are not exactly new to programming and we were already fairly familiar with C programming so Go was not a very difficult language to transition into.

In writing this article I hope to explain how simple it is to write a web server in Go, as well as describing some methods we used to create a nice webpage via templating.

Web Server Layout

For those who do not understand the basics of a web server (as opposed to a website): a web server receives an HTTP request from a web browser. The web server then responds by returning a webpage which the browser requested (or an error, such as 404 if the file is not available).

Screen Shot 2014-05-19 at 9.14.04 PM

In many cases, a webpage is served via the LAMP (stack) software bundle,

LAMP_software_bundle.svg

In this case, as opposed Apache as a Web Server we replaced it with Go, and the data base was replaced with MongoDB (NoSQL), since it was easily integrated with Go.

Learning to write a web server may be a good idea because there have been several benchmark test where Go (1.2+) handles HTTP requests significantly faster and can handle just as many requests as most of its competition [1][2].

Handling Web Pages in Go

Golang.org, has a fairly decent wiki article regarding web applications and I recommend reading it if you are serious about developing anything for the web via Go. From the wiki they show you how to use the “net/http” library to create a basic web server. Yes, folks it is really that easy!

In our case we decided some really basic CSS and HTML would make it look a bit nicer. We therefore decided to use the templating system in Go, which allowed us to server various portions of the page separately:

Since, we were using the templating system we could then load various aspects of the page relatively easily:

As you can see we can load the user data, the slide bar, and options all separately from the body into the CSS and if we desired we could also update only a single portion at a time (potentially saving server calls). In our case, this function was called when the user requested the page, and it needed to be “handled” via the http.Handlefunc(“/”, handler):

The page should then be displayed in the templated format to the user, pretty much as a standard webpage (plus or minus CSS mistakes I made while testing). If you would like to see our full code you can view it on the github there are quite a few mistakes, code smells, and various other issues. However, I felt it was important to share how to build a website backend/web server in Go (Golang). There were not many articles written about the subject and I intend to improve our web server and possibly use it in a website in the near future.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

 characters available

Time limit is exhausted. Please reload the CAPTCHA.