-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
48 lines (40 loc) · 1.22 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
package main
import (
"github.com/Sirupsen/logrus"
"log"
"net"
"net/http"
"net/http/httputil"
"os"
"strings"
)
func main() {
unixSocketPath := "/run/docker/plugins/ipamproxy.sock"
httpReverseProxiedHost := os.Getenv("IPAM_HTTP_PROXY_HOST")
requestLoggingEnabled := strings.ToLower(os.Getenv("REQUEST_LOGGING_ENABLED")) == "true"
logger := logrus.New()
log.SetOutput(logger.Writer())
if httpReverseProxiedHost == "" {
logger.Fatal("KEY=VALUE of IPAM_HTTP_PROXY_HOST=HOST must be be set when driver is installed, or set when installed driver is inactive")
}
director := func(req *http.Request) {
req.URL.Scheme = "http"
req.URL.Host = httpReverseProxiedHost
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if requestLoggingEnabled {
requestDump, err := httputil.DumpRequest(r, true)
if err != nil {
logger.Fatal(err)
}
logger.Info(string(requestDump))
}
proxy := &httputil.ReverseProxy{Director: director}
proxy.ServeHTTP(w, r)
})
unixListener, err := net.Listen("unix", unixSocketPath)
if err != nil {
logger.Fatal("could not bind to " + unixSocketPath + "; make sure it does not already exist, and can be created")
}
logger.Fatal(http.Serve(unixListener, nil))
}