-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ee6aa08
commit df2a722
Showing
7 changed files
with
166 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# The .dockerignore file excludes files from the container build process. | ||
# | ||
# https://docs.docker.com/engine/reference/builder/#dockerignore-file | ||
|
||
# Exclude locally vendored dependencies. | ||
vendor/ | ||
|
||
# Exclude "build-time" ignore files. | ||
.dockerignore | ||
.gcloudignore | ||
|
||
# Exclude git history and configuration. | ||
.gitignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# The .gcloudignore file excludes file from upload to Cloud Build. | ||
# If this file is deleted, gcloud will default to .gitignore. | ||
# | ||
# https://cloud.google.com/cloud-build/docs/speeding-up-builds#gcloudignore | ||
# https://cloud.google.com/sdk/gcloud/reference/topic/gcloudignore | ||
|
||
# Exclude locally vendored dependencies. | ||
vendor/ | ||
|
||
# Exclude git history and configuration. | ||
.git/ | ||
.gitignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# Copyright 2021 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
# Use the offical golang image to create a binary. | ||
# This is based on Debian and sets the GOPATH to /go. | ||
# https://hub.docker.com/_/golang | ||
FROM golang:1.15-buster as builder | ||
|
||
# Create and change to the app directory. | ||
WORKDIR /app | ||
|
||
# Retrieve application dependencies. | ||
# This allows the container build to reuse cached dependencies. | ||
# Expecting to copy go.mod and if present go.sum. | ||
COPY go.* ./ | ||
RUN go mod download | ||
|
||
# Copy local code to the container image. | ||
COPY . ./ | ||
|
||
# Build the binary. | ||
RUN go build -mod=readonly -v -o server | ||
|
||
# Use the official Debian slim image for a lean production container. | ||
# https://hub.docker.com/_/debian | ||
# https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds | ||
FROM debian:buster-slim | ||
RUN set -x && apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y \ | ||
ca-certificates && \ | ||
rm -rf /var/lib/apt/lists/* | ||
|
||
# Copy the binary to the production image from the builder stage. | ||
COPY --from=builder /app/server /app/server | ||
|
||
# Run the web service on container startup. | ||
CMD ["/app/server"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module github.com/GoogleCloudPlatform/golang-samples/run/sigterm-handler | ||
|
||
go 1.13 |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright 2021 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// Sample run-helloworld is a minimal Cloud Run service. | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
) | ||
|
||
func main() { | ||
go signalHandler() | ||
|
||
log.Print("starting server...") | ||
http.HandleFunc("/", handler) | ||
|
||
// Determine port for HTTP service. | ||
port := os.Getenv("PORT") | ||
if port == "" { | ||
port = "8080" | ||
log.Printf("defaulting to port %s", port) | ||
} | ||
|
||
// Start HTTP server. | ||
log.Printf("listening on port %s", port) | ||
if err := http.ListenAndServe(":"+port, nil); err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
// [START cloudrun_sigterm_handler] | ||
func signalHandler() { | ||
signalChan := make(chan os.Signal, 1) | ||
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM) | ||
|
||
sig := <-signalChan | ||
log.Printf("%s signal caught.", sig) | ||
os.Exit(0) | ||
} | ||
|
||
// [END cloudrun_sigterm_handler] | ||
|
||
func handler(w http.ResponseWriter, r *http.Request) { | ||
fmt.Fprint(w, "Hello World!\n") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright 2021 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package main | ||
|
||
import ( | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
func TestHandler(t *testing.T) { | ||
req := httptest.NewRequest("GET", "/", nil) | ||
rr := httptest.NewRecorder() | ||
handler(rr, req) | ||
|
||
if got := rr.Body.String(); got != "Hello World!\n" { | ||
t.Errorf("got %q, want %q", got, "Hello World!\n") | ||
} | ||
} |