all repos

onasty @ 42ac36cd430f8bcb20a54be349a6538ecc81ebd4

a one-time notes service

onasty/internal/config/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
package config

import (
	"os"
)

type Config struct {
	AppEnv     string
	ServerPort string

	LogLevel  string
	LogFormat string

	PostgresDSN string
}

func NewConfig() *Config {
	return &Config{
		AppEnv:      getenvOrDefault("APP_ENV", "debug"),
		ServerPort:  getenvOrDefault("SERVER_PORT", "3000"),
		LogLevel:    getenvOrDefault("LOG_LEVEL", "debug"),
		LogFormat:   getenvOrDefault("LOG_FORMAT", "json"),
		PostgresDSN: getenvOrDefault("POSTGRESQL_DSN", ""),
	}
}

func (c *Config) IsDevMode() bool {
	return c.AppEnv == "debug" || c.AppEnv == "test"
}

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