5
0
Fork 0

Use env for config instead of flags

feat/improved-bypass
UnicodingUnicorn 2019-02-18 23:43:24 +08:00
parent f54300aaea
commit e82b4b400e
6 changed files with 25 additions and 7 deletions

2
.env Normal file
View File

@ -0,0 +1,2 @@
LISTEN=:8080
SECRET=secret

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

View File

@ -2,6 +2,15 @@
Beep backend handling login. For now, just a POST endpoint returning a JWT. In the furture, SMS-based perpetual login.
## Environment variables
Supply environment variables by either exporting them or editing ```.env```.
| ENV | Description | Default |
| ---- | ----------- | ------- |
| LISTEN | Host and port number to listen on | :8080 |
| SECRET | JWT secret | secret |
## API (temporary)
```

1
go.mod
View File

@ -2,5 +2,6 @@ module login
require (
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/joho/godotenv v1.3.0
github.com/julienschmidt/httprouter v1.2.0
)

2
go.sum
View File

@ -1,4 +1,6 @@
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
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=

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/julienschmidt/httprouter"
"github.com/dgrijalva/jwt-go"
)
@ -14,11 +15,13 @@ var listen string
var secret []byte
func main() {
var s string
// Parse flags
flag.StringVar(&listen, "listen", ":8080", "host and port to listen on")
flag.StringVar(&s, "secret", "secret", "JWT secret")
flag.Parse()
// Load .env
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
listen = os.Getenv("LISTEN")
s := os.Getenv("SECRET")
secret = []byte(s)