Add shout proxy to example app.

This commit is contained in:
Tom Wilkie
2015-06-05 14:56:31 +00:00
parent bbc384a3d7
commit 03bdff8694
5 changed files with 76 additions and 4 deletions

2
.gitignore vendored
View File

@@ -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

3
experimental/example/.gitignore vendored Normal file
View File

@@ -0,0 +1,3 @@
qotd/qotd
goapp/app
shout/shout

View File

@@ -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

View File

@@ -0,0 +1,5 @@
FROM gliderlabs/alpine
MAINTAINER Weaveworks Inc <help@weave.works>
WORKDIR /home/weave
ADD shout /home/weave/
ENTRYPOINT ["/home/weave/shout"]

View File

@@ -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)
}