/
main.go
148 lines (127 loc) · 3.67 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Docker Proxy acts as a web server to web browsers serving the GUI application
// files. It also acts as a proxy for clients (GUI and CLI) to access Docker
// hosts. Docker related events originating from the Docker Hosts are being
// aggregated for client use. Handles Cross-Origin Resource Sharing (CORS)
// restrictions on XMLHttpRequests
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"handlers"
"io"
"io/ioutil"
"net/http"
"time"
)
// Command line flags.
type flags struct {
isProxy bool
listenIP string
listenPort string
proxyIP string
proxyPort string
name string
}
// Docker Agents send heartbeats to Docker Proxy.
type heartbeat struct {
fromIP string
fromName string
}
// Http routing map.
var mux map[string]func(http.ResponseWriter, *http.Request)
func printMux() {
for key, value := range mux {
fmt.Println("Key:", key, "Value:", value)
}
}
func getCmdLineArgs() flags {
isProxy := flag.Bool("x", true, "run as proxy")
listenIP := flag.String("lip", "0.0.0.0", "listen IP address")
listenPort := flag.String("lp", "8005", "listen port")
proxyIP := flag.String("pip", "0.0.0.0", "proxy IP address")
proxyPort := flag.String("pp", "8006", "proxy port")
name := flag.String("n", "NoName", "Name of this agent")
flag.Parse()
return flags{*isProxy, *listenIP, *listenPort, *proxyIP, *proxyPort, *name}
}
// REST endpoints supported by the proxy are as follows.
// /spa -- for AngularJS GUI files (GET)
// /hb/{heartbeat struct} (POST)
// /images/{image struct} (POST)
func setupRoutes() {
mux = make(map[string]func(http.ResponseWriter, *http.Request))
mux["/spa"] = handlers.HandleSpa
mux["/hb"] = handlers.HandleHeartbeat
mux["/images"] = handlers.HandleImages
}
func sendHeartbeat(f flags) {
url := "http://" + f.proxyIP + ":" + f.proxyPort
fmt.Println("URL:>", url)
hb := map[string]string{"fromIP": f.listenIP, "fromName": f.name}
json, _ := json.Marshal(hb)
var jsonStr = []byte(string(json))
//var hb heartbeat = []byte(`{"fromIP":"123.345.234.111"}, {"fromName":"antti"}`)
//var jsonStr = []byte(`{"title":"Buy cheese and bread for breakfast."}`)
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
req.Header.Set("X-Custom-Header", "myvalue")
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
fmt.Println("response Status:", resp.Status)
fmt.Println("response Headers:", resp.Header)
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println("response Body:", string(body))
}
func startHeartbeat(sec time.Duration, f flags) {
ticker := time.NewTicker(sec * time.Second)
quit := make(chan struct{})
go func() {
for {
select {
case <-ticker.C:
sendHeartbeat(f)
case <-quit:
ticker.Stop()
return
}
}
}()
}
func main() {
flags := getCmdLineArgs()
fmt.Println("Command Line Flags:")
if flags.isProxy {
fmt.Println("[isProxy: true]")
} else {
fmt.Println("[isProxy: false]")
}
fmt.Println("[listenUrl: " + flags.listenIP + "]")
fmt.Println("[listenPort: " + flags.listenPort + "]")
fmt.Println("[proxyUrl: " + flags.proxyIP + "]")
fmt.Println("[proxyPort: " + flags.proxyPort + "]\n")
server := http.Server{
Addr: flags.listenIP + ":" + flags.listenPort,
Handler: &myHandler{},
}
setupRoutes()
fmt.Println("Listening to " + server.Addr)
if !flags.isProxy {
go startHeartbeat(5, flags)
}
server.ListenAndServe()
}
type myHandler struct{}
func (*myHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Println("Request: " + r.URL.String())
if h, ok := mux[r.URL.String()]; ok {
h(w, r)
return
}
io.WriteString(w, "My server: "+r.URL.String())
}