From d76f21d508b21ab48a3a96b19b789ff16cec15f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torma=20Krist=C3=B3f?= Date: Sun, 6 Oct 2019 23:56:19 +0200 Subject: [PATCH] isprime --- .travis.yml | 3 +++ isprime/Dockerfile | 30 ++++++++++++++++++++++++++++++ isprime/go.mod | 1 + isprime/isprime.go | 39 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 73 insertions(+) create mode 100644 isprime/Dockerfile create mode 100644 isprime/go.mod create mode 100644 isprime/isprime.go diff --git a/.travis.yml b/.travis.yml index c9e4e89..0da2fb2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,3 +9,6 @@ script: - cd hello-world - docker build -t=docker.pkg.github.com/tormachris/knative-report-functions/hello-world . - docker push docker.pkg.github.com/tormachris/knative-report-functions/hello-world + - cd ../isprime + - docker build -t=docker.pkg.github.com/tormachris/knative-report-functions/isprime . + - docker push docker.pkg.github.com/tormachris/knative-report-functions/isprime diff --git a/isprime/Dockerfile b/isprime/Dockerfile new file mode 100644 index 0000000..f06c711 --- /dev/null +++ b/isprime/Dockerfile @@ -0,0 +1,30 @@ +# Use the offical Golang image to create a build artifact. +# This is based on Debian and sets the GOPATH to /go. +# https://hub.docker.com/_/golang +FROM golang:1.13 as builder + +# Create and change to the app directory. +WORKDIR /app + +# Retrieve application dependencies. +# This allows the container build to reuse cached dependencies. +COPY go.* ./ +RUN go mod download + +# Copy local code to the container image. +COPY . ./ + +# Build the binary. +RUN CGO_ENABLED=0 GOOS=linux go build -v -o server + +# Use the official Alpine image for a lean production container. +# https://hub.docker.com/_/alpine +# https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds +FROM alpine:3 +RUN apk add --no-cache ca-certificates + +# Copy the binary to the production image from the builder stage. +COPY --from=builder /app/server /server + +# Run the web service on container startup. +CMD ["/server"] diff --git a/isprime/go.mod b/isprime/go.mod new file mode 100644 index 0000000..16c6805 --- /dev/null +++ b/isprime/go.mod @@ -0,0 +1 @@ +module github.com/tormachris/knative-report-functions/isprime diff --git a/isprime/isprime.go b/isprime/isprime.go new file mode 100644 index 0000000..12cd0b2 --- /dev/null +++ b/isprime/isprime.go @@ -0,0 +1,39 @@ +package main + +import ( + "fmt" + "log" + "net/http" + "os" +) + +func handler(w http.ResponseWriter, r *http.Request) { + target := os.Getenv("TARGET") + num, err := strconv.Atoi(target) + if err != nil { + return "", fmt.Errorf("Failed to parse %s as int! %v", target, err) + } + logrus.Infof("Checking if %s is prime", target) + if num <= 1 { + return fmt.Fprintf(w,"%d is not prime", num), nil + } + for i := 2; i <= int(math.Floor(float64(num)/2)); i++ { + if num%i == 0 { + return fmt.Fprintf(w,"%d is not prime", num), nil + } + } + return fmt.Fprintf(w,"%d is prime", num), nil +} + +func main() { + log.Print("Hello world sample started.") + + http.HandleFunc("/", handler) + + port := os.Getenv("PORT") + if port == "" { + port = "8080" + } + + log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil)) +}