Last active
April 22, 2021 22:54
-
-
Save zemm/abbcebac45c334c227503e71c19ac1ba to your computer and use it in GitHub Desktop.
Minimal go serve server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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