-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservethis.go
47 lines (40 loc) · 1.11 KB
/
servethis.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package main
import (
"flag"
"log"
"net/http"
"os"
"path/filepath"
"strings"
)
var (
port string
path string
)
func main() {
// Catch flags
flag.StringVar(&port, "port", "8000", "Port to serve content on")
flag.StringVar(&path, "path", ".", "Directory to serve content from")
flag.Parse()
// We want the hostname to make a nice easy to copy link
hostname, err := os.Hostname()
if err != nil {
hostname = "0.0.0.0"
}
hostname = strings.ToLower(hostname)
absolutePath, err := filepath.Abs(path)
if err != nil {
absolutePath = "./"
}
log.Printf("Starting http server to serve '%s' at:\nhttp://%s:%s/", absolutePath, hostname, port)
fileHandler := http.FileServer(http.Dir(absolutePath))
wrappedHandler := AccessLoggingHandler(fileHandler)
log.Fatal(http.ListenAndServe(":"+port, wrappedHandler))
}
// A bit excessive? Perhaps. But I enjoy seeing what's going on while the server is running.
func AccessLoggingHandler(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Println(r.Method, r.RemoteAddr, "=>", r.RequestURI)
h.ServeHTTP(w, r)
})
}