servedir/main.go

33 lines
660 B
Go
Raw Normal View History

2013-10-21 13:45:14 -07:00
package main
import (
2013-10-21 19:21:59 -07:00
"flag"
"fmt"
2013-10-21 13:45:14 -07:00
"log"
"net/http"
"os"
2013-10-21 13:45:14 -07:00
)
2013-10-21 19:21:59 -07:00
var port = flag.Int("port", 8000, "port from which to serve")
2013-10-21 13:45:14 -07:00
func logger(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Printf("%s: %s\n", r.RemoteAddr, r.URL)
h.ServeHTTP(w, r)
})
}
func main() {
2013-10-21 19:21:59 -07:00
flag.Parse()
hostname, err := os.Hostname()
if err != nil {
log.Fatal("problem getting hostname:", err)
}
log.Printf("serving on: http://%s:%d/", hostname, *port)
2013-10-21 19:21:59 -07:00
addr := fmt.Sprintf(":%d", *port)
2013-10-21 13:45:14 -07:00
fh := http.FileServer(http.Dir("./"))
2013-10-21 19:21:59 -07:00
if err := http.ListenAndServe(addr, logger(fh)); err != nil {
log.Fatal(err)
}
2013-10-21 13:45:14 -07:00
}