Skip to content

Commit 0923279

Browse files
Merge pull request #8 from speakeasy-api/env-vars-for-secrets
feat: use env vars for secrets
2 parents b6b0368 + 4d03f07 commit 0923279

File tree

14 files changed

+133
-22
lines changed

14 files changed

+133
-22
lines changed

cmd/server/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func main() {
2222

2323
func appStart(ctx context.Context, a *app.App) ([]app.Listener, error) {
2424
// Load configuration from config/config.yaml which contains details such as DB connection params
25-
cfg, err := config.Load()
25+
cfg, err := config.Load(ctx)
2626
if err != nil {
2727
return nil, err
2828
}

config/config-docker.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
11
http:
22
port: "8080"
3-
psql:
4-
dsn: "postgresql://guest:guest@postgres:5432/speakeasy?sslmode=disable"

config/config.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
11
http:
22
port: "8080"
3-
psql:
4-
dsn: "postgresql://guest:guest@localhost:5432/speakeasy?sslmode=disable"

docker-compose.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ services:
44
build: .
55
ports:
66
- "8080:8080"
7+
environment:
8+
- POSTGRES_DSN="postgresql://guest:guest@postgres:5432/speakeasy?sslmode=disable" # Test credentials
9+
- SPEAKEASY_ENVIRONMENT=docker
710
depends_on:
811
- postgres
912
postgres:

go.mod

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ go 1.18
44

55
require (
66
github.com/AlekSi/pointer v1.2.0
7+
github.com/caarlos0/env/v6 v6.9.3
78
github.com/cenkalti/backoff/v4 v4.1.2
9+
github.com/go-playground/validator/v10 v10.11.0
810
github.com/golang-migrate/migrate/v4 v4.15.1
911
github.com/golang/mock v1.6.0
1012
github.com/gorilla/mux v1.8.0
@@ -34,11 +36,14 @@ require (
3436
github.com/felixge/httpsnoop v1.0.2 // indirect
3537
github.com/go-logr/logr v1.2.3 // indirect
3638
github.com/go-logr/stdr v1.2.2 // indirect
39+
github.com/go-playground/locales v0.14.0 // indirect
40+
github.com/go-playground/universal-translator v0.18.0 // indirect
3741
github.com/gogo/protobuf v1.3.2 // indirect
3842
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
3943
github.com/hashicorp/errwrap v1.0.0 // indirect
4044
github.com/hashicorp/go-multierror v1.1.0 // indirect
4145
github.com/imdario/mergo v0.3.12 // indirect
46+
github.com/leodido/go-urn v1.2.1 // indirect
4247
github.com/mitchellh/mapstructure v1.4.3 // indirect
4348
github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6 // indirect
4449
github.com/opencontainers/go-digest v1.0.0 // indirect
@@ -54,7 +59,9 @@ require (
5459
go.opentelemetry.io/otel/metric v0.27.0 // indirect
5560
go.uber.org/atomic v1.9.0 // indirect
5661
go.uber.org/multierr v1.8.0 // indirect
62+
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 // indirect
5763
golang.org/x/net v0.0.0-20220225172249-27dd8689420f // indirect
5864
golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8 // indirect
65+
golang.org/x/text v0.3.7 // indirect
5966
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
6067
)

go.sum

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ github.com/buger/jsonparser v0.0.0-20180808090653-f4dd9f5a6b44/go.mod h1:bbYlZJ7
156156
github.com/bugsnag/bugsnag-go v0.0.0-20141110184014-b1d153021fcd/go.mod h1:2oa8nejYd4cQ/b0hMIopN0lCRxU0bueqREvZLWFrtK8=
157157
github.com/bugsnag/osext v0.0.0-20130617224835-0dd3f918b21b/go.mod h1:obH5gd0BsqsP2LwDJ9aOkm/6J86V6lyAXCoQWGw3K50=
158158
github.com/bugsnag/panicwrap v0.0.0-20151223152923-e2c28503fcd0/go.mod h1:D/8v3kj0zr8ZAKg1AQ6crr+5VwKN5eIywRkfhyM/+dE=
159+
github.com/caarlos0/env/v6 v6.9.3 h1:Tyg69hoVXDnpO5Qvpsu8EoquarbPyQb+YwExWHP8wWU=
160+
github.com/caarlos0/env/v6 v6.9.3/go.mod h1:hvp/ryKXKipEkcuYjs9mI4bBCg+UI0Yhgm5Zu0ddvwc=
159161
github.com/cenkalti/backoff/v4 v4.0.2/go.mod h1:eEew/i+1Q6OrCDZh3WiXYv3+nJwBASZ8Bog/87DQnVg=
160162
github.com/cenkalti/backoff/v4 v4.1.2 h1:6Yo7N8UP2K6LWZnW94DLVSSrbobcWdVzAYOisuDPIFo=
161163
github.com/cenkalti/backoff/v4 v4.1.2/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw=
@@ -290,6 +292,7 @@ github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfc
290292
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
291293
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
292294
github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY=
295+
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
293296
github.com/creack/pty v1.1.11 h1:07n33Z8lZxZ2qwegKbObQohDhXDQxiMMz1NOUGYlesw=
294297
github.com/creack/pty v1.1.11/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
295298
github.com/cyphar/filepath-securejoin v0.2.2/go.mod h1:FpkQEhXnPnOthhzymB7CGsFk2G9VLXONKD9G7QGMM+4=
@@ -392,6 +395,14 @@ github.com/go-openapi/jsonreference v0.19.3/go.mod h1:rjx6GuL8TTa9VaixXglHmQmIL9
392395
github.com/go-openapi/spec v0.19.3/go.mod h1:FpwSN1ksY1eteniUU7X0N/BgJ7a4WvBFVA8Lj9mJglo=
393396
github.com/go-openapi/swag v0.19.2/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk=
394397
github.com/go-openapi/swag v0.19.5/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk=
398+
github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A=
399+
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
400+
github.com/go-playground/locales v0.14.0 h1:u50s323jtVGugKlcYeyzC0etD1HifMjqmJqb8WugfUU=
401+
github.com/go-playground/locales v0.14.0/go.mod h1:sawfccIbzZTqEDETgFXqTho0QybSa7l++s0DH+LDiLs=
402+
github.com/go-playground/universal-translator v0.18.0 h1:82dyy6p4OuJq4/CByFNOn/jYrnRPArHwAcmLoJZxyho=
403+
github.com/go-playground/universal-translator v0.18.0/go.mod h1:UvRDBj+xPUEGrFYl+lu/H90nyDXpg0fqeB/AQUGNTVA=
404+
github.com/go-playground/validator/v10 v10.11.0 h1:0W+xRM511GY47Yy3bZUbJVitCNg2BOGlCyvTqsp/xIw=
405+
github.com/go-playground/validator/v10 v10.11.0/go.mod h1:i+3WkQ1FvaUjjxh1kSvIA4dMGDBiPU55YFDl0WbKdWU=
395406
github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
396407
github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs=
397408
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
@@ -659,14 +670,18 @@ github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxv
659670
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
660671
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
661672
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
662-
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
663673
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
674+
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
675+
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
664676
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
665677
github.com/kr/pty v1.1.5/go.mod h1:9r2w37qlBe7rQ6e1fg1S/9xpWHSnaqNdHD3WcMdbPDA=
666678
github.com/kr/pty v1.1.8/go.mod h1:O1sed60cT9XZ5uDucP5qwvh+TE3NnUj51EiZO/lmSfw=
667-
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
668679
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
680+
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
681+
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
669682
github.com/ktrysmt/go-bitbucket v0.6.4/go.mod h1:9u0v3hsd2rqCHRIpbir1oP7F58uo5dq19sBYvuMoyQ4=
683+
github.com/leodido/go-urn v1.2.1 h1:BqpAaACuzVSgi/VLzGZIobT2z4v53pjosyNd9Yv6n/w=
684+
github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY=
670685
github.com/lib/pq v0.0.0-20180327071824-d34b9ff171c2/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
671686
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
672687
github.com/lib/pq v1.1.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
@@ -794,6 +809,7 @@ github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi
794809
github.com/pierrec/lz4/v4 v4.1.8/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
795810
github.com/pkg/browser v0.0.0-20210706143420-7d21f8c997e2/go.mod h1:HKlIX3XHQyzLZPlr7++PzdhaXEj94dEiJgZDTsxEqUI=
796811
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8/go.mod h1:HKlIX3XHQyzLZPlr7++PzdhaXEj94dEiJgZDTsxEqUI=
812+
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
797813
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
798814
github.com/pkg/errors v0.8.1-0.20171018195549-f15c970de5b7/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
799815
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
@@ -838,6 +854,9 @@ github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6L
838854
github.com/rogpeppe/go-internal v1.1.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
839855
github.com/rogpeppe/go-internal v1.2.2/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
840856
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
857+
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
858+
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
859+
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
841860
github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ=
842861
github.com/rs/zerolog v1.13.0/go.mod h1:YbFCdg8HfsridGWAh22vktObvhZbQsZXe4/zB0OKkWU=
843862
github.com/rs/zerolog v1.15.0/go.mod h1:xYTKnLHcpfU2225ny5qZjxnj9NvkumZYjJHlAThCjNc=
@@ -1011,6 +1030,8 @@ golang.org/x/crypto v0.0.0-20201002170205-7f63de1d35b0/go.mod h1:LzIPMQfyMNhhGPh
10111030
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
10121031
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
10131032
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
1033+
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 h1:0es+/5331RGQPcXlMfP+WrnIIS6dNnNRe0WB02W0F4M=
1034+
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
10141035
golang.org/x/exp v0.0.0-20180321215751-8460e604b9de/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
10151036
golang.org/x/exp v0.0.0-20180807140117-3d87b88a115f/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
10161037
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
@@ -1112,6 +1133,7 @@ golang.org/x/net v0.0.0-20210505024714-0287a6fb4125/go.mod h1:9nx3DQGgdP8bBQD5qx
11121133
golang.org/x/net v0.0.0-20210614182718-04defd469f4e/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
11131134
golang.org/x/net v0.0.0-20210813160813-60bc85c4be6d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
11141135
golang.org/x/net v0.0.0-20211013171255-e13a2654a71e/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
1136+
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
11151137
golang.org/x/net v0.0.0-20220225172249-27dd8689420f h1:oA4XRj0qtSt8Yo1Zms0CUlsT3KG69V2UGQWPBxujDmc=
11161138
golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
11171139
golang.org/x/oauth2 v0.0.0-20180227000427-d7d64896b5ff/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
@@ -1236,6 +1258,7 @@ golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBc
12361258
golang.org/x/sys v0.0.0-20210616045830-e2b7044e8c71/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
12371259
golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
12381260
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
1261+
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
12391262
golang.org/x/sys v0.0.0-20210818153620-00dd8d7831e7/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
12401263
golang.org/x/sys v0.0.0-20210906170528-6f6e22806c34/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
12411264
golang.org/x/sys v0.0.0-20211013075003-97ac67df715c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
@@ -1252,6 +1275,7 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
12521275
golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
12531276
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
12541277
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
1278+
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
12551279
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
12561280
golang.org/x/time v0.0.0-20180412165947-fbb02b2291d2/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
12571281
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=

internal/config/config.go

Lines changed: 78 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,94 @@
11
package config
22

3-
import "github.com/speakeasy-api/rest-template-go/internal/core/config"
3+
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
8+
"github.com/caarlos0/env/v6"
9+
"github.com/go-playground/validator/v10"
10+
"github.com/speakeasy-api/rest-template-go/internal/core/config"
11+
"github.com/speakeasy-api/rest-template-go/internal/core/errors"
12+
"github.com/speakeasy-api/rest-template-go/internal/core/logging"
13+
"go.uber.org/zap"
14+
"gopkg.in/yaml.v2"
15+
)
16+
17+
const (
18+
// ErrInvalidEnvironment is returned when the SPEAKEASY_ENVIRONMENT environment variable is not set.
19+
ErrInvalidEnvironment = errors.Error("SPEAKEASY_ENVIRONMENT is not set")
20+
// ErrValidation is returned when the configuration is invalid.
21+
ErrValidation = errors.Error("invalid configuration")
22+
// ErrEnvVars is returned when the environment variables are invalid.
23+
ErrEnvVars = errors.Error("failed parsing env vars")
24+
// ErrRead is returned when the configuration file cannot be read.
25+
ErrRead = errors.Error("failed to read file")
26+
// ErrUnmarshal is returned when the configuration file cannot be unmarshalled.
27+
ErrUnmarshal = errors.Error("failed to unmarshal file")
28+
)
29+
30+
var (
31+
baseConfigPath = "config/config.yaml"
32+
envConfigPath = "config/config-%s.yaml"
33+
)
434

535
// Config represents the configuration of our application.
636
type Config struct {
737
config.AppConfig `yaml:",inline"`
838
}
939

1040
// Load loads the configuration from the config/config.yaml file.
11-
func Load() (*Config, error) {
41+
func Load(ctx context.Context) (*Config, error) {
1242
cfg := &Config{}
1343

14-
if err := config.Load(cfg); err != nil {
44+
if err := loadFromFiles(ctx, cfg); err != nil {
1545
return nil, err
1646
}
1747

48+
if err := env.Parse(cfg); err != nil {
49+
return nil, ErrEnvVars.Wrap(err)
50+
}
51+
52+
validate := validator.New()
53+
if err := validate.Struct(cfg); err != nil {
54+
return nil, ErrValidation.Wrap(err)
55+
}
56+
1857
return cfg, nil
1958
}
59+
60+
func loadFromFiles(ctx context.Context, cfg any) error {
61+
environ := os.Getenv("SPEAKEASY_ENVIRONMENT")
62+
if environ == "" {
63+
return ErrInvalidEnvironment
64+
}
65+
66+
if err := loadYaml(ctx, baseConfigPath, cfg); err != nil {
67+
return err
68+
}
69+
70+
p := fmt.Sprintf(envConfigPath, environ)
71+
72+
if _, err := os.Stat(p); !errors.Is(err, os.ErrNotExist) {
73+
if err := loadYaml(ctx, p, cfg); err != nil {
74+
return err
75+
}
76+
}
77+
78+
return nil
79+
}
80+
81+
func loadYaml(ctx context.Context, filename string, cfg any) error {
82+
logging.From(ctx).Info("Loading configuration", zap.String("path", filename))
83+
84+
data, err := os.ReadFile(filename)
85+
if err != nil {
86+
return ErrRead.Wrap(err)
87+
}
88+
89+
if err := yaml.Unmarshal(data, cfg); err != nil {
90+
return ErrUnmarshal.Wrap(err)
91+
}
92+
93+
return nil
94+
}

internal/core/config/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package config
22

33
import (
4-
"io/ioutil"
4+
"os"
55

66
"github.com/speakeasy-api/rest-template-go/internal/core/drivers/psql"
77
"github.com/speakeasy-api/rest-template-go/internal/core/errors"
@@ -24,7 +24,7 @@ type AppConfig struct {
2424

2525
// Load loads the configuration from a yaml file on disk.
2626
func Load(cfg interface{}) error {
27-
data, err := ioutil.ReadFile("config/config.yaml") // TODO support different environments
27+
data, err := os.ReadFile("config/config.yaml") // TODO support different environments
2828
if err != nil {
2929
return ErrRead.Wrap(err)
3030
}

internal/core/drivers/psql/psql.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const (
1717

1818
// Config represents the configuration for our postgres database.
1919
type Config struct {
20-
DSN string `yaml:"dsn"` // TODO get this from secrets in the future
20+
DSN string `env:"POSTGRES_DSN" validate:"required"`
2121
}
2222

2323
// Driver provides an implementation for connecting to a postgres database.

internal/core/errors/errors.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func (w wrappedError) Unwrap() error {
8888

8989
// New just wraps errors.New as we don't want to alias the errors package everywhere to use it.
9090
func New(message string) error {
91-
// nolint:goerr113
91+
//nolint:goerr113
9292
return errors.New(message)
9393
}
9494

0 commit comments

Comments
 (0)