-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
67 lines (58 loc) · 1.26 KB
/
main.go
File metadata and controls
67 lines (58 loc) · 1.26 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"fmt"
"io/ioutil"
"net"
"net/http"
"github.com/fatih/color"
)
func getIntranetIP() (ipList string, err error) {
ifaces, err := net.Interfaces()
if err != nil {
return "", err
}
for _, iface := range ifaces {
addrs, err := iface.Addrs()
if err != nil {
continue
}
for _, addr := range addrs {
switch v := addr.(type) {
case *net.IPNet:
if v.IP.To4() != nil && !v.IP.IsLoopback() {
return v.IP.String(), nil
}
case *net.IPAddr:
if v.IP.To4() != nil && !v.IP.IsLoopback() {
return v.IP.String(), nil
}
}
}
}
return "", fmt.Errorf("not found")
}
func main() {
webRoot := "."
listenPort := 8000
fs := http.FileServer(http.Dir(webRoot))
http.Handle("/", fs)
color.Yellow("Listening...")
color.Yellow("You can access with:")
ip, _ := getIntranetIP()
color.Green(" http://%s:%d\n\n", ip, listenPort)
files, err := ioutil.ReadDir(webRoot)
if err == nil {
color.Yellow("List files: ")
for _, file := range files {
path := file.Name()
if file.IsDir() {
path += "/"
}
color.Blue(" http://%s:%d/%s\n", ip, listenPort, path)
}
}
fmt.Println("")
if err := http.ListenAndServe(fmt.Sprintf(":%d", listenPort), nil); err != nil {
color.Red("error happend: %v", err)
}
}