From 56fa4ab05ffb27a4d9eaaedfa9c671654c9d9ba3 Mon Sep 17 00:00:00 2001 From: Joerg Lehmann Date: Sat, 2 Jul 2022 10:45:51 +0200 Subject: [PATCH] make deployment container-ready --- Dockerfile | 13 +++++++++++++ helper.go | 9 +++++++++ main.go | 2 +- metrics.go | 2 +- persistence.go | 2 +- 5 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..ab62972 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,13 @@ +FROM golang:alpine AS builder +WORKDIR /build +ADD go.mod . +COPY . . +RUN go build +FROM alpine +WORKDIR /build +COPY --from=builder /build/wo-bisch-web /build/wo-bisch-web +COPY snippets snippets +COPY templates templates +COPY static static +CMD ["./wo-bisch-web"] +EXPOSE 4000 diff --git a/helper.go b/helper.go index 1a3737f..dc1ce7d 100644 --- a/helper.go +++ b/helper.go @@ -2,6 +2,7 @@ package main import ( "log" + "os" ) // Contains tells whether a contains x. @@ -18,3 +19,11 @@ func Contains(a []string, x string) bool { } return false } + +func getenv(key, fallback string) string { + value := os.Getenv(key) + if len(value) == 0 { + return fallback + } + return value +} diff --git a/main.go b/main.go index b33a0be..c26e293 100644 --- a/main.go +++ b/main.go @@ -136,6 +136,6 @@ func main() { http.HandleFunc("/stripewebhook", stripeWebhookHandler) logit("Starting Web Application...") - http.ListenAndServe("127.0.0.1:4000", nil) + http.ListenAndServe(":4000", nil) logit("Terminating Web Application...") } diff --git a/metrics.go b/metrics.go index c56171c..48b477c 100644 --- a/metrics.go +++ b/metrics.go @@ -112,7 +112,7 @@ func metricsHandler(response http.ResponseWriter, request *http.Request) { mystart = start[0] } - url := "http://localhost:8086/api/v2/query?org=wobischorg" + url := getenv("INFLUX_URL", "http://localhost:8086/api/v2/query?org=wobischorg") data := []byte(fmt.Sprintf(`from(bucket:"wobischbucket") |> range(start: %s, stop: %s) |> filter(fn: (r) => r._measurement == "measurement") diff --git a/persistence.go b/persistence.go index 80ec444..1bc8550 100644 --- a/persistence.go +++ b/persistence.go @@ -26,7 +26,7 @@ func newPool() *redis.Pool { // Dial is an application supplied function for creating and // configuring a connection. Dial: func() (redis.Conn, error) { - c, err := redis.Dial("tcp", ":6379") + c, err := redis.Dial("tcp", getenv("REDIS_CONNECTION_STRING",":6379")) if err != nil { panic(err.Error()) }