5
0
Fork 0

Use env for config instead of flags

master
UnicodingUnicorn 2019-02-18 23:42:49 +08:00
parent f9b278f18c
commit 863d44134a
6 changed files with 21 additions and 13 deletions

2
.env Normal file
View File

@ -0,0 +1,2 @@
LISTEN=:8080
NATS=nats://localhost:4222

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/bite /bite
COPY --from=build /src/.env /.env
ENTRYPOINT ["/bite"]

View File

@ -8,14 +8,14 @@ Beep backend handling of audio bites. Chopped up words spoken are uploaded into
go build && ./backend-bite
```
## 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 |
| nats | URL of NATS | nats://localhost:4222 |
| LISTEN | Host and port number to listen on | :8080 |
| NATS | Host and port of nats | nats://localhost:4222 |
## API

1
go.mod
View File

@ -2,6 +2,7 @@ module bite
require (
github.com/golang/protobuf v1.2.1-0.20190205222052-c823c79ea157
github.com/joho/godotenv v1.3.0
github.com/julienschmidt/httprouter v1.2.0
github.com/nats-io/gnatsd v1.4.1 // indirect
github.com/nats-io/go-nats v1.7.0

4
go.sum
View File

@ -1,7 +1,7 @@
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/julienschmidt/httprouter v1.2.0 h1:TDTW5Yz1mjftljbcKqRcrYhd4XeOoI98t+9HbQbYf7g=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/nats-io/gnatsd v1.4.1 h1:RconcfDeWpKCD6QIIwiVFcvForlXpWeJP7i5/lDLy44=

14
main.go
View File

@ -1,15 +1,16 @@
package main
import (
"flag"
"net/http"
"log"
"os"
"strconv"
"time"
"github.com/julienschmidt/httprouter"
"github.com/nats-io/go-nats"
"github.com/golang/protobuf/proto"
"github.com/joho/godotenv"
)
var listen string
@ -18,10 +19,13 @@ var natsHost string
var natsConn *nats.Conn
func main() {
// Parse flags
flag.StringVar(&listen, "listen", ":8080", "host and port to listen on")
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")
}
listen = os.Getenv("LISTEN")
natsHost = os.Getenv("NATS")
// NATS client
natsConn, err := nats.Connect(natsHost)