forked from darkhelmet/balance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
32 lines (28 loc) · 870 Bytes
/
http.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
package main
import (
BA "github.com/darkhelmet/balance/backends"
"github.com/gonuts/commander"
"log"
"net/http"
"net/http/httputil"
)
func httpBalance(bind string, backends BA.Backends) {
log.Println("using http balancing")
proxy := &httputil.ReverseProxy{Director: func(req *http.Request) {
req.URL.Host = backends.Choose()
}}
log.Printf("listening on %s, balancing %d backends", bind, backends.Len())
err := http.ListenAndServe(bind, proxy)
if err != nil {
log.Fatalf("failed to bind: %s", err)
}
}
func init() {
fs := newFlagSet("http")
cmd.Commands = append(cmd.Commands, &commander.Command{
UsageLine: "http [options] <backend> [<more backends>]",
Short: "performs http based load balancing",
Flag: *fs,
Run: balancer(httpBalance),
})
}