diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..fea043d --- /dev/null +++ b/Dockerfile @@ -0,0 +1,4 @@ +FROM alpine:3.2 +ADD listen /listen +WORKDIR / +ENTRYPOINT [ "/listen" ] diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..8b7b4cf --- /dev/null +++ b/Makefile @@ -0,0 +1,14 @@ + +GOPATH:=$(shell go env GOPATH) + +.PHONY: proto test docker + + +build: + go build -o listen + +test: + go test -v ./... -cover + +docker: + docker build . -t listen-web:latest diff --git a/README.md b/README.md new file mode 100644 index 0000000..acfcaf6 --- /dev/null +++ b/README.md @@ -0,0 +1,53 @@ +# Listen Service + +This is the Listen service + +Generated with + +``` +micro new gitlab.com/nkprince007/listen --namespace=go.micro --type=web +``` + +## Getting Started + +- [Configuration](#configuration) +- [Dependencies](#dependencies) +- [Usage](#usage) + +## Configuration + +- FQDN: go.micro.web.listen +- Type: web +- Alias: listen + +## Dependencies + +Micro services depend on service discovery. The default is consul. + +``` +# install consul +brew install consul + +# run consul +consul agent -dev +``` + +## Usage + +A Makefile is included for convenience + +Build the binary + +``` +make build +``` + +Run the service +``` +./listen-web +``` + +Build a docker image +``` +make docker +``` diff --git a/handler/handler.go b/handler/handler.go new file mode 100644 index 0000000..82471c4 --- /dev/null +++ b/handler/handler.go @@ -0,0 +1,29 @@ +package handler + +import ( + "encoding/json" + "net/http" + "time" +) + +// Echo receives whatever was sent and posts it back +func Echo(w http.ResponseWriter, r *http.Request) { + // decode the incoming request as json + var request map[string]interface{} + if err := json.NewDecoder(r.Body).Decode(&request); err != nil { + http.Error(w, err.Error(), 500) + return + } + + response := map[string]interface{}{ + "payload": request, + "processedAt": time.Now().Format(time.RFC1123), + "status": "received", + } + + // encode and write the response as json + if err := json.NewEncoder(w).Encode(response); err != nil { + http.Error(w, err.Error(), 500) + return + } +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..c1908d9 --- /dev/null +++ b/main.go @@ -0,0 +1,28 @@ +package main + +import ( + "github.com/micro/go-log" + "github.com/micro/go-web" + "gitlab.com/nkprince007/listen/handler" +) + +func main() { + // create new web service + service := web.NewService( + web.Name("go.micro.web.listen"), + web.Version("latest"), + ) + + // register call handler + service.HandleFunc("/", handler.Echo) + + // initialise service + if err := service.Init(); err != nil { + log.Fatal(err) + } + + // run service + if err := service.Run(); err != nil { + log.Fatal(err) + } +}