Serving the current directory over HTTP with Go

Featured image for sharing metadata for article

I've recently been updating a few lightweight static websites and have wanted to preview the changes locally.

Although I usually reach for python -mhttp.server, it's a bit cumbersome to type, so I wanted something a little shorter. I was thinking of writing a script to make it quicker to type, when I thought - "what if I can do it quicker in Go"?

It turns out, it's blazingly fast to do so, in two lines(!) of code:

package main

import (
	"log"
	"net/http"
)

func main() {
	http.Handle("/", http.FileServer(http.Dir(".")))
	log.Fatal(http.ListenAndServe("127.0.0.1:8080", nil))
}

That's it! This allows you to serve the current directory that the built binary is executed in over HTTP.

I've wrapped this into a standalone tool just to make it easier to install and run, and I may end up adding some more (limited scope) functionality, while leaving it as lightweight as possible.

Written by Jamie Tanna's profile image Jamie Tanna on , and last updated on .

Content for this article is shared under the terms of the Creative Commons Attribution Non Commercial Share Alike 4.0 International, and code is shared under the Apache License 2.0.

#blogumentation #go.

This post was filed under articles.

Interactions with this post

Interactions with this post

Below you can find the interactions that this page has had using WebMention.

Have you written a response to this post? Let me know the URL:

Do you not have a website set up with WebMention capabilities? You can use Comment Parade.