-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdsf.go
55 lines (48 loc) · 956 Bytes
/
dsf.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
49
50
51
52
53
54
55
package main
import (
"flag"
"fmt"
"net"
"net/http"
)
var (
root = flag.String("root", "./", "root of files")
port = flag.Int("port", 8080, "port to listen")
)
func main() {
flag.Parse()
var err error
defer func() {
if err != nil {
fmt.Printf("fatal error: %s\n", err)
}
}()
interfaces, err := net.Interfaces()
if err != nil {
err = fmt.Errorf("listing net interfaces: %w", err)
return
}
for _, i := range interfaces {
var addrs []net.Addr
addrs, err = i.Addrs()
if err != nil {
err = fmt.Errorf("i.Addrs: %w", err)
return
}
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
fmt.Printf("listening on: http://%s:%d\n", ip, *port)
}
}
err = http.ListenAndServe(fmt.Sprintf(":%d", *port), http.FileServer(http.Dir(*root)))
if err != nil {
err = fmt.Errorf("http.ListenAndServe: %w", err)
return
}
}