From 734c8a3dc6b725ba58a5a31de865aefcdd6cc4a7 Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 22 Dec 2016 16:27:30 +0000 Subject: [PATCH] gateway 0.01 --- gateway/.gitignore | 1 + gateway/Dockerfile | 15 +++++++++++++++ gateway/server.go | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 gateway/.gitignore create mode 100644 gateway/Dockerfile create mode 100644 gateway/server.go diff --git a/gateway/.gitignore b/gateway/.gitignore new file mode 100644 index 00000000..ad230ccf --- /dev/null +++ b/gateway/.gitignore @@ -0,0 +1 @@ +gateway diff --git a/gateway/Dockerfile b/gateway/Dockerfile new file mode 100644 index 00000000..8476e7e3 --- /dev/null +++ b/gateway/Dockerfile @@ -0,0 +1,15 @@ +from golang:1.7.3 + +RUN go get -d github.com/docker/docker/api/types \ + && go get -d github.com/docker/docker/api/types/filters \ + && go get -d github.com/docker/docker/api/types/swarm \ + && go get -d github.com/docker/docker/client + +WORKDIR /go/src/app/ +RUN go get github.com/gorilla/mux +COPY server.go . +RUN go build +EXPOSE 8080 + +CMD ["./app"] + diff --git a/gateway/server.go b/gateway/server.go new file mode 100644 index 00000000..cb25d690 --- /dev/null +++ b/gateway/server.go @@ -0,0 +1,40 @@ +package main + +import ( + "bytes" + "log" + "net/http" + + "io/ioutil" + + "strconv" + + "github.com/gorilla/mux" +) + +func proxy(w http.ResponseWriter, r *http.Request) { + if r.Method == "POST" { + log.Println(r.Header) + header := r.Header["X-Function"] + log.Println(header) + if header[0] == "catservice" { + // client := http.Client{Timeout: time.Second * 2} + requestBody, _ := ioutil.ReadAll(r.Body) + buf := bytes.NewBuffer(requestBody) + + response, err := http.Post("http://"+header[0]+":"+strconv.Itoa(8080)+"/", "text/plain", buf) + if err != nil { + log.Fatalln(err) + } + responseBody, _ := ioutil.ReadAll(response.Body) + w.Write(responseBody) + + } + } +} + +func main() { + r := mux.NewRouter() + r.HandleFunc("/", proxy) + log.Fatal(http.ListenAndServe(":8080", r)) +}