Skip to content

load config file via viper #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package main

import (
"fmt"
"github.com/fsnotify/fsnotify"
"time"

"github.com/spf13/viper"
_ "github.com/spf13/viper/remote"
)

func loadConfigFile(configType, configLocation, consulKey string) {
setDefaults()
if configLocation == "" {
return
}

switch configType {
case "local":
viper.SetConfigName("config")
viper.AddConfigPath(configLocation)

err := viper.ReadInConfig()
if err != nil {
panic(fmt.Errorf("Fatal error config file: %s \n", err))
}

viper.WatchConfig()

}

viper.OnConfigChange(func(e fsnotify.Event) {
fmt.Println("Config file changed:", e.Name)
})
}

func setDefaults() {
viper.SetDefault("server.port", 3000)
viper.SetDefault("backends.numConns", 3)
viper.SetDefault("metrics.port", 8080)
}
15 changes: 15 additions & 0 deletions config.yml.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
metrics:
port: 8080

server:
port: 3001
cacert: ''
privkey: ''

cache:
enabled: true

backends:
hosts: []
numConns: 3

2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ require (
github.com/OneOfOne/xxhash v1.2.8 // indirect
github.com/dgraph-io/ristretto v0.0.3
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13 // indirect
github.com/fsnotify/fsnotify v1.4.7
github.com/kr/text v0.2.0 // indirect
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/prometheus/client_golang v1.7.1
github.com/spaolacci/murmur3 v1.1.0 // indirect
github.com/spf13/viper v1.7.1
github.com/stretchr/testify v1.6.1 // indirect
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae // indirect
google.golang.org/protobuf v1.25.0 // indirect
Expand Down
Loading