-
Notifications
You must be signed in to change notification settings - Fork 10
/
app.go
107 lines (97 loc) · 2.18 KB
/
app.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
import (
sq "subquery"
"net/http"
"flag"
"fmt"
"net"
"log"
"os"
"time"
"encoding/json"
)
var appServerPort *int = flag.Int("appServerPort", 80, "appServer port")
var cacheip *string = flag.String("cacheip", "127.0.0.1", "memcache server ip")
var timeouts uint64
func JsonHandler(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
decoder := json.NewDecoder(r.Body)
var req sq.JsonRequest
err := decoder.Decode(&req)
sq.Check(err)
timeStart := time.Now()
go sq.ParseRequest(req.D, timeStart)
time.Sleep(time.Microsecond*10)
fmt.Fprintln(w, 1)
}
func FlushHandler(w http.ResponseWriter, r *http.Request) {
for _, conf := range sq.DepConfigs {
sq.Subs[conf.Name].CacheSema.Acquire()
sq.Subs[conf.Name].CacheClient.FlushAll()
sq.Subs[conf.Name].CacheSema.Release()
fmt.Fprintln(w, "Flushed", conf.Name)
}
}
func IdHandler(w http.ResponseWriter, r *http.Request) {
ifaces, err := net.Interfaces()
if err != nil {
fmt.Fprintln(w, "fail")
return
}
for _, iface := range ifaces {
if iface.Flags&net.FlagUp == 0 {
continue // interface down
}
if iface.Flags&net.FlagLoopback != 0 {
continue // loopback interface
}
if iface.Name != "eth2" {
continue
}
addrs, err := iface.Addrs()
if err != nil {
fmt.Fprintln(w, "fail")
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
}
if ip == nil || ip.IsLoopback() {
continue
}
ip = ip.To4()
if ip == nil {
continue // not an ipv4 address
}
// return ip.String(), nil
fmt.Fprintln(w, ip.String())
return
}
}
fmt.Fprintln(w, "fail")
return
}
func main() {
flag.Parse()
value := os.Getenv("BYPASS")
if len(value) == 0 {
sq.BypassCaches = false
} else {
sq.BypassCaches = true
}
sq.InitSubSystems(*cacheip)
http.HandleFunc("/json", JsonHandler)
http.HandleFunc("/flush", FlushHandler)
http.HandleFunc("/id", IdHandler)
log.Fatal(
http.ListenAndServe(
fmt.Sprintf(":%d", *appServerPort),
nil,
),
)
}