-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
70 lines (57 loc) · 1.29 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
67
68
69
70
package main
import (
"log"
"net/http"
"net/http/httputil"
"net/url"
"os"
"time"
)
var (
badToken = "bad token"
tick = time.Second * 10
timeout = time.Second * 2
pong = "PONG"
done = "DONE"
tokenKey = "token"
serverKey = "server"
noEnv = "no env %s"
newRequest = "new request %s: %s"
urlHub = "http://%s:8080/register"
)
func main() {
log.Println("start server ...")
token := os.Getenv(tokenKey)
if token == "" {
log.Fatalf(noEnv, tokenKey)
return
}
server := os.Getenv(serverKey)
if server == "" {
log.Fatalf(noEnv, serverKey)
return
}
go initPing(server, token)
http.HandleFunc("/register", func(w http.ResponseWriter, r *http.Request) {
log.Printf(newRequest, r.RequestURI, r.RemoteAddr)
index(token, w, r)
})
u, _ := url.Parse("http://127.0.0.1:4444")
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
log.Printf(newRequest, r.RequestURI, r.RemoteAddr)
tokenEnv := os.Getenv(tokenKey)
token := r.Header.Get(tokenKey)
if token != tokenEnv {
w.WriteHeader(400)
w.Write([]byte(badToken))
log.Println(badToken)
return
}
proxy := httputil.NewSingleHostReverseProxy(u)
proxy.ServeHTTP(w, r)
})
err := http.ListenAndServe(":6677", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}