/
main.go
48 lines (42 loc) · 998 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
38
39
40
41
42
43
44
45
46
47
48
package main
import (
"flag"
"log"
"net"
"net/http"
"regexp"
)
var (
port string
dir string
)
func init() {
flag.StringVar(&port, "port", "7070", "server running port")
flag.StringVar(&dir, "dir", ".", "file server path")
flag.Parse()
}
func main() {
fs := http.FileServer(http.Dir(dir))
http.Handle("/", EncodeHandler(fs))
lip := GetOutboundIP()
log.Printf("Serving %s on HTTP %s:%s\n", dir, lip, port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}
func GetOutboundIP() net.IP {
conn, err := net.Dial("udp", "8.8.8.8:80")
if err != nil {
log.Fatal(err)
}
defer conn.Close()
localAddr := conn.LocalAddr().(*net.UDPAddr)
return localAddr.IP
}
func EncodeHandler(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
re, _ := regexp.Compile("[.](gz)$")
if re.MatchString(r.URL.Path) {
w.Header().Add("Content-Encoding", "gzip")
}
next.ServeHTTP(w, r)
})
}