From 03bdff8694f00674f4d0b0b3aa80590a08f6b12c Mon Sep 17 00:00:00 2001 From: Tom Wilkie Date: Fri, 5 Jun 2015 14:56:31 +0000 Subject: [PATCH] Add shout proxy to example app. --- .gitignore | 2 - experimental/example/.gitignore | 3 ++ experimental/example/Makefile | 8 +++- experimental/example/shout/Dockerfile | 5 +++ experimental/example/shout/shout.go | 62 +++++++++++++++++++++++++++ 5 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 experimental/example/.gitignore create mode 100644 experimental/example/shout/Dockerfile create mode 100644 experimental/example/shout/shout.go diff --git a/.gitignore b/.gitignore index d9e1c3de5..e14a70224 100644 --- a/.gitignore +++ b/.gitignore @@ -44,8 +44,6 @@ experimental/genreport/genreport experimental/graphviz/graphviz experimental/oneshot/oneshot experimental/_integration/_integration -experimental/example/qotd/qotd -experimental/example/goapp/app *sublime-project *sublime-workspace *npm-debug.log diff --git a/experimental/example/.gitignore b/experimental/example/.gitignore new file mode 100644 index 000000000..4eba73260 --- /dev/null +++ b/experimental/example/.gitignore @@ -0,0 +1,3 @@ +qotd/qotd +goapp/app +shout/shout diff --git a/experimental/example/Makefile b/experimental/example/Makefile index 7b3a69f39..2d453335e 100644 --- a/experimental/example/Makefile +++ b/experimental/example/Makefile @@ -4,13 +4,17 @@ CFLAGS=-g -lpthread %.o: %.c $(CC) -c -o $@ $< $(CFLAGS) -all: qotd/qotd goapp/app +all: qotd/qotd goapp/app shout/shout qotd/qotd: qotd/qotd.o gcc -o $@ $< $(CFLAGS) goapp/app: goapp/app.go - go build -ldflags "-extldflags \"-static\"" -tags netgo -o goapp/app ./goapp +shout/shout: shout/shout.go + +shout/shout goapp/app: + go get -tags netgo ./$(@D) + go build -ldflags "-extldflags \"-static\"" -tags netgo -o $@ ./$(@D) clean: rm -f qotd/*.o qotd/qotd goapp/app diff --git a/experimental/example/shout/Dockerfile b/experimental/example/shout/Dockerfile new file mode 100644 index 000000000..1b9be7638 --- /dev/null +++ b/experimental/example/shout/Dockerfile @@ -0,0 +1,5 @@ +FROM gliderlabs/alpine +MAINTAINER Weaveworks Inc +WORKDIR /home/weave +ADD shout /home/weave/ +ENTRYPOINT ["/home/weave/shout"] diff --git a/experimental/example/shout/shout.go b/experimental/example/shout/shout.go new file mode 100644 index 000000000..5014b5ebe --- /dev/null +++ b/experimental/example/shout/shout.go @@ -0,0 +1,62 @@ +package main + +import ( + "encoding/json" + "flag" + "fmt" + "log" + "net/http" + "os" + "os/signal" + "syscall" + + SHOUTCLOUD "github.com/richo/GOSHOUT" +) + +type requestResponse struct { + Text string `json:"text"` +} + +func main() { + var ( + addr = flag.String("addr", ":8080", "HTTP listen address") + ) + flag.Parse() + + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + var req requestResponse + err := json.NewDecoder(r.Body).Decode(&req) + if err != nil { + w.WriteHeader(http.StatusTeapot) + return + } + + req.Text, err = SHOUTCLOUD.UPCASE(req.Text) + if err != nil { + w.WriteHeader(http.StatusTeapot) + return + } + + w.WriteHeader(http.StatusOK) + json.NewEncoder(w).Encode(&req) + }) + + errc := make(chan error) + + go func() { + log.Printf("listening on %s", *addr) + errc <- http.ListenAndServe(*addr, nil) + }() + + go func() { + errc <- interrupt() + }() + + log.Printf("%v", <-errc) +} + +func interrupt() error { + c := make(chan os.Signal) + signal.Notify(c, syscall.SIGINT, syscall.SIGTERM) + return fmt.Errorf("%s", <-c) +}