-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
80 lines (67 loc) · 2.29 KB
/
config.go
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package config
import (
"fmt"
"github.com/bbengfort/epistolary/pkg"
"github.com/bbengfort/epistolary/pkg/utils/logger"
"github.com/bbengfort/epistolary/pkg/utils/sentry"
"github.com/gin-gonic/gin"
"github.com/rotationalio/confire"
"github.com/rotationalio/confire/validate"
"github.com/rs/zerolog"
)
type Config struct {
Maintenance bool `split_words:"true" default:"false"`
BindAddr string `split_words:"true" default:":8000"`
Mode GinMode `split_words:"true" default:"release"`
LogLevel logger.LevelDecoder `split_words:"true" default:"info"`
ConsoleLog bool `split_words:"true" default:"false"`
AllowOrigins []string `split_words:"true" default:"https://epistolary.app"`
Database DatabaseConfig
Token TokenConfig
Sentry sentry.Config
processed bool
}
type DatabaseConfig struct {
URL string `required:"true" split_words:"true"`
ReadOnly bool `split_words:"true" default:"false"`
Testing bool `split_words:"true" default:"false"`
}
type TokenConfig struct {
Keys map[string]string `desc:"a map of key ID to key path"`
Audience string `default:"https://epistolary.app"`
Issuer string `default:"https://api.epistolary.app"`
CookieDomain string `split_words:"true" default:"epistolary.app"`
}
// New creates a new Config object from environment variables prefixed with EPISTOLARY.
func New() (conf Config, err error) {
if err = confire.Process("epistolary", &conf); err != nil {
return conf, err
}
// Ensure the Sentry release is named correctly
if conf.Sentry.Release == "" {
conf.Sentry.Release = fmt.Sprintf("epistolary@%s", pkg.Version())
}
conf.processed = true
return conf, nil
}
func (c Config) GetLogLevel() zerolog.Level {
return zerolog.Level(c.LogLevel)
}
func (c Config) IsZero() bool {
return !c.processed
}
// Mark a manually constructed as processed as long as it is validated.
func (c Config) Mark() (Config, error) {
if err := validate.Validate(&c); err != nil {
return c, err
}
c.processed = true
return c, nil
}
type GinMode string
func (s GinMode) Validate() error {
if s != gin.ReleaseMode && s != gin.DebugMode && s != gin.TestMode {
return fmt.Errorf("%q is not a valid gin mode", s)
}
return nil
}