4
1
Fork 0

Use env for config instead of flags

pull/24/head
UnicodingUnicorn 2019-02-18 23:43:00 +08:00
parent a0f72735f7
commit d323083f74
6 changed files with 24 additions and 11 deletions

2
.env Normal file
View File

@ -0,0 +1,2 @@
LISTEN=:8080
POSTGRES=postgresql://root@localhost:26257/core?sslmode=disable

View File

@ -3,11 +3,12 @@ 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 CGO_ENABLED=0 go build -ldflags "-s -w"
FROM scratch
COPY --from=build /src/core /core
COPY --from=build /src/.env /.env
ENTRYPOINT ["/core"]

View File

@ -12,14 +12,14 @@ migrate -database cockroach://root@localhost:26257/core?sslmode=disable -source
go build && ./core
```
## 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 |
| postgres | URL of Postgres | postgresql://root@localhost:26257/core?sslmode=disable |
| LISTEN | Host and port number to listen on | :8080 |
| POSTGRES | URL of Postgres | postgresql://root@localhost:26257/core?sslmode=disable |
## API

2
go.mod
View File

@ -2,8 +2,10 @@ module backend/core
require (
github.com/golang/protobuf v1.1.0 // indirect
github.com/joho/godotenv v1.3.0
github.com/julienschmidt/httprouter v0.0.0-20180715161854-348b672cd90d
github.com/lib/pq v0.0.0-20180523175426-90697d60dd84
github.com/ttacon/builder v0.0.0-20170518171403-c099f663e1c2 // indirect
github.com/ttacon/libphonenumber v1.0.0
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4 // indirect
)

4
go.sum
View File

@ -1,5 +1,7 @@
github.com/golang/protobuf v1.1.0 h1:0iH4Ffd/meGoXqF2lSAhZHt8X+cPgkfn/cb6Cce5Vpc=
github.com/golang/protobuf v1.1.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
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 v0.0.0-20180715161854-348b672cd90d h1:of6+TpypLAaiv4JxgH5aplBZnt0b65B4v4c8q5oy+Sk=
github.com/julienschmidt/httprouter v0.0.0-20180715161854-348b672cd90d/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/lib/pq v0.0.0-20180523175426-90697d60dd84 h1:it29sI2IM490luSc3RAhp5WuCYnc6RtbfLVAB7nmC5M=
@ -8,3 +10,5 @@ github.com/ttacon/builder v0.0.0-20170518171403-c099f663e1c2 h1:5u+EJUQiosu3JFX0
github.com/ttacon/builder v0.0.0-20170518171403-c099f663e1c2/go.mod h1:4kyMkleCiLkgY6z8gK5BkI01ChBtxR0ro3I1ZDcGM3w=
github.com/ttacon/libphonenumber v1.0.0 h1:5DJsnAoMCC+LjJ6lQqjjf2EHiDD6KH4rVhDsMn5oXII=
github.com/ttacon/libphonenumber v1.0.0/go.mod h1:E0TpmdVMq5dyVlQ7oenAkhsLu86OkUl+yR4OAxyEg/M=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4 h1:YUO/7uOKsKeq9UokNS62b8FYywz3ker1l1vDZRCRefw=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=

14
main.go
View File

@ -2,10 +2,11 @@ package main
import (
"database/sql"
"flag"
"log"
"net/http"
"os"
"github.com/joho/godotenv"
"github.com/julienschmidt/httprouter"
_ "github.com/lib/pq"
)
@ -14,10 +15,13 @@ var listen string
var postgres string
func main() {
// Parse flags
flag.StringVar(&listen, "listen", ":8080", "host and port to listen on")
flag.StringVar(&postgres, "postgres", "postgresql://root@localhost:26257/core?sslmode=disable", "postgres string")
flag.Parse()
// Load .env
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
listen = os.Getenv("LISTEN")
postgres = os.Getenv("POSTGRES")
// Open postgres
log.Printf("connecting to postgres %s", postgres)