-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
78 lines (66 loc) · 1.62 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
package main
import (
"gopkg.in/yaml.v2"
"io/ioutil"
"log"
"net/http"
"net/http/httputil"
"net/url"
"os"
)
var lastServedIndex int
type Server struct {
Url string `json:"url"`
}
func (s *Server) IsHealthy() bool {
if res, err := http.Head(s.Url); err == nil {
return res.StatusCode == http.StatusOK
} else {
return false
}
}
func (s *Server) ReverseProxy() *httputil.ReverseProxy {
serverUrl, err := url.Parse(s.Url)
if err != nil {
log.Fatalln(err.Error())
}
return httputil.NewSingleHostReverseProxy(serverUrl)
}
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
availableServers := yamlReader()
noOfServers := len(availableServers)
for lastServedIndex < noOfServers {
server := availableServers[lastServedIndex]
if server.IsHealthy() {
server.ReverseProxy().ServeHTTP(w, r)
log.Printf("-- server %s served\n ", availableServers[lastServedIndex].Url)
counter(&lastServedIndex, noOfServers)
return
} else {
counter(&lastServedIndex, noOfServers)
}
}
})
log.Printf("<--> load balancer server started at port %s\n", os.Args[1])
log.Fatalln(http.ListenAndServe(os.Args[1], nil))
}
func yamlReader() []*Server {
var servers map[string][]*Server
yamlFile, err := ioutil.ReadFile("./servers.yaml")
if err != nil {
log.Printf("yamlFile.Get err #%v ", err)
}
err = yaml.Unmarshal(yamlFile, &servers)
if err != nil {
log.Fatalf("Unmarshal: %v", err)
}
return servers["servers"]
}
func counter(lastServedIndex *int, totalServersCount int) {
if *lastServedIndex == totalServersCount-1 {
*lastServedIndex = 0
} else {
*lastServedIndex++
}
}