-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
48 lines (42 loc) · 1021 Bytes
/
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
package main
import (
"context"
"net/http"
"net/http/httputil"
"net/url"
"github.com/seipan/mylb/backend"
"github.com/seipan/mylb/utils"
"go.uber.org/zap"
)
// Serve serves a loadbalancer.
func main() {
backends := backend.NewDefaultBackend()
for _, b := range backends {
url, err := url.Parse(b.GetURL())
if err != nil {
utils.Error("usrl parse err",
zap.String("error", err.Error()),
zap.String("url", b.GetURL()),
)
}
b.SetReverProxy(httputil.NewSingleHostReverseProxy(url))
}
serverPool, err := utils.GetPoolType(backends)
if err != nil {
utils.Error("get pool type err",
zap.String("error", err.Error()),
)
}
go healthCheck(context.Background(), serverPool)
go benchCheck(context.Background(), serverPool)
lbHandler := NewLBHandler(serverPool)
s := http.Server{
Addr: ":" + "8080",
Handler: http.HandlerFunc(lbHandler.Serve),
}
if err := s.ListenAndServe(); err != nil {
utils.Error("listen and serve err",
zap.String("error", err.Error()),
)
}
}