-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
82 lines (62 loc) · 1.33 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
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
"crypto/tls"
"fmt"
"net/http"
"strings"
"time"
)
type webRequest struct {
r *http.Request
w http.ResponseWriter
doneCh chan struct{}
}
var (
requestCh = make(chan *webRequest)
registerCh = make(chan string)
unregisterCh = make(chan string)
heartbeat = time.Tick(5 * time.Second)
)
var (
transport = http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
)
func init() {
http.DefaultClient = &http.Client{Transport: &transport}
}
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
doneCh := make(chan struct{})
request := webRequest{
r: r,
w: w,
doneCh: doneCh,
}
requestCh <- &request
<-doneCh
})
go processRequests()
go http.ListenAndServe(":2000", nil)
go http.ListenAndServe(":2002", new(appserverHandler))
println("Load balancer stared, pres <Enter> to exit.")
fmt.Scanln()
}
type appserverHandler struct{}
func (h *appserverHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ip := ""
if strings.HasPrefix(r.RemoteAddr, "[::1]") {
ip = "localhost"
} else {
ip = strings.Split(r.RemoteAddr, ":")[0]
}
port := r.URL.Query().Get("port")
switch r.URL.Path {
case "/register":
registerCh <- ip + ":" + port
case "/unregister":
unregisterCh <- ip + ":" + port
}
}