-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
42 lines (37 loc) · 788 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
package main
import (
"fmt"
"log"
"net/http"
"net/http/httputil"
"net/url"
)
var (
servers = []string{
"http://127.0.0.1:5000",
"http://127.0.0.1:5001",
"http://127.0.0.1:5002",
"http://127.0.0.1:5003",
"http://127.0.0.1:5004",
}
lastServedIndex = 0
)
func main() {
http.HandleFunc("/", forwardRequest)
http.ListenAndServe(":8080", nil)
}
func forwardRequest(rw http.ResponseWriter, req *http.Request) {
url := getServer()
reverseProxy := httputil.NewSingleHostReverseProxy(url)
fmt.Println("Routing request to the server", url)
reverseProxy.ServeHTTP(rw, req)
}
func getServer() *url.URL {
nextIndex := (lastServedIndex + 1) % 5
url, err := url.Parse(servers[lastServedIndex])
lastServedIndex = nextIndex
if err != nil {
log.Fatal(err)
}
return url
}