Skip to content

Instantly share code, notes, and snippets.

@17xande
Last active February 4, 2022 13:27
Show Gist options
  • Save 17xande/b5defeb0bb239e3bcd47d86840d2d728 to your computer and use it in GitHub Desktop.
Save 17xande/b5defeb0bb239e3bcd47d86840d2d728 to your computer and use it in GitHub Desktop.
Simple Go HTTP Static Server
package main
import (
"log"
"net/http"
"os"
)
func Log(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Printf("%s %s %s\n", r.RemoteAddr, r.Method, r.URL)
handler.ServeHTTP(w, r)
})
}
func main() {
// Simple static webserver:
port := "3000"
if len(os.Args) > 1 {
port = os.Args[1]
}
log.Printf("Listening on http://localhost:%s\n", port)
log.Fatal(http.ListenAndServe(":"+port, Log(http.FileServer(http.Dir(".")))))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment