From 175354acd3d86d6ac115c19024b01d03f4bab5e5 Mon Sep 17 00:00:00 2001 From: Marcus Noble Date: Fri, 8 May 2020 14:30:50 +0100 Subject: [PATCH] Initial commit --- .gitignore | 18 ++++++++++++++++ Dockerfile | 16 ++++++++++++++ LICENSE | 22 ++++++++++++++++++++ Makefile | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 1 + go.mod | 5 +++++ go.sum | 2 ++ main.go | 21 +++++++++++++++++++ 8 files changed, 146 insertions(+) create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100644 LICENSE create mode 100644 Makefile create mode 100644 README.md create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b588aef --- /dev/null +++ b/.gitignore @@ -0,0 +1,18 @@ +### Go ### +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +/vendor/ +/Godeps/ +qr + +.vscode + diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..0917445 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,16 @@ +FROM golang:alpine AS builder +RUN apk update && apk add --no-cache git && adduser -D -g '' gopher && apk add -U --no-cache ca-certificates +WORKDIR /app/ +ADD go.mod go.sum ./ +RUN go mod download +ADD . . +RUN GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -ldflags="-w -s" -o qr main.go + +FROM scratch +WORKDIR /app/ +COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ +COPY --from=builder /etc/passwd /etc/passwd +COPY --from=builder /app/qr /app/qr +USER gopher +ENTRYPOINT ["/app/qr"] + diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..1284717 --- /dev/null +++ b/LICENSE @@ -0,0 +1,22 @@ +MIT License + +Copyright (c) 2020 Marcus Noble + +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..f410b47 --- /dev/null +++ b/Makefile @@ -0,0 +1,61 @@ +.DEFAULT_GOAL := default + +IMAGE ?= docker.cluster.fun/averagemarcus/qr:latest + +.PHONY: test # Run all tests, linting and format checks +test: lint check-format run-tests + +.PHONY: lint # Perform lint checks against code +lint: + @go vet && golint -set_exit_status ./... + +.PHONY: check-format # Checks code formatting and returns a non-zero exit code if formatting errors found +check-format: + @gofmt -e -l . + +.PHONY: format # Performs automatic format fixes on all code +format: + @gofmt -s -w . + +.PHONY: run-tests # Runs all tests +run-tests: + @go test + +.PHONY: fetch-deps # Fetch all project dependencies +fetch-deps: + @go mod tidy + +.PHONY: build # Build the project +build: lint check-format fetch-deps + @go build -o qr main.go + +.PHONY: docker-build # Build the docker image +docker-build: + @docker build -t $(IMAGE) . + +.PHONY: docker-publish # Push the docker image to the remote registry +docker-publish: + @docker push $(IMAGE) + +.PHONY: run # Run the application +run: + @go run main.go + +.PHONY: ci # Perform CI specific tasks to perform on a pull request +ci: + @echo "⚠️ 'ci' unimplemented" + +.PHONY: release # Release the latest version of the application +release: + @echo "⚠️ 'release' unimplemented" + +.PHONY: help # Show this list of commands +help: + @echo "${REPO_NAME}" + @echo "Usage: make [target]" + @echo "" + @echo "target description" | expand -t20 + @echo "-----------------------------------" + @grep '^.PHONY: .* #' Makefile | sed 's/\.PHONY: \(.*\) # \(.*\)/\1 \2/' | expand -t20 + +default: test build diff --git a/README.md b/README.md new file mode 100644 index 0000000..67b58e7 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# qr diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..7561549 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module qr + +go 1.13 + +require github.com/skip2/go-qrcode v0.0.0-20191027152451-9434209cb086 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..25d3851 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/skip2/go-qrcode v0.0.0-20191027152451-9434209cb086 h1:RYiqpb2ii2Z6J4x0wxK46kvPBbFuZcdhS+CIztmYgZs= +github.com/skip2/go-qrcode v0.0.0-20191027152451-9434209cb086/go.mod h1:PLPIyL7ikehBD1OAjmKKiOEhbvWyHGaNDjquXMcYABo= diff --git a/main.go b/main.go new file mode 100644 index 0000000..440ce54 --- /dev/null +++ b/main.go @@ -0,0 +1,21 @@ +package main + +import ( + "fmt" + qrcode "github.com/skip2/go-qrcode" + "net/http" +) + +func main() { + http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { + website := req.URL.Query().Get("website") + if website != "" { + png, _ := qrcode.Encode(website, qrcode.Medium, 80) + w.Write(png) + return + } + + w.WriteHeader(400) + }) + fmt.Println(http.ListenAndServe(":8080", nil)) +}