@@ -2,7 +2,6 @@ package main
2
2
3
3
import (
4
4
"database/sql"
5
- "encoding/json"
6
5
"fmt"
7
6
"os"
8
7
"os/signal"
@@ -18,50 +17,30 @@ import (
18
17
"github.com/iplay88keys/my-recipe-library/pkg/api"
19
18
"github.com/iplay88keys/my-recipe-library/pkg/api/recipes"
20
19
"github.com/iplay88keys/my-recipe-library/pkg/api/users"
20
+ "github.com/iplay88keys/my-recipe-library/pkg/config"
21
21
"github.com/iplay88keys/my-recipe-library/pkg/repositories"
22
22
"github.com/iplay88keys/my-recipe-library/pkg/token"
23
23
24
24
_ "github.com/go-sql-driver/mysql"
25
25
)
26
26
27
- type Config struct {
28
- DatabaseCreds DBCreds `env:"DATABASE_CREDS, required"`
29
- RedisURL string `env:"REDIS_URL, required"`
30
- AccessSecret string `env:"ACCESS_SECRET, required"`
31
- RefreshSecret string `env:"REFRESH_SECRET, required"`
32
- Port string `env:"PORT"`
33
- Static string `env:"STATIC_DIR"`
34
- }
35
-
36
- type DBCreds struct {
37
- URL string `json:"url"`
38
- InstanceName string `json:"gcloud_instance_name"`
39
- DBName string `json:"gcloud_db_name"`
40
- User string `json:"gcloud_user"`
41
- Password string `json:"gcloud_password"`
42
- }
43
-
44
- func (d * DBCreds ) UnmarshalEnv (data string ) error {
45
- return json .Unmarshal ([]byte (data ), d )
46
- }
47
-
48
27
func main () {
49
- config := Config {
28
+ cfg := config. Config {
50
29
Port : "8080" ,
51
30
Static : "ui/build" ,
52
31
}
53
32
54
- err := envstruct .Load (& config )
33
+ err := envstruct .Load (& cfg )
55
34
if err != nil {
56
35
panic (err )
57
36
}
58
37
59
- db , err := connectToMySQL (& config )
38
+ db , err := connectToMySQL (& config. MySQLCreds {} )
60
39
if err != nil {
61
40
panic (err )
62
41
}
63
42
64
- redisClient , err := connectToRedis (config .RedisURL )
43
+ redisClient , err := connectToRedis (cfg .RedisURL )
65
44
if err != nil {
66
45
panic (err )
67
46
}
@@ -72,11 +51,11 @@ func main() {
72
51
usersRepo := repositories .NewUsersRepository (db )
73
52
74
53
redisRepo := repositories .NewRedisRepository (redisClient )
75
- tokenService := token .NewService (config .AccessSecret , config .RefreshSecret )
54
+ tokenService := token .NewService (cfg .AccessSecret , cfg .RefreshSecret )
76
55
77
56
a := api .New (& api.Config {
78
- Port : config .Port ,
79
- StaticDir : config .Static ,
57
+ Port : cfg .Port ,
58
+ StaticDir : cfg .Static ,
80
59
Validate : tokenService .ValidateToken ,
81
60
RetrieveAccessDetails : redisRepo .RetrieveTokenDetails ,
82
61
Endpoints : []* api.Endpoint {
@@ -104,8 +83,7 @@ func main() {
104
83
},
105
84
})
106
85
107
- fmt .Printf ("Serving at http://localhost:%s\n " , config .Port )
108
- fmt .Println ("ctrl-c to quit" )
86
+ fmt .Printf ("Serving at http://localhost:%s\n " , cfg .Port )
109
87
stopApi := a .Start ()
110
88
111
89
defer stopApi ()
@@ -115,10 +93,10 @@ func main() {
115
93
blockUntilSigterm ()
116
94
}
117
95
118
- func connectToMySQL (config * Config ) (db * sql.DB , err error ) {
119
- if config .DatabaseCreds . URL != "" {
96
+ func connectToMySQL (config * config. MySQLCreds ) (db * sql.DB , err error ) {
97
+ if config .URL != "" {
120
98
var unquotedURL string
121
- url := config .DatabaseCreds . URL
99
+ url := config .URL
122
100
123
101
unquotedURL , err = strconv .Unquote (url )
124
102
if err == nil {
@@ -131,8 +109,8 @@ func connectToMySQL(config *Config) (db *sql.DB, err error) {
131
109
}
132
110
133
111
} else {
134
- cfg := mysql .Cfg (config .DatabaseCreds . InstanceName , config .DatabaseCreds . User , config . DatabaseCreds .Password )
135
- cfg .DBName = config .DatabaseCreds . DBName
112
+ cfg := mysql .Cfg (config .InstanceName , config .User , config .Password )
113
+ cfg .DBName = config .DBName
136
114
137
115
db , err = mysql .DialCfg (cfg )
138
116
if err != nil {
0 commit comments