-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
37 lines (31 loc) · 874 Bytes
/
main.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
package main
import (
"flag"
"fmt"
"log"
"net/http"
"time"
)
type fileHandler struct {
handle http.Handler
}
func (h fileHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
begin := time.Now()
log.Printf("[Remote=%s] [Path=%s] [RequestStart]", r.RemoteAddr, r.URL.Path)
w.Header().Set("Cache-control", "no-store")
h.handle.ServeHTTP(w, r)
log.Printf("[Remote=%s] [Path=%s] [RequestEnd] [ResponseTime=%s]",
r.RemoteAddr, r.URL.Path, time.Since(begin))
}
func main() {
port := flag.String("port", "8085", "Port to listen")
path := flag.String("path", ".", "path to serve")
flag.Parse()
log.Println("Listenting on port", *port, "serving", *path)
fs := http.FileServer(http.Dir(*path))
http.Handle("/", fileHandler{http.StripPrefix("/", fs)})
err := http.ListenAndServe(fmt.Sprintf(":%s", *port), nil)
if err != nil {
log.Fatalln(err)
}
}