From 140433f7205b28a549ffea1bb756fb91c72db223 Mon Sep 17 00:00:00 2001 From: UnicodingUnicorn <7555ic@gmail.com> Date: Mon, 18 Feb 2019 23:43:49 +0800 Subject: [PATCH] Use env for config instead of flags --- .env | 2 ++ Dockerfile | 3 ++- README.md | 9 +++++++++ go.mod | 1 + go.sum | 4 ++-- main.go | 15 +++++++++------ 6 files changed, 25 insertions(+), 9 deletions(-) create mode 100644 .env diff --git a/.env b/.env new file mode 100644 index 0000000..4715d14 --- /dev/null +++ b/.env @@ -0,0 +1,2 @@ +DBPATH=/tmp/badger +NATS=nats://localhost:4222 diff --git a/Dockerfile b/Dockerfile index a4a4273..e09c289 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,7 +3,7 @@ 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" RUN mkdir -p /tmp/badger @@ -12,5 +12,6 @@ FROM scratch COPY --from=build /src/store /store COPY --from=build /tmp/badger /tmp/badger +COPY --from=build /src/.env /.env ENTRYPOINT ["/store"] diff --git a/README.md b/README.md index da1ab5f..d325d7a 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,15 @@ Single Badger store to serve bite, transcription and any others. Is kinda bite-centric, so required values revolve around a Bite. Transacts through NATS. +## Environment Variables + +Supply environment variables by either exporting them or editing ```.env```. + +| ENV | Description | Default | +| ---- | ----------- | ------- | +| DBPATH | Path to store badger files in. Please make sure it exists. | /tmp/badger | +| NATS | Host and port of nats | nats://localhost:4222 | + ## Key format Takes in three variables: ```type```, ```key``` and ```start```. Type is the type of data to be inserted, e.g. ```bite```, ```bite_user``` or ```transcription```. Key could be some secret passphrase declaring you the Raj of British India for all I know. Start is the Epoch timestamp of the start of the Bite. diff --git a/go.mod b/go.mod index 437b3e1..8e9fba2 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ require ( github.com/dgraph-io/badger v1.5.4 github.com/dgryski/go-farm v0.0.0-20190104051053-3adb47b1fb0f // indirect github.com/golang/protobuf v1.2.1-0.20190205222052-c823c79ea157 + github.com/joho/godotenv v1.3.0 github.com/nats-io/gnatsd v1.4.1 // indirect github.com/nats-io/go-nats v1.7.0 github.com/nats-io/nkeys v0.0.2 // indirect diff --git a/go.sum b/go.sum index 9201362..5f943d4 100644 --- a/go.sum +++ b/go.sum @@ -6,10 +6,10 @@ github.com/dgraph-io/badger v1.5.4 h1:gVTrpUTbbr/T24uvoCaqY2KSHfNLVGm0w+hbee2HMe github.com/dgraph-io/badger v1.5.4/go.mod h1:VZxzAIRPHRVNRKRo6AXrX9BJegn6il06VMTZVJYCIjQ= github.com/dgryski/go-farm v0.0.0-20190104051053-3adb47b1fb0f h1:dDxpBYafY/GYpcl+LS4Bn3ziLPuEdGRkRjYAbSlWxSA= github.com/dgryski/go-farm v0.0.0-20190104051053-3adb47b1fb0f/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw= -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/golang/protobuf v1.2.1-0.20190205222052-c823c79ea157 h1:SdQMHsZ18/XZCHuwt3IF+dvHgYTO2XMWZjv3XBKQqAI= github.com/golang/protobuf v1.2.1-0.20190205222052-c823c79ea157/go.mod h1:Qd/q+1AKNOZr9uGQzbzCmRO6sUih6GTPZv6a1/R87v0= +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/nats-io/gnatsd v1.4.1 h1:RconcfDeWpKCD6QIIwiVFcvForlXpWeJP7i5/lDLy44= github.com/nats-io/gnatsd v1.4.1/go.mod h1:nqco77VO78hLCJpIcVfygDP2rPGfsEHkGTUk94uh5DQ= github.com/nats-io/go-nats v1.7.0 h1:oQOfHcLr8hb43QG8yeVyY2jtarIaTjOv41CGdF3tTvQ= diff --git a/main.go b/main.go index 2d3cca0..01f68ff 100644 --- a/main.go +++ b/main.go @@ -2,10 +2,11 @@ package main import ( "encoding/json" - "flag" "log" "net/http" + "os" + "github.com/joho/godotenv" "github.com/dgraph-io/badger" "github.com/nats-io/go-nats" "github.com/golang/protobuf/proto" @@ -18,17 +19,19 @@ var db *badger.DB var nc *nats.Conn func main() { - // Parse flags - flag.StringVar(&dbPath, "dbpath", "/tmp/badger", "path to store data") - flag.StringVar(&natsHost, "nats", "nats://localhost:4222", "host and port of NATS") - flag.Parse() + // Load .env + err := godotenv.Load() + if err != nil { + log.Fatal("Error loading .env file") + } + dbPath = os.Getenv("DBPATH") + natsHost = os.Getenv("NATS") // Open badger log.Printf("starting badger at %s", dbPath) opts := badger.DefaultOptions opts.Dir = dbPath opts.ValueDir = dbPath - var err error db, err = badger.Open(opts) if err != nil { log.Fatal(err)