Skip to content

Commit d658381

Browse files
committed
make db configurable
1 parent 2399bff commit d658381

File tree

6 files changed

+74
-9
lines changed

6 files changed

+74
-9
lines changed

Dockerfile

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
FROM golang:1.13-alpine as builder
2+
RUN apk add --no-cache git make
3+
ENV GOOS=linux
4+
ENV CGO_ENABLED=0
5+
ENV GO111MODULE=on
6+
COPY . /src
7+
WORKDIR /src
8+
RUN rm -f go.sum
9+
RUN make test
10+
RUN make alpine
11+
12+
FROM alpine:3.11
13+
RUN apk add --no-cache ca-certificates git curl
14+
WORKDIR /app
15+
COPY --from=builder /src/bin/apiserver /app/apiserver
16+
CMD ["/app/apiserver"]

Makefile

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.PHONY: test alpine
2+
3+
all: test alpine
4+
5+
alpine:
6+
go build -a -installsuffix cgo -o bin/apiserver cmd/apiserver/main.go
7+
8+
test:
9+
go test ./... -count=1

cmd/apiserver/main.go

+41-9
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,29 @@ import (
66
"fmt"
77
"net/http"
88

9+
log "github.com/sirupsen/logrus"
10+
flag "github.com/spf13/pflag"
11+
912
_ "github.com/lib/pq"
1013
)
1114

15+
var (
16+
dbUser string
17+
dbPassword string
18+
dbHost string
19+
dbName string
20+
)
21+
22+
func init() {
23+
log.SetFormatter(&log.JSONFormatter{})
24+
25+
flag.StringVar(&dbName, "db-name", getEnv("DB_NAME", "postgres"), "database name")
26+
flag.StringVar(&dbUser, "db-user", getEnv("DB_USER", "postgres"), "database username")
27+
flag.StringVar(&dbPassword, "db-password", os.Getenv("DB_PASSWORD"), "database password")
28+
flag.StringVar(&dbHost, "db-hostname", getEnv("DB_HOST", "localhost"), "database hostname")
29+
flag.Parse()
30+
}
31+
1232
type Client struct {
1333
PSK string `json:"psk"`
1434
Peer
@@ -24,12 +44,20 @@ type GatewayResponse struct {
2444
}
2545

2646
func main() {
27-
http.HandleFunc("/gateways/gw0", func(w http.ResponseWriter, _ *http.Request) {
47+
http.HandleFunc("/gateways/gw0", gatewayConfigHandler())
48+
49+
bindAddr := fmt.Sprintf("%s:%d", "127.0.0.1", 6969)
50+
fmt.Println("running @", bindAddr)
51+
fmt.Println((&http.Server{Addr: bindAddr}).ListenAndServe())
52+
}
53+
54+
func gatewayConfigHandler() func(w http.ResponseWriter, _ *http.Request) {
55+
return func(w http.ResponseWriter, _ *http.Request) {
2856
postgresConnection := fmt.Sprintf("user=%s password=%s dbname=%s host=%s sslmode=disable",
29-
"postgres",
30-
"asdf",
31-
"postgres",
32-
"localhost")
57+
dbUser,
58+
dbPassword,
59+
dbName,
60+
dbHost)
3361

3462
db, err := sql.Open("postgres", postgresConnection)
3563
if err != nil {
@@ -62,9 +90,13 @@ func main() {
6290

6391
w.WriteHeader(http.StatusOK)
6492
json.NewEncoder(w).Encode(resp)
65-
})
93+
}
94+
}
6695

67-
bindAddr := fmt.Sprintf("%s:%d", "127.0.0.1", 6969)
68-
fmt.Println("running @", bindAddr)
69-
fmt.Println((&http.Server{Addr: bindAddr}).ListenAndServe())
96+
func getEnv(key, fallback string) string {
97+
if value, ok := os.LookupEnv(key); ok {
98+
return value
99+
}
100+
101+
return fallback
70102
}

go.mod

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/nais/device
2+
3+
go 1.13
4+
5+
require github.com/lib/pq v1.3.0

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/lib/pq v1.3.0 h1:/qkRGz8zljWiDcFvgpwUpwIAPu3r07TDvs3Rws+o/pU=
2+
github.com/lib/pq v1.3.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=

main.go

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package naisdevice

0 commit comments

Comments
 (0)