Last active
February 4, 2022 13:27
-
-
Save 17xande/b5defeb0bb239e3bcd47d86840d2d728 to your computer and use it in GitHub Desktop.
Simple Go HTTP Static Server
This file contains hidden or 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 ( | |
"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