Browse Source

do configs through env

master
Stephen McQuay 6 years ago
parent
commit
a8e03e721e
2 changed files with 48 additions and 0 deletions
  1. +29
    -0
      config.go
  2. +19
    -0
      main.go

+ 29
- 0
config.go View File

@@ -0,0 +1,29 @@
package main

import (
"errors"
)

type Configs struct {
Verbose bool
Dbhost string
Dbname string
Dbport int
Port int
}

func NewConfig() *Configs {
return &Configs{
Verbose: true,
Dbname: "itslog",
Dbport: 5432,
Port: 80,
}
}

func (c *Configs) validate() error {
if c.Dbhost == "" {
return errors.New("must specify dbhost (export ITSLOG_DBHOST=blah.local)")
}
return nil
}

+ 19
- 0
main.go View File

@@ -0,0 +1,19 @@
package main

import (
"github.com/kelseyhightower/envconfig"
"log"
)

func main() {
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
config := NewConfig()
err := envconfig.Process("ITSLOG", config)
if err != nil {
log.Fatal(err)
}
if err := config.validate(); err != nil {
log.Fatal(err)
}
log.Printf("%+v", config)
}

Loading…
Cancel
Save