-
Notifications
You must be signed in to change notification settings - Fork 1
/
vdistribute.go
70 lines (56 loc) · 1.68 KB
/
vdistribute.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
package main
import "net/http"
import "log/syslog"
import "io/ioutil"
import "io"
import "os"
import "github.com/pborman/getopt"
var servers []string
var slog *syslog.Writer
func vDistribute(w http.ResponseWriter, r *http.Request) {
client := &http.Client{
}
slog.Notice("distributor called " + r.Method + " " + r.Host + " " + r.RequestURI)
var body []byte
var status int
var statusText string
for _, server := range servers {
req, _ := http.NewRequest(r.Method, "http://" + server + r.RequestURI, nil)
req.Header.Add("Host", r.Host)
req.Host = r.Host
req.Header = r.Header
req.URL.Opaque = r.RequestURI
resp, err := client.Do(req)
if err != nil {
slog.Notice (server + " error returned:" + err.Error())
continue
}
defer resp.Body.Close()
body, _ = ioutil.ReadAll(resp.Body)
slog.Notice (server + " returned:" + resp.Status)
status = resp.StatusCode
statusText = resp.Status
}
// return last status and body
w.Header().Add("Status", statusText);
w.WriteHeader(status)
io.WriteString(w, string(body))
}
func main() {
slog, _ = syslog.New(syslog.LOG_INFO, "[vdistribute]")
defer slog.Close()
var l = getopt.String('a', ":6083", "listen port (:6083)")
var opts = getopt.CommandLine
opts.Parse(os.Args)
if opts.NArgs() > 0 {
for _, arg := range opts.Args() {
slog.Notice("Adding Server:" + arg)
servers = append(servers, arg)
}
} else {
slog.Notice("Not Enough Servers")
os.Exit(1)
}
http.HandleFunc("/", vDistribute)
http.ListenAndServe(*l, nil)
}