-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
77 lines (68 loc) · 1.6 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
package main
import (
"encoding/json"
"fmt"
"os"
)
type PostgresConfig struct {
Host string `json:"host"`
Port int `json:"port"`
User string `json:"user"`
Password string `json:"password"`
Name string `json:"name"`
}
func (c PostgresConfig) Dialect() string {
return "postgres"
}
func (c PostgresConfig) ConnectionInfo() string {
if c.Password == "" {
return fmt.Sprintf("host=%s port=%d user=%s dbname=%s sslmode=disable", c.Host, c.Port, c.User, c.Name)
}
return fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", c.Host, c.Port, c.User, c.Password, c.Name)
}
func DefaultPostgresConfig() PostgresConfig {
return PostgresConfig{
Host: "localhost",
Port: 5432,
User: "kho",
// Password: "your-password",
Name: "simplephotohost_dev",
}
}
type Config struct {
Port int `json:"port"`
Env string `json:"env"`
Pepper string `json:"pepper"`
HMACKey string `json:"hmac_key"`
Database PostgresConfig `json:"database"`
}
func (c Config) IsProd() bool {
return c.Env == "prod"
}
func DefaultConfig() Config {
return Config{
Port: 3000,
Env: "dev",
Pepper: "secret-random-string",
HMACKey: "secret-hmac-key",
Database: DefaultPostgresConfig(),
}
}
func LoadConfig(configReq bool) Config {
f, err := os.Open(".config")
if err != nil {
if configReq {
panic(err)
}
fmt.Println("Using the default config...")
return DefaultConfig()
}
var c Config
dec := json.NewDecoder(f)
err = dec.Decode(&c)
if err != nil {
panic(err)
}
fmt.Println("Successfully loaded .config")
return c
}