Skip to content

Instantly share code, notes, and snippets.

@zemm
Last active April 22, 2021 22:54
Show Gist options
  • Save zemm/abbcebac45c334c227503e71c19ac1ba to your computer and use it in GitHub Desktop.
Save zemm/abbcebac45c334c227503e71c19ac1ba to your computer and use it in GitHub Desktop.
Minimal go serve server
package main
import (
"flag"
"log"
"net/http"
)
func loggingHandler(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Println(r.Method, r.URL.Path)
h.ServeHTTP(w, r)
})
}
func main() {
var addr string
var path string
flag.StringVar(&addr, "addr", ":8080", "Listen address")
flag.StringVar(&path, "path", ".", "Path to serve")
flag.Parse()
log.Printf("Serving pages from %s at %s", path, addr)
http.Handle("/", loggingHandler(http.FileServer(http.Dir("."))))
err := http.ListenAndServe(addr, nil)
log.Printf("Error while serving: %v", err)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment