all repos

onasty @ e0dc5bb

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package config

import (
	"os"
	"time"
)

type Config struct {
	AppEnv       string
	ServerPort   string
	PasswordSalt string

	JwtSigningKey      string
	JwtAccessTokenTTL  time.Duration
	JwtRefreshTokenTTL time.Duration

	LogLevel  string
	LogFormat string

	PostgresDSN string
}

func NewConfig() *Config {
	return &Config{
		AppEnv:             getenvOrDefault("APP_ENV", "debug"),
		ServerPort:         getenvOrDefault("SERVER_PORT", "3000"),
		PasswordSalt:       getenvOrDefault("PASSWORD_SALT", ""),
		JwtSigningKey:      getenvOrDefault("JWT_SIGNING_KEY", ""),
		JwtAccessTokenTTL:  mustParseDuration(getenvOrDefault("JWT_ACCESS_TOKEN_TTL", "15m")),
		JwtRefreshTokenTTL: mustParseDuration(getenvOrDefault("JWT_REFRESH_TOKEN_TTL", "15d")),
		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
}

func mustParseDuration(dur string) time.Duration {
	d, _ := time.ParseDuration(dur)
	return d
}