helloworld: go clarity tweaks and go modules for shell (#1908)

* helloworld: go clarity tweaks and go modules for shell

* helloworld: go and shell README and comment alignment
This commit is contained in:
Adam Ross 2019-10-21 15:15:25 -07:00 committed by Knative Prow Robot
parent e2ee73d197
commit 81bc837df5
8 changed files with 95 additions and 56 deletions

View File

@ -6,8 +6,8 @@ FROM golang:1.13 as builder
# Create and change to the app directory. # Create and change to the app directory.
WORKDIR /app WORKDIR /app
# Retrieve application dependencies. # Retrieve application dependencies using go modules.
# This allows the container build to reuse cached dependencies. # Allows container builds to reuse downloaded dependencies.
COPY go.* ./ COPY go.* ./
RUN go mod download RUN go mod download
@ -15,6 +15,7 @@ RUN go mod download
COPY . ./ COPY . ./
# Build the binary. # Build the binary.
# -mod=readonly ensures immutable go.mod and go.sum in container builds.
RUN CGO_ENABLED=0 GOOS=linux go build -mod=readonly -v -o server RUN CGO_ENABLED=0 GOOS=linux go build -mod=readonly -v -o server
# Use the official Alpine image for a lean production container. # Use the official Alpine image for a lean production container.

View File

@ -42,7 +42,7 @@ cd knative-docs/docs/serving/samples/hello-world/helloworld-go
) )
func handler(w http.ResponseWriter, r *http.Request) { func handler(w http.ResponseWriter, r *http.Request) {
log.Print("Hello world received a request.") log.Print("helloworld: received a request")
target := os.Getenv("TARGET") target := os.Getenv("TARGET")
if target == "" { if target == "" {
target = "World" target = "World"
@ -51,7 +51,7 @@ cd knative-docs/docs/serving/samples/hello-world/helloworld-go
} }
func main() { func main() {
log.Print("Hello world sample started.") log.Print("helloworld: starting server...")
http.HandleFunc("/", handler) http.HandleFunc("/", handler)
@ -60,6 +60,7 @@ cd knative-docs/docs/serving/samples/hello-world/helloworld-go
port = "8080" port = "8080"
} }
log.Printf("helloworld: listening on port %s", port)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil)) log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
} }
``` ```
@ -77,8 +78,8 @@ cd knative-docs/docs/serving/samples/hello-world/helloworld-go
# Create and change to the app directory. # Create and change to the app directory.
WORKDIR /app WORKDIR /app
# Retrieve application dependencies. # Retrieve application dependencies using go modules.
# This allows the container build to reuse cached dependencies. # Allows container builds to reuse downloaded dependencies.
COPY go.* ./ COPY go.* ./
RUN go mod download RUN go mod download
@ -86,6 +87,7 @@ cd knative-docs/docs/serving/samples/hello-world/helloworld-go
COPY . ./ COPY . ./
# Build the binary. # Build the binary.
# -mod=readonly ensures immutable go.mod and go.sum in container builds.
RUN CGO_ENABLED=0 GOOS=linux go build -mod=readonly -v -o server RUN CGO_ENABLED=0 GOOS=linux go build -mod=readonly -v -o server
# Use the official Alpine image for a lean production container. # Use the official Alpine image for a lean production container.

View File

@ -8,7 +8,7 @@ import (
) )
func handler(w http.ResponseWriter, r *http.Request) { func handler(w http.ResponseWriter, r *http.Request) {
log.Print("Hello world received a request.") log.Print("helloworld: received a request")
target := os.Getenv("TARGET") target := os.Getenv("TARGET")
if target == "" { if target == "" {
target = "World" target = "World"
@ -17,7 +17,7 @@ func handler(w http.ResponseWriter, r *http.Request) {
} }
func main() { func main() {
log.Print("Hello world sample started.") log.Print("helloworld: starting server...")
http.HandleFunc("/", handler) http.HandleFunc("/", handler)
@ -26,5 +26,6 @@ func main() {
port = "8080" port = "8080"
} }
log.Printf("helloworld: listening on port %s", port)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil)) log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
} }

View File

@ -1,26 +1,32 @@
# Use the offical Golang image to create a build artifact. # Use the official Golang image to create a build artifact.
# This is based on Debian and sets the GOPATH to /go. # This is based on Debian and sets the GOPATH to /go.
# https://hub.docker.com/_/golang # https://hub.docker.com/_/golang
FROM golang:1.12 as builder FROM golang:1.13 as builder
# Create and change to the app directory.
WORKDIR /app
# Retrieve application dependencies using go modules.
# Allows container builds to reuse downloaded dependencies.
COPY go.* ./
RUN go mod download
# Copy local code to the container image. # Copy local code to the container image.
WORKDIR /go/src/github.com/knative/docs/helloworld-shell COPY invoke.go ./
COPY invoke.go .
# Build the command inside the container. # Build the binary.
# (You may fetch or manage dependencies here, # -mod=readonly ensures immutable go.mod and go.sum in container builds.
# either manually or with a tool like "godep".) RUN CGO_ENABLED=0 GOOS=linux go build -mod=readonly -v -o server
RUN CGO_ENABLED=0 GOOS=linux go build -v -o invoke
# The official Alpine base image # Use the official Alpine image for a lean production container.
# https://hub.docker.com/_/alpine # https://hub.docker.com/_/alpine
# Use a Docker multi-stage build to create a lean production image.
# https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds # https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds
FROM alpine:3.10 FROM alpine:3
RUN apk add --no-cache ca-certificates
# Copy Go binary # Copy the binary to the production image from the builder stage.
COPY --from=builder /go/src/github.com/knative/docs/helloworld-shell/invoke /invoke COPY --from=builder /app/server /server
COPY script.sh . COPY script.sh ./
# Run the web service on container startup. # Run the web service on container startup.
CMD ["/invoke"] CMD ["/server"]

View File

@ -42,64 +42,75 @@ cd knative-docs/docs/serving/samples/hello-world/helloworld-shell
package main package main
import ( import (
"fmt" "fmt"
"log" "log"
"net/http" "net/http"
"os" "os"
"os/exec" "os/exec"
) )
func handler(w http.ResponseWriter, r *http.Request) { func handler(w http.ResponseWriter, r *http.Request) {
cmd := exec.CommandContext(r.Context(), "/bin/sh", "script.sh") log.Print("helloworld: received a request")
cmd.Stderr = os.Stderr
out, err := cmd.Output() cmd := exec.CommandContext(r.Context(), "/bin/sh", "script.sh")
if err != nil { cmd.Stderr = os.Stderr
w.WriteHeader(500) out, err := cmd.Output()
} if err != nil {
w.Write(out) w.WriteHeader(500)
}
w.Write(out)
} }
func main() { func main() {
http.HandleFunc("/", handler) log.Print("helloworld: starting server...")
port := os.Getenv("PORT") http.HandleFunc("/", handler)
if port == "" {
port = "8080"
}
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil)) port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
log.Printf("helloworld: listening on %s", port)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
} }
``` ```
1. Create a new file named `Dockerfile` and copy the code block below into it. 1. Create a new file named `Dockerfile` and copy the code block below into it.
```docker ```docker
# Use the offical Golang image to create a build artifact. # Use the official Golang image to create a build artifact.
# This is based on Debian and sets the GOPATH to /go. # This is based on Debian and sets the GOPATH to /go.
# https://hub.docker.com/_/golang # https://hub.docker.com/_/golang
FROM golang:1.12 as builder FROM golang:1.13 as builder
# Create and change to the app directory.
WORKDIR /app
# Retrieve application dependencies using go modules.
# Allows container builds to reuse downloaded dependencies.
COPY go.* ./
RUN go mod download
# Copy local code to the container image. # Copy local code to the container image.
WORKDIR /go/src/github.com/knative/docs/helloworld-shell COPY invoke.go ./
COPY invoke.go .
# Build the command inside the container. # Build the binary.
# (You may fetch or manage dependencies here, # -mod=readonly ensures immutable go.mod and go.sum in container builds.
# either manually or with a tool like "godep".) RUN CGO_ENABLED=0 GOOS=linux go build -mod=readonly -v -o server
RUN CGO_ENABLED=0 GOOS=linux go build -v -o invoke
# The official Alpine base image # Use the official Alpine image for a lean production container.
# https://hub.docker.com/_/alpine # https://hub.docker.com/_/alpine
# Use a Docker multi-stage build to create a lean production image.
# https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds # https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds
FROM alpine:3.10 FROM alpine:3
RUN apk add --no-cache ca-certificates
# Copy Go binary # Copy the binary to the production image from the builder stage.
COPY --from=builder /go/src/github.com/knative/docs/helloworld-shell/invoke /invoke COPY --from=builder /app/server /server
COPY script.sh . COPY script.sh ./
# Run the web service on container startup. # Run the web service on container startup.
CMD ["/invoke"] CMD ["/server"]
``` ```
1. Create a new file, `service.yaml` and copy the following service definition 1. Create a new file, `service.yaml` and copy the following service definition
@ -122,6 +133,13 @@ cd knative-docs/docs/serving/samples/hello-world/helloworld-shell
value: "Shell Sample v1" value: "Shell Sample v1"
``` ```
1. Use the go tool to create a
[`go.mod`](https://github.com/golang/go/wiki/Modules#gomod) manifest.
```shell
go mod init github.com/knative/docs/docs/serving/samples/hello-world/helloworld-shell
```
## Building and deploying the sample ## Building and deploying the sample
Once you have recreated the sample code files (or used the files in the sample Once you have recreated the sample code files (or used the files in the sample

View File

@ -0,0 +1,3 @@
module github.com/knative/docs/docs/serving/samples/hello-world/helloworld-shell
go 1.13

View File

@ -9,6 +9,8 @@ import (
) )
func handler(w http.ResponseWriter, r *http.Request) { func handler(w http.ResponseWriter, r *http.Request) {
log.Print("helloworld: received a request")
cmd := exec.CommandContext(r.Context(), "/bin/sh", "script.sh") cmd := exec.CommandContext(r.Context(), "/bin/sh", "script.sh")
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
out, err := cmd.Output() out, err := cmd.Output()
@ -19,6 +21,8 @@ func handler(w http.ResponseWriter, r *http.Request) {
} }
func main() { func main() {
log.Print("helloworld: starting server...")
http.HandleFunc("/", handler) http.HandleFunc("/", handler)
port := os.Getenv("PORT") port := os.Getenv("PORT")
@ -26,5 +30,6 @@ func main() {
port = "8080" port = "8080"
} }
log.Printf("helloworld: listening on %s", port)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil)) log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
} }

View File

@ -58,6 +58,9 @@ languages:
- "Dockerfile" - "Dockerfile"
- language: "shell" - language: "shell"
expectedOutput: "Hello Shell Sample v1!" expectedOutput: "Hello Shell Sample v1!"
preCommands:
- exec: "cp"
args: "../../docs/serving/samples/hello-world/helloworld-shell/go.mod helloworld-shell_tmp/go.mod"
copies: copies:
- "script.sh" - "script.sh"
- "invoke.go" - "invoke.go"