-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
48 lines (43 loc) · 842 Bytes
/
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
package main
import (
"fmt"
"net/http"
"net/http/httputil"
"net/url"
"math/rand"
)
var backendHosts []Host
type Host struct {
url *url.URL
}
func init() {
backendHosts = append(backendHosts, Host{
url: &url.URL{
Scheme: "http",
Host: "localhost:8000",
},
})
backendHosts = append(backendHosts, Host{
url: &url.URL{
Scheme: "http",
Host: "localhost:8001",
},
})
backendHosts = append(backendHosts, Host{
url: &url.URL{
Scheme: "http",
Host: "localhost:8002",
},
})
}
func LbHandler(w http.ResponseWriter, r *http.Request){
index := rand.Intn(len(backendHosts))
fmt.Println(index)
randomHost := backendHosts[index]
httputil.NewSingleHostReverseProxy(randomHost.url).ServeHTTP(w, r)
}
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", LbHandler)
http.ListenAndServe(":8005", mux)
}