Created
April 21, 2017 10:11
-
-
Save rodkranz/7e3a7018059154011bca6bc1f8a7a6f3 to your computer and use it in GitHub Desktop.
Simple file server in GO
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 ( | |
"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