diff --git a/sample-functions/DockerHubStats/.gitignore b/sample-functions/DockerHubStats/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..5b8e38158c6e9cce1f61c317110b6240082a9b55 --- /dev/null +++ b/sample-functions/DockerHubStats/.gitignore @@ -0,0 +1 @@ +DockerHubStats diff --git a/sample-functions/DockerHubStats/Dockerfile b/sample-functions/DockerHubStats/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..6ab0b2bc9954a77e99b0e5ec73ae289eafd5bc1b --- /dev/null +++ b/sample-functions/DockerHubStats/Dockerfile @@ -0,0 +1,13 @@ +FROM golang:1.7.3-alpine +MAINTAINER alexellis2@gmail.com +ENTRYPOINT [] + +RUN apk --no-cache add make +WORKDIR /go/src/github.com/alexellis/faas/sample-functions/DockerHubStats +COPY . /go/src/github.com/alexellis/faas/sample-functions/DockerHubStats + +RUN make +COPY fwatchdog /usr/bin/fwatchdog +ENV fprocess "/go/bin/DockerHubStats" + +CMD [ "/usr/bin/fwatchdog"] diff --git a/sample-functions/DockerHubStats/Makefile b/sample-functions/DockerHubStats/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..7e48bf78f81a4443b0296502a36bf111ff3c1c9b --- /dev/null +++ b/sample-functions/DockerHubStats/Makefile @@ -0,0 +1,9 @@ + +.PHONY: install image + +install: + @go install . + +image: + @docker build -t alexellis2/dockerhub-stats . + diff --git a/sample-functions/DockerHubStats/README.md b/sample-functions/DockerHubStats/README.md new file mode 100644 index 0000000000000000000000000000000000000000..a0449f8a845873fbcc8141669fe10e9cd5b8bf2b --- /dev/null +++ b/sample-functions/DockerHubStats/README.md @@ -0,0 +1,11 @@ +Building: + +``` +# docker service rm hubstats ; docker service create --network=functions --name=hubstats alexellis2/dockerhub-stats +``` + +Query the function through the gateway: + +``` +# curl -X POST -d "alexellis2" -v http://localhost:8080/function/hubstats +``` \ No newline at end of file diff --git a/sample-functions/DockerHubStats/main.go b/sample-functions/DockerHubStats/main.go new file mode 100644 index 0000000000000000000000000000000000000000..846c46664020f87671e4de866eaee70fb57f6be6 --- /dev/null +++ b/sample-functions/DockerHubStats/main.go @@ -0,0 +1,53 @@ +package main + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "log" + "net/http" + "os" + "strings" +) + +type dockerHubStatsType struct { + Count int `json:"count"` +} + +func sanitizeInput(input string) string { + parts := strings.Split(input, "\n") + return strings.Trim(parts[0], " ") +} + +func requestHubStats(org string) dockerHubStatsType { + client := http.Client{} + res, err := client.Get("https://hub.docker.com/v2/repositories/" + org) + if err != nil { + log.Fatalln("Unable to reach Docker Hub server.") + } + + body, err := ioutil.ReadAll(res.Body) + if err != nil { + log.Fatalln("Unable to parse response from server.") + } + + dockerHubStats := dockerHubStatsType{} + json.Unmarshal(body, &dockerHubStats) + return dockerHubStats +} + +func main() { + input, err := ioutil.ReadAll(os.Stdin) + if err != nil { + log.Fatal("Unable to read standard input:", err) + } + org := string(input) + if len(input) == 0 { + log.Fatalln("A username or organisation is required.") + } + + org = sanitizeInput(org) + dockerHubStats := requestHubStats(org) + + fmt.Printf("The organisation or user %s has %d repositories on the Docker hub.\n", org, dockerHubStats.Count) +}