/ tutorials

Making a basic webserver in go

Welcome!

I know its been a bit longer then the few weeks I said it would take to get a blog post out, but its here now. Today I'm going to teach you how to make a basic webserver in go, a language developed by google, and serve a page.

Why do I want to run a webserver in go?

Go is notorious for its light resource use and performance. During my testing, a webserver only took up less then 2mb of RAM! Compared to nodejs, which uses 15mb of RAM to do the same thing, and you can start to see why you want to use go to run a webserver.

So how do I do it?

Lets get into it, I am however assuming you have go and all the relevant programs installed.

To start, we need to start the webserver. In go, we have a func main() which is executed by the program when it runs. In there, we want to write the following:

func main() {
	http.HandleFunc("/", handler)
	err := http.ListenAndServeTLS(":8400", "ssl/ca.crt", "ssl/ca.key", nil)
	fmt.Println("Webserver running.")
	if err != nil {
		log.Fatal("listenandserve: ", err)
	}
}

So, inside our main function, the first line handles all the requests coming in. We send this to our handler function, which I will talk about later. We then have the next line, which sets up the webserver, on port 8400 in my case. If you look further down the line, it has a key and certificate specified. That's how easy it is to import your ssl private key and certifcate, no messing round with any modules to read our files.

We then print a message saying the webserver has started, and it puts out an error to the console if there is any problems. Lets get onto the handler function, which is where our files are actually served.

Just above our main function, insert this

func handler(res http.ResponseWriter, req *http.Request) {
	http.ServeFile(res, req, req.URL.Path[1:])
}

Yes, that's it, no more. This is all we need to start serving static files. Lets talk over that main line. We give http.ServeFile our response and request, so it can grab data about the request and write the response to the user. The final argument is where you specify what file you want to serve the user, and what I've put in gets the URL (eg '/page.html', '/directory') and finds the appropriate file/directory to serve.

Here is the entire code for this program:

//Basic webserver to serve static files- https://danielgray.me
package main

import (
	"fmt"
	"log"
	"net/http"
)

func handler(res http.ResponseWriter, req *http.Request) {
	http.ServeFile(res, req, req.URL.Path[1:])
}

func main() {
	http.HandleFunc("/", handler)
	err := http.ListenAndServeTLS(":8400", "ssl/ca.crt", "ssl/ca.key", nil)
	fmt.Println("Webserver running.")
	if err != nil {
		log.Fatal("listenandserve: ", err)
	}
}

Testing time!

Now, create an index.html in the directory your go program is, write something and run go run FILENAME.go in the terminal. Now navigate to the site in your browser, and you should see the content of your html file you just wrote! (Please note, if you are using a self-signed certificate, its normal to see an error about a 'bad certificate' in the console).

Voilla! Your index page is served.

You can even make directories and serve them:

A wonderful subdirectory where I can serve more awesome content.

And here is an interesting use case, using the webserver to host files (to do this, just make sure your directory has no index.html) :

Oooh, using the webserver to host files!

Now unfortunately, that's the end of todays tutorial. I hope you find this useful, and feel free to share this with your friend if you did (I have no objections!). I'll see you in the next blog post, which will be soon, I promise this time. Bye!

Questions? Feel free to contact me via whatever method you want, you can find some over at my website (Although I do discourage the use of bird for communications, they tend to have trouble finding me).

Making a basic webserver in go
Share this