From 877effc6adedb115b8f08184a9fccfd8bc9ce507 Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Sat, 22 Jun 2013 10:10:22 -0900 Subject: [PATCH] Add dockerui server files and Dockerfile --- .gitignore | 1 + Dockerfile | 14 ++++++++++ dockerui.go | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++ js/app.js | 5 ++-- 4 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100644 dockerui.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..35f9ef908 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +dockerui diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 000000000..70877a542 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,14 @@ +# Dockerfile for DockerUI + +FROM ubuntu + +MAINTAINER Michael Crosby http://crosbymichael.com + +RUN echo "deb http://archive.ubuntu.com/ubuntu precise main universe" > /etc/apt/sources.list +RUN apt-get update +RUN apt-get upgrade + +ADD . /dockerui + +EXPOSE 9000:9000 + diff --git a/dockerui.go b/dockerui.go new file mode 100644 index 000000000..23bde6a7d --- /dev/null +++ b/dockerui.go @@ -0,0 +1,77 @@ +package main + +import ( + "flag" + "fmt" + "github.com/elazarl/goproxy" + "log" + "net/http" + "os" + "strings" +) + +var ( + endpoint = flag.String("e", "", "Docker d endpoint.") + verbose = flag.Bool("v", false, "Verbose logging.") + port = flag.String("p", "9000", "Port to serve dockerui.") +) + +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 { + 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() + + path := fmt.Sprintf(":%s", *port) + + cwd, err := os.Getwd() + if err != nil { + log.Fatal(err) + } + + handler := createHandler(cwd) + + log.Fatal(http.ListenAndServe(path, handler)) +} diff --git a/js/app.js b/js/app.js index 1da29030d..c71dc83d0 100644 --- a/js/app.js +++ b/js/app.js @@ -11,7 +11,8 @@ angular.module('dockerui', ['dockerui.services', 'dockerui.filters']) $routeProvider.otherwise({redirectTo: '/'}); }]) // This is your docker url that the api will use to make requests - .constant('DOCKER_ENDPOINT', 'http://192.168.1.9') - .constant('DOCKER_PORT', ':4243') + // You need to set this to the api endpoint without the port i.e. http://192.168.1.9 + .constant('DOCKER_ENDPOINT', '/dockerapi') + .constant('DOCKER_PORT', '') // Docker port, leave as an empty string if no port is requred. i.e. 4243 .constant('UI_VERSION', 'v0.2') .constant('DOCKER_API_VERSION', 'v1.2');