5
0
Fork 0

Use env for config instead of flags

master
UnicodingUnicorn 2019-02-18 23:43:49 +08:00
parent f897b74878
commit 140433f720
6 changed files with 25 additions and 9 deletions

2
.env Normal file
View File

@ -0,0 +1,2 @@
DBPATH=/tmp/badger
NATS=nats://localhost:4222

View File

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

View File

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

1
go.mod
View File

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

4
go.sum
View File

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

15
main.go
View File

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