Skip to content

Instantly share code, notes, and snippets.

@rodkranz
Created April 21, 2017 10:11
Show Gist options
  • Save rodkranz/7e3a7018059154011bca6bc1f8a7a6f3 to your computer and use it in GitHub Desktop.
Save rodkranz/7e3a7018059154011bca6bc1f8a7a6f3 to your computer and use it in GitHub Desktop.
Simple file server in GO
package main
import (
"net/http"
"fmt"
"flag"
"log"
)
var (
addr string
port int
)
func init() {
flag.StringVar(&addr, "addr", "0.0.0.0", "Define address that server will run.")
flag.IntVar(&port, "port", 9100, "Define port that server will listen.")
flag.Parse()
}
func main() {
mux := http.NewServeMux()
fs := http.FileServer(http.Dir("./"))
mux.Handle("/", http.StripPrefix("/", fs))
log.Printf("Server running: %s:%d", addr, port)
http.ListenAndServe(fmt.Sprintf("%s:%d", addr, port), Logger(mux))
}
func Logger(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Printf("Requested: %s", r.URL.Path)
h.ServeHTTP(w, r)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment