til

Today I Learned: collection of notes, tips and tricks and stuff I learn from day to day working with computers and technology as an open source contributor and product manager

View project on GitHub

Serving Static Files From a Go App

When I developed pxy-redirect, a simple app in Go hosted with DigitalOcean, I wanted to service a static page.

Serving an index.html from the root of my repository worked fine when testing locally, but not when the app was deployed to DigitalOcean.

The solution was placing the static assets in a subdirectory, like: static/.

So I went from:

http.ServeFile(w, r, "index.html")

To:

http.ServeFile(w, r, "static/index.html")

And in addition I added:

  • favicon.ico
  • robots.txt
if url.String() == "/robots.txt" {
    http.ServeFile(w, r, "static/robots.txt")
    return
}

if url.String() == "/favicon.ico" {
    http.ServeFile(w, r, "static/favicon.ico")
    return
}

All in all a nice little collection for a basic HTTP server not relying on a framework, just Go.

Resources and References