forked from usefathom/fathom
/
config.go
88 lines (71 loc) · 1.92 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
81
82
83
84
85
86
87
88
package config
import (
"math/rand"
"net/url"
"os"
"path/filepath"
"github.com/joho/godotenv"
"github.com/kelseyhightower/envconfig"
log "github.com/sirupsen/logrus"
"github.com/usefathom/fathom/pkg/datastore/sqlstore"
)
// Config wraps the configuration structs for the various application parts
type Config struct {
Database *sqlstore.Config
Secret string
}
// LoadEnv loads env values from the supplied file
func LoadEnv(file string) {
if file == "" {
log.Warn("Missing configuration file. Using defaults.")
return
}
absFile, _ := filepath.Abs(file)
_, err := os.Stat(absFile)
fileNotExists := os.IsNotExist(err)
if fileNotExists {
log.Warnf("Error reading configuration. File `%s` does not exist.", file)
return
}
log.Printf("Configuration file: %s", absFile)
// read file into env values
err = godotenv.Load(absFile)
if err != nil {
log.Fatalf("Error parsing configuration file: %s", err)
}
}
// Parse environment into a Config struct
func Parse() *Config {
var cfg Config
// with config file loaded into env values, we can now parse env into our config struct
err := envconfig.Process("Fathom", &cfg)
if err != nil {
log.Fatalf("Error parsing configuration from environment: %s", err)
}
if cfg.Database.URL != "" {
_, err := url.Parse(cfg.Database.URL)
if err != nil {
log.Fatalf("Error parsing DATABASE_URL from environment: %s", err)
}
}
// alias sqlite to sqlite3
if cfg.Database.Driver == "sqlite" {
cfg.Database.Driver = "sqlite3"
}
// use absolute path to sqlite3 database
if cfg.Database.Driver == "sqlite3" {
cfg.Database.Name, _ = filepath.Abs(cfg.Database.Name)
}
// if secret key is empty, use a randomly generated one
if cfg.Secret == "" {
cfg.Secret = randomString(40)
}
return &cfg
}
func randomString(len int) string {
bytes := make([]byte, len)
for i := 0; i < len; i++ {
bytes[i] = byte(65 + rand.Intn(25)) //A=65 and Z = 65+25
}
return string(bytes)
}