File tree Expand file tree Collapse file tree 6 files changed +33
-6
lines changed
Expand file tree Collapse file tree 6 files changed +33
-6
lines changed Original file line number Diff line number Diff line change @@ -24,6 +24,7 @@ FROM alpine
2424# Copy our static executable.
2525RUN apk update && apk add --no-cache vips-dev
2626COPY --from=builder /go/bin/app /go/bin/app
27+ COPY --from=builder /src/app/cmd/config.yaml /go/bin
2728
2829# Run the hello binary.
29- ENTRYPOINT ["/go/bin/app" ]
30+ ENTRYPOINT ["/go/bin/app" , "-configpath=/go/bin/config.yaml" ]
Original file line number Diff line number Diff line change 77 build :
88 cmds :
99 - mkdir -p out/bin
10+ - cp cmd/config.yaml out/bin
1011 - go build -mod vendor -o out/bin/gomin cmd/main.go
1112
1213 clean :
4445 server.run :
4546 deps : [db.migrate,build]
4647 cmds :
47- - ./out/bin/gomin
48+ - ./out/bin/gomin -configpath "{{ .TASKFILE_DIR }}/out/bin/config.yaml"
4849 env :
4950 APP_DB_URL : " postgres://test:test@localhost:5435/gomin"
5051 APP_PORT : 1323
Original file line number Diff line number Diff line change 1+ PORT : 1323
2+ DB_URL : postgres://test:test@localhost:5435/gomin
Original file line number Diff line number Diff line change @@ -26,5 +26,7 @@ services:
2626 - ' 1323:1323'
2727 restart : always
2828 environment :
29+ # Must be prefixed with APP_
30+ # See pkg/common/config/config.go and viper for more details
2931 - APP_DB_URL=postgres://test:test@gomin_db:5432/gomin
3032 - APP_PORT=1323
Original file line number Diff line number Diff line change 11package config
22
33import (
4+ "flag"
5+ "os"
6+
7+ "github.com/rs/zerolog/log"
48 "github.com/spf13/viper"
59)
610
711type Config struct {
8- Port string `mapstructure:"PORT"`
12+ Port int `mapstructure:"PORT"`
913 DbUrl string `mapstructure:"DB_URL"`
1014}
1115
1216func LoadConfig () (c Config , err error ) {
13- viper .SetConfigType ("env" )
17+ // Assumes a configpath Flag passed into our application
18+ configPath := flag .String ("configpath" , "" , "Config Path" )
19+ flag .Parse ()
20+ if configPath == nil || len (* configPath ) == 0 {
21+ log .Fatal ().Msgf ("Unable to load config path. Empty Path specified. " )
22+ }
23+ if _ , err := os .Stat (* configPath ); os .IsNotExist (err ) {
24+ // path/to/whatever does not exist
25+ log .Fatal ().Msgf ("Unable to load config path. Path not found. " )
26+ }
27+
28+ // Actually load in the config file from the path provided
29+ viper .SetConfigFile (* configPath )
30+ viper .SetConfigType ("yaml" )
31+
32+ // Enable overriding with environment variables, useful for docker
1433 viper .SetEnvPrefix ("APP" )
1534 viper .AutomaticEnv ()
1635
1736 err = viper .ReadInConfig ()
18-
1937 if err != nil {
2038 return
2139 }
2240
41+ // Unmarshal the config into the Config struct above
2342 err = viper .Unmarshal (& c )
24-
43+ if err != nil {
44+ log .Fatal ().Msgf ("unable to decode into struct, %v" , err )
45+ }
2546 return
2647}
You can’t perform that action at this time.
0 commit comments