-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
66 lines (57 loc) · 1.17 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import (
"flag"
"fmt"
"log"
"net"
"net/http"
"os"
"strings"
"github.com/liserjrqlxue/RDMO/router"
)
var (
port = flag.String(
"port",
":9091",
"web server listen port",
)
)
var StaticDir = make(map[string]string)
func main() {
flag.Parse()
_ = os.Mkdir("data", 0755)
http.HandleFunc("/loadMO", router.LoadMO)
http.HandleFunc("/updateMO", router.UpdateMO)
StaticDir["/static"] = "static"
StaticDir["/public"] = "public"
http.HandleFunc(
"/",
func(w http.ResponseWriter, r *http.Request) {
// static file server
for prefix, staticDir := range StaticDir {
if strings.HasPrefix(r.URL.Path, prefix) {
file := staticDir + r.URL.Path[len(prefix):]
fmt.Println(file)
http.ServeFile(w, r, file)
return
}
}
router.Index(w, r)
},
)
fmt.Printf("start http://%v%v\n", GetOutboundIP(), *port)
err := http.ListenAndServe(*port, nil)
if err != nil {
log.Fatal(err)
}
}
// Get preferred outbound ip of this machine
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
}