Browse Source

do configs through env

Stephen McQuay 5 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 @@
1
+package main
2
+
3
+import (
4
+	"errors"
5
+)
6
+
7
+type Configs struct {
8
+	Verbose bool
9
+	Dbhost  string
10
+	Dbname  string
11
+	Dbport  int
12
+	Port    int
13
+}
14
+
15
+func NewConfig() *Configs {
16
+	return &Configs{
17
+		Verbose: true,
18
+		Dbname:  "itslog",
19
+		Dbport:  5432,
20
+		Port:    80,
21
+	}
22
+}
23
+
24
+func (c *Configs) validate() error {
25
+	if c.Dbhost == "" {
26
+		return errors.New("must specify dbhost (export ITSLOG_DBHOST=blah.local)")
27
+	}
28
+	return nil
29
+}

+ 19
- 0
main.go View File

@@ -0,0 +1,19 @@
1
+package main
2
+
3
+import (
4
+	"github.com/kelseyhightower/envconfig"
5
+	"log"
6
+)
7
+
8
+func main() {
9
+	log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
10
+	config := NewConfig()
11
+	err := envconfig.Process("ITSLOG", config)
12
+	if err != nil {
13
+		log.Fatal(err)
14
+	}
15
+	if err := config.validate(); err != nil {
16
+		log.Fatal(err)
17
+	}
18
+	log.Printf("%+v", config)
19
+}

Loading…
Cancel
Save