diff --git a/.env b/.env new file mode 100644 index 0000000..66bedc0 --- /dev/null +++ b/.env @@ -0,0 +1,2 @@ +LISTEN=:8080 +REDIS=:6379 diff --git a/Dockerfile b/Dockerfile index c34c42f..c0e43c0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,12 +3,13 @@ FROM golang:1.11-rc-alpine as build RUN apk add --no-cache git=2.18.1-r0 WORKDIR /src -COPY go.mod go.sum *.go ./ +COPY go.mod go.sum .env *.go ./ RUN go get -d -v ./... RUN CGO_ENABLED=0 go build -ldflags "-s -w" FROM scratch COPY --from=build /src/heartbeat /heartbeat +COPY --from=build /src/.env /.env ENTRYPOINT ["/heartbeat"] diff --git a/README.md b/README.md index 00abfdf..ad05118 100644 --- a/README.md +++ b/README.md @@ -2,14 +2,14 @@ Beep backend records and makes available the last seen times of users. -## Flags +## Environment variables -Flags are supplied to the compiled go program in the form ```-flag=stuff```. +Supply environment variables by either exporting them or editing ```.env```. -| Flag | Description | Default | +| ENV | Description | Default | | ---- | ----------- | ------- | -| listen | Port number to listen on | 8080 | -| redis | Host and port of redis | :6379 | +| LISTEN | Host and port number to listen on | :8080 | +| REDIS | Host and port of redis | :6379 | ## API diff --git a/go.mod b/go.mod index 9680834..57f149c 100644 --- a/go.mod +++ b/go.mod @@ -2,6 +2,7 @@ module heartbeat require ( github.com/go-redis/redis v6.15.1+incompatible + github.com/joho/godotenv v1.3.0 github.com/julienschmidt/httprouter v1.2.0 github.com/onsi/ginkgo v1.7.0 // indirect github.com/onsi/gomega v1.4.3 // indirect diff --git a/go.sum b/go.sum index 8abf657..1870c2e 100644 --- a/go.sum +++ b/go.sum @@ -6,6 +6,8 @@ github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= +github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc= +github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg= github.com/julienschmidt/httprouter v1.2.0 h1:TDTW5Yz1mjftljbcKqRcrYhd4XeOoI98t+9HbQbYf7g= github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= diff --git a/main.go b/main.go index 0ad68e4..08c9b5a 100644 --- a/main.go +++ b/main.go @@ -1,13 +1,14 @@ package main import ( - "flag" "fmt" "log" "net/http" + "os" "strconv" "time" + "github.com/joho/godotenv" "github.com/julienschmidt/httprouter" "github.com/go-redis/redis" ) @@ -24,10 +25,13 @@ var connections map[RawClient][]chan []byte var redisClient *redis.Client func main() { - // Parse flags - flag.StringVar(&listen, "listen", ":8080", "host and port to listen on") - flag.StringVar(&redisHost, "redis", ":6379", "host and port of redis") - flag.Parse() + // Load .env + err := godotenv.Load() + if err != nil { + log.Fatal("Error loading .env file") + } + listen = os.Getenv("LISTEN") + redisHost = os.Getenv("REDIS") connections = make(map[RawClient][]chan []byte)