Skip to content

Latest commit

 

History

History
44 lines (30 loc) · 1.11 KB

serving_static_files_from_a_go_app.md

File metadata and controls

44 lines (30 loc) · 1.11 KB

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