-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
75 lines (63 loc) · 1.42 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
package main
import (
"fmt"
"os"
"strconv"
)
type Config struct {
ServerHost string
ServerPort int
JWTSecret string
PostgresHost string
PostgresPort int64
PostgresUser string
PostgresPassword string
PostgresDB string
}
func (c *Config) ServerAddr() string {
return fmt.Sprintf("%s:%d", c.ServerHost, c.ServerPort)
}
func (c *Config) PostgresDSN() string {
return fmt.Sprintf(
"postgres://%s:%s@%s:%d/%s",
c.PostgresUser,
c.PostgresPassword,
c.PostgresHost,
c.PostgresPort,
c.PostgresDB,
)
}
func NewConfig() *Config {
c := Config{
ServerHost: "localhost",
ServerPort: 8000,
JWTSecret: "secret",
PostgresHost: "localhost",
PostgresPort: 5432,
PostgresUser: "root",
PostgresPassword: "root",
PostgresDB: "movie_reservation_system",
}
if value, err := strconv.Atoi(os.Getenv("SERVER_PORT")); err == nil {
c.ServerPort = value
}
if value := os.Getenv("JWT_SECRET"); value != "" {
c.JWTSecret = value
}
if value := os.Getenv("POSTGRES_HOST"); value != "" {
c.PostgresHost = value
}
if value, err := strconv.Atoi(os.Getenv("POSTGRES_PORT")); err == nil {
c.PostgresPort = int64(value)
}
if value := os.Getenv("POSTGRES_USER"); value != "" {
c.PostgresUser = value
}
if value := os.Getenv("POSTGRES_PASSWORD"); value != "" {
c.PostgresPassword = value
}
if value := os.Getenv("POSTGRES_DB"); value != "" {
c.PostgresDB = value
}
return &c
}