isprime
This commit is contained in:
		@@ -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
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										30
									
								
								isprime/Dockerfile
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								isprime/Dockerfile
									
									
									
									
									
										Normal file
									
								
							@@ -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"]
 | 
			
		||||
							
								
								
									
										1
									
								
								isprime/go.mod
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								isprime/go.mod
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1 @@
 | 
			
		||||
module github.com/tormachris/knative-report-functions/isprime
 | 
			
		||||
							
								
								
									
										39
									
								
								isprime/isprime.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								isprime/isprime.go
									
									
									
									
									
										Normal file
									
								
							@@ -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))
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user