diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5657f6e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +vendor \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..4917698 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,20 @@ +FROM golang:1.9rc2-alpine as BUILDER +MAINTAINER john@johnmccabe.net + +RUN apk --no-cache add make +WORKDIR /go/src/github.com/faas-and-furious/qrcode +COPY . /go/src/github.com/faas-and-furious/qrcode + +RUN make + +FROM alpine + +COPY --from=builder /go/bin/qrcode /usr/bin +ADD https://github.com/alexellis/faas/releases/download/0.6.0/fwatchdog /usr/bin +RUN chmod +x /usr/bin/fwatchdog +COPY --from=builder /go/bin/qrcode /usr/bin +RUN chmod +x /usr/bin/qrcode + +ENV fprocess "/usr/bin/qrcode" + +CMD [ "/usr/bin/fwatchdog"] \ No newline at end of file diff --git a/Gopkg.lock b/Gopkg.lock new file mode 100644 index 0000000..49a49f7 --- /dev/null +++ b/Gopkg.lock @@ -0,0 +1,15 @@ +# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'. + + +[[projects]] + branch = "master" + name = "github.com/skip2/go-qrcode" + packages = [".","bitset","reedsolomon"] + revision = "695fc75a09738f9be56aaa3aecfd7dac28e66b63" + +[solve-meta] + analyzer-name = "dep" + analyzer-version = 1 + inputs-digest = "9094a24c5d5f0ca0993e22a6e3af8cc6a50b6d87628ae6f69f433da34c8721ae" + solver-name = "gps-cdcl" + solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml new file mode 100644 index 0000000..5e7dea5 --- /dev/null +++ b/Gopkg.toml @@ -0,0 +1,26 @@ + +# Gopkg.toml example +# +# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md +# for detailed Gopkg.toml documentation. +# +# required = ["github.com/user/thing/cmd/thing"] +# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"] +# +# [[constraint]] +# name = "github.com/user/project" +# version = "1.0.0" +# +# [[constraint]] +# name = "github.com/user/project2" +# branch = "dev" +# source = "github.com/myfork/project2" +# +# [[override]] +# name = "github.com/x/y" +# version = "2.4.0" + + +[[constraint]] + branch = "master" + name = "github.com/skip2/go-qrcode" diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..8864d4a --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2017 + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..e952b61 --- /dev/null +++ b/Makefile @@ -0,0 +1,14 @@ +.PHONY: install image dependencies + +build: dependencies + @go install . + +image: + @docker build -t faasandfurious/qrcode . + +dependencies: + @apk update + @apk add git + @rm -rf /var/cache/apk/* + @go get -u github.com/golang/dep/cmd/dep + @dep ensure \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..fa58b95 --- /dev/null +++ b/README.md @@ -0,0 +1,34 @@ +# FaaS qrcode + +[![Go Report Card](https://goreportcard.com/badge/github.com/faas-and-furious/qrcode)](https://goreportcard.com/report/github.com/faas-and-furious/qrcode) [![](https://images.microbadger.com/badges/image/faasandfurious/qrcode.svg)](https://microbadger.com/images/faasandfurious/qrcode "Get your own image badge on microbadger.com") + +This repo contains an example [FaaS](https://github.com/alexellis/faas) function which uses the [skip2/go-qrcode](https://github.com/skip2/go-qrcode) Go library to generate a QR Code for a string. + +## Deploying the Function + +Make sure you have deployed a FaaS stack to your cluster using the instructions on the [FaaS repo](https://github.com/alexellis/faas). + +### Use the CLI (`faas-cli`) + +**Get the CLI** + +The [faas-cli](https://github.com/alexellis/faas-cli/) can be installed via `brew install faas-cli` or `curl -sSL https://get.openfaas.com | sudo sh`. + +Now deploy the function as follows: + +``` +# faas-cli -action deploy -image=faasandfurious/qrcode -name=qrcode -fprocess="/usr/bin/qrcode" +200 OK +URL: http://localhost:8080/function/qrcode +``` + +### Testing the Function +Now that the function is running in your FaaS environment you can test it from the command line by running: + +``` +$ curl localhost:8080/function/qrcode --data "https://github.com/alexellis/faas" > qrcode.png +``` + +![](images/qrcode.png) + +You can check the QR code works using your phone, or an online QR Code decoder ([ZXing Decoder Online](https://zxing.org/w/decode.jspx) for example) diff --git a/function.go b/function.go new file mode 100644 index 0000000..65a5102 --- /dev/null +++ b/function.go @@ -0,0 +1,22 @@ +package main + +import ( + "encoding/binary" + "io/ioutil" + "log" + "os" + + qrcode "github.com/skip2/go-qrcode" +) + +func main() { + input, err := ioutil.ReadAll(os.Stdin) + if err != nil { + log.Fatalf("Unable to read standard input: %s", err.Error()) + } + png, err := qrcode.Encode(string(input), qrcode.Medium, 256) + if err != nil { + log.Fatalf("Unable to read standard input: %s", err.Error()) + } + binary.Write(os.Stdout, binary.LittleEndian, png) +} diff --git a/images/qrcode.png b/images/qrcode.png new file mode 100644 index 0000000..66a7ce0 Binary files /dev/null and b/images/qrcode.png differ