Skip to content

Commit

Permalink
Merge pull request #10 from vibrato/viperconfig
Browse files Browse the repository at this point in the history
Improve rapid development
  • Loading branch information
TWinsnes authored Aug 6, 2018
2 parents e8e8fca + bce2534 commit d03c240
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 36 deletions.
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/vendor
/.git
/dist
14 changes: 10 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,21 @@ RUN curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh

WORKDIR $GOPATH/src/github.com/vibrato/TechTestApp

COPY Gopkg.toml Gopkg.lock $GOPATH/src/github.com/vibrato/TechTestApp/

RUN dep ensure -vendor-only -v

COPY . .

RUN ./build.sh \
&& cp -r ./dist /TechTestApp
RUN go build -o /TechTestApp

FROM alpine:latest

WORKDIR /TechTestApp

COPY --from=build /TechTestApp .
COPY assets ./assets
COPY conf.toml ./conf.toml

COPY --from=build /TechTestApp TechTestApp

ENTRYPOINT [ "./TechTestApp" ]
ENTRYPOINT [ "./TechTestApp" ]
98 changes: 86 additions & 12 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions Gopkg.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
[[constraint]]
name = "github.com/BurntSushi/toml"
version = "0.3.0"

[[constraint]]
name = "github.com/gorilla/mux"
version = "1.6.2"
Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func init() {

// initConfig reads in config file and ENV variables if set.
func initConfig() {
conf, err := config.LoadConfig("conf.toml")
conf, err := config.LoadConfig()

if err != nil {
fmt.Print(err)
Expand Down
43 changes: 28 additions & 15 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@
package config

import (
"io/ioutil"

"github.com/BurntSushi/toml"
"github.com/spf13/viper"
)

// internalConfig wraps the config values as the toml library was
Expand All @@ -38,25 +36,40 @@ type Config struct {
ListenPort string
}

// LoadConfig loads the configuration from file,
// and falls back to default calues if file
// could not be be loaded
func LoadConfig(path string) (Config, error) {
var conf = Config{}
func LoadConfig() (*Config, error) {
var conf = &Config{}

bytes, err := ioutil.ReadFile(path)
v := viper.New()

if err != nil {
return conf, err
}
v.SetConfigName("conf")
v.SetConfigType("toml")
v.AddConfigPath(".")

tomlcontent := string(bytes)
v.SetEnvPrefix("VTT")
v.AutomaticEnv()

_, err = toml.Decode(tomlcontent, &conf)
v.SetDefault("DbUser", "postgres")
v.SetDefault("DbPassword", "postgres")
v.SetDefault("DbName", "postgres")
v.SetDefault("DbPort", "postgres")
v.SetDefault("DbHost", "localhost")

v.SetDefault("ListenHost", "127.0.0.1")
v.SetDefault("ListenPort", "3000")

err := v.ReadInConfig() // Find and read the config file

if err != nil {
return conf, err
return nil, err
}

conf.DbUser = v.GetString("DbUser")
conf.DbPassword = v.GetString("DbPassword")
conf.DbName = v.GetString("DbName")
conf.DbHost = v.GetString("DbHost")
conf.DbPort = v.GetString("DbPort")
conf.ListenHost = v.GetString("ListenHost")
conf.ListenPort = v.GetString("ListenPort")

return conf, nil
}

0 comments on commit d03c240

Please sign in to comment.