5
0
Fork 0

Use env for config instead of flags

master
UnicodingUnicorn 2019-02-18 23:43:11 +08:00
parent 84d9ed7c06
commit 413015945a
6 changed files with 21 additions and 11 deletions

2
.env Normal file
View File

@ -0,0 +1,2 @@
LISTEN=:8080
REDIS=:6379

View File

@ -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"]

View File

@ -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

1
go.mod
View File

@ -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

2
go.sum
View File

@ -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=

14
main.go
View File

@ -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)