-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
52 lines (48 loc) · 1.07 KB
/
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
49
50
51
52
package main
import (
"fmt"
"io"
"log"
"net"
"net/http"
"os"
"github.com/gorilla/handlers"
)
func main() {
str, _ := os.Getwd()
var port string
if len(os.Args) > 1 {
port = os.Args[1]
} else {
port = ":8080"
}
log.Println(str)
fs := http.FileServer(http.Dir(str))
f := handlers.CustomLoggingHandler(log.Writer(), fs, func(writer io.Writer, params handlers.LogFormatterParams) {
str := fmt.Sprintf("%v %v %v %v\n",
params.TimeStamp.Local().Format("2006/01/02 15:04:05"),
params.StatusCode,
params.Request.Method,
params.Request.URL,
)
writer.Write([]byte(str))
})
http.Handle("/", f)
log.Println("Listening on", getInternalIP(), port)
log.Panic(http.ListenAndServe(port, nil))
}
func getInternalIP() (ips []string) {
addrs, err := net.InterfaceAddrs()
if err != nil {
return
}
for _, address := range addrs {
// check the address type and if it is not a loopback the display it
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
ips = append(ips, ipnet.IP.String())
}
}
}
return
}