Skip to content

Commit

Permalink
feat: Add sigterm handler sample
Browse files Browse the repository at this point in the history
  • Loading branch information
averikitsch committed Jan 19, 2021
1 parent ee6aa08 commit df2a722
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 0 deletions.
13 changes: 13 additions & 0 deletions run/sigterm-handler/.dockerignore
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
12 changes: 12 additions & 0 deletions run/sigterm-handler/.gcloudignore
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
47 changes: 47 additions & 0 deletions run/sigterm-handler/Dockerfile
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"]
3 changes: 3 additions & 0 deletions run/sigterm-handler/go.mod
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 added run/sigterm-handler/go.sum
Empty file.
61 changes: 61 additions & 0 deletions run/sigterm-handler/main.go
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")
}
30 changes: 30 additions & 0 deletions run/sigterm-handler/main_test.go
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")
}
}

0 comments on commit df2a722

Please sign in to comment.