forked from kevana/ui-for-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dockerui.go
72 lines (60 loc) · 1.56 KB
/
dockerui.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
package main
import (
"flag"
"fmt"
"github.com/elazarl/goproxy"
"log"
"net/http"
"strings"
)
var (
endpoint = flag.String("e", "", "Dockerd endpoint")
verbose = flag.Bool("v", false, "Verbose logging")
port = flag.String("p", "9000", "Port to serve dockerui")
assets = flag.String("a", ".", "Path to the assets")
)
type multiHandler struct {
base http.Handler
proxy *goproxy.ProxyHttpServer
verbose bool
}
func (h *multiHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if h.verbose {
log.Printf("%s: %s\n", r.Method, r.URL.String())
}
if isDockerRequest(r.URL.String()) {
h.proxy.ServeHTTP(w, r)
} else {
h.base.ServeHTTP(w, r)
}
}
func isDockerRequest(url string) bool {
log.Printf("Got request: %s", url)
return strings.Contains(url, "dockerapi/")
}
func createHandler(dir string) http.Handler {
fileHandler := http.FileServer(http.Dir(dir))
proxy := goproxy.NewProxyHttpServer()
proxy.Verbose = *verbose
proxy.OnRequest().DoFunc(func(r *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) {
c := http.Client{}
path := strings.Replace(r.URL.RequestURI(), "dockerapi/", "", -1)
n, err := http.NewRequest(r.Method, *endpoint+path, r.Body)
n.Header = r.Header
if err != nil {
log.Fatal(err)
}
resp, err := c.Do(n)
if err != nil {
log.Fatal(err)
}
return r, resp
})
return &multiHandler{base: fileHandler, proxy: proxy, verbose: *verbose}
}
func main() {
flag.Parse()
handler := createHandler(*assets)
path := fmt.Sprintf(":%s", *port)
log.Fatal(http.ListenAndServe(path, handler))
}