Skip to content

Commit 963770b

Browse files
Docker working again, config working again
1 parent a443045 commit 963770b

File tree

6 files changed

+33
-6
lines changed

6 files changed

+33
-6
lines changed

Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ FROM alpine
2424
# Copy our static executable.
2525
RUN apk update && apk add --no-cache vips-dev
2626
COPY --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"]

Taskfile.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ tasks:
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:
@@ -44,7 +45,7 @@ tasks:
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

cmd/config.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
PORT: 1323
2+
DB_URL: postgres://test:test@localhost:5435/gomin

cmd/config.yml

Whitespace-only changes.

docker-compose.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff 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

pkg/common/config/config.go

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,47 @@
11
package config
22

33
import (
4+
"flag"
5+
"os"
6+
7+
"github.com/rs/zerolog/log"
48
"github.com/spf13/viper"
59
)
610

711
type Config struct {
8-
Port string `mapstructure:"PORT"`
12+
Port int `mapstructure:"PORT"`
913
DbUrl string `mapstructure:"DB_URL"`
1014
}
1115

1216
func 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
}

0 commit comments

Comments
 (0)