all repos

onasty @ c685a654684618188c618d65966a689f8b2dab39

a one-time notes service

onasty/mailer/config.go(view raw)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package main

import (
	"os"
	"strconv"
)

type Config struct {
	AppURL        string
	NatsURL       string
	MailgunFrom   string
	MailgunDomain string
	MailgunAPIKey string

	LogLevel    string
	LogFormat   string
	LogShowLine bool

	MetricsEnabled bool
	MetricsPort    int
}

func NewConfig() *Config {
	return &Config{
		AppURL:         getenvOrDefault("APP_URL", ""),
		NatsURL:        getenvOrDefault("NATS_URL", ""),
		MailgunFrom:    getenvOrDefault("MAILGUN_FROM", ""),
		MailgunDomain:  getenvOrDefault("MAILGUN_DOMAIN", ""),
		MailgunAPIKey:  getenvOrDefault("MAILGUN_API_KEY", ""),
		LogLevel:       getenvOrDefault("LOG_LEVEL", "debug"),
		LogFormat:      getenvOrDefault("LOG_FORMAT", "json"),
		LogShowLine:    getenvOrDefault("LOG_SHOW_LINE", "true") == "true",
		MetricsPort:    mustGetenvOrDefaultInt("METRICS_PORT", 8001),
		MetricsEnabled: getenvOrDefault("METRICS_ENABLED", "true") == "true",
	}
}

func getenvOrDefault(key, def string) string {
	if v, ok := os.LookupEnv(key); ok {
		return v
	}
	return def
}

func mustGetenvOrDefaultInt(key string, def int) int {
	if v, ok := os.LookupEnv(key); ok {
		r, err := strconv.Atoi(v)
		if err != nil {
			panic(err)
		}
		return r
	}
	return def
}