From 4635bd4d777d327487a09ddc5caa66d3c5ab5d42 Mon Sep 17 00:00:00 2001 From: Sameer Dev Date: Tue, 23 Apr 2024 17:34:36 +0530 Subject: [PATCH] ID:FCLD-228;DONE:7;HOURS:7; system file gen test in builder --- Dockerfile | 43 +++++++++++++++++++++++++++++++++++++++++++ fc.toml | 15 +++++++++++++++ go.mod | 3 +++ go.sum | 0 main.go | 19 +++++++++++++++++++ 5 files changed, 80 insertions(+) create mode 100644 Dockerfile create mode 100644 fc.toml create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..4d3bb2a --- /dev/null +++ b/Dockerfile @@ -0,0 +1,43 @@ +### SYSTEM GENERATED FILE, MAKE CHANGES AS NEEDED ### + +# Use a specific version as the base image for the builder stage +FROM golang:1.21-alpine AS builder + +# Set /app as the working directory in the builder stage +WORKDIR /app + +### UNCOMMENT AS NEEDED ### +# Copy go.mod and go.sum files into the builder stage +# This allows the dependencies to be installed before copying the rest of the application code +COPY go.sum ./ +COPY go.mod ./ + +### DEFAULT BOOTSTRAP BUILD CODE, RUN LOCALLY TO GENERATE go.mod ### +# If go.mod exists and not empty, install the listed packages +# If it doesn't exist or is empty, print a message and skip the installation +RUN if [ -s go.mod ]; then \ + go mod download; \ + else \ + echo "No go.mod found, continuing with default settings"; \ + go mod init app; \ + fi + +# Copy the rest of the application code into the builder stage +COPY . /app + +# Build the application +RUN go build -v -o app . + +# Start a new stage with a distroless image for smaller image size and improved security +FROM gcr.io/distroless/static-debian11 + +# Copy the /app directory from the builder stage into the current stage +# This includes the application code and the installed packages +COPY --from=builder /app /app + +# Set /app as the working directory in the current stage +WORKDIR /app + +# Set the command to run when the container starts +# This will start the application +CMD [ "./app" ] \ No newline at end of file diff --git a/fc.toml b/fc.toml new file mode 100644 index 0000000..cce773e --- /dev/null +++ b/fc.toml @@ -0,0 +1,15 @@ +app = "golang-dockerfile-app-sample" +region = "asia-south1" +handler = "" + +[build] + dockerfile = "Dockerfile" + ignorefile = ".gitignore" + [build.args] + foo = "bar" + +[env] + FOO = "BAR" + +[http_service] + internal_port = 3000 diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..e04299a --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module golang-buildpack-app-sample + +go 1.21.5 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..e69de29 diff --git a/main.go b/main.go new file mode 100644 index 0000000..cae698d --- /dev/null +++ b/main.go @@ -0,0 +1,19 @@ +package main + +import ( + "fmt" + "net/http" +) + +func handler(w http.ResponseWriter, r *http.Request) { + fmt.Fprintf(w, "Hello World!") +} + +func main() { + // Register the handler function to handle requests to "/" + http.HandleFunc("/", handler) + + // Start the HTTP server on port 3000 + fmt.Println("Example app listening on port 3000!") + http.ListenAndServe(":3000", nil) +}