-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
168 lines (149 loc) · 4.88 KB
/
main.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package main
import (
"context"
"log"
"os"
"reflect"
"time"
"github.com/echovl/orderflo-dev/cloud/github"
"github.com/echovl/orderflo-dev/cloud/google"
"github.com/echovl/orderflo-dev/db/mongodb"
"github.com/echovl/orderflo-dev/db/mysql"
"github.com/echovl/orderflo-dev/db/redis"
"github.com/echovl/orderflo-dev/feeds/pexels"
"github.com/echovl/orderflo-dev/feeds/pixabay"
"github.com/echovl/orderflo-dev/http"
"github.com/echovl/orderflo-dev/layerhub"
"github.com/echovl/orderflo-dev/payments/paypal"
"github.com/echovl/orderflo-dev/upload/s3"
"github.com/spf13/viper"
"go.uber.org/zap"
)
type Config struct {
Port string `mapstructure:"PORT"`
RedisAddr string `mapstructure:"REDIS_ADDR"`
RedisUsername string `mapstructure:"REDIS_USERNAME"`
RedisPassword string `mapstructure:"REDIS_PASSWORD"`
MongoURL string `mapstructure:"MONGO_URL"`
MongoDBName string `mapstructure:"MONGO_DB_NAME"`
MySQLDSN string `mapstructure:"MYSQL_DSN"`
AWSAccessKeyID string `mapstructure:"AWS_ACCESS_KEY_ID"`
AWSSecretAccessKey string `mapstructure:"AWS_SECRET_ACCESS_KEY"`
AWSRegion string `mapstructure:"AWS_REGION"`
AWSBucket string `mapstructure:"AWS_BUCKET"`
PixabayKey string `mapstructure:"PIXABAY_API_KEY"`
PexelsKey string `mapstructure:"PEXELS_API_KEY"`
PaypalClientID string `mapstructure:"PAYPAL_CLIENT_ID"`
PaypalSecret string `mapstructure:"PAYPAL_SECRET"`
CDNBase string `mapstructure:"CDN_BASE"`
RendererSocket string `mapstructure:"RENDERER_SOCKET"`
GithubClientID string `mapstructure:"GITHUB_CLIENT_ID"`
GithubClientSecret string `mapstructure:"GITHUB_CLIENT_SECRET"`
GithubRedirectURI string `mapstructure:"GITHUB_REDIRECT_URI"`
GoogleClientID string `mapstructure:"GOOGLE_CLIENT_ID"`
GoogleClientSecret string `mapstructure:"GOOGLE_CLIENT_SECRET"`
GoogleRedirectURI string `mapstructure:"GOOGLE_REDIRECT_URI"`
}
func loadConfig(path string) (Config, error) {
var config Config
viper.AddConfigPath(path)
viper.SetConfigName("app")
viper.SetConfigType("env")
viper.AutomaticEnv()
viper.ReadInConfig()
if err := unmarshalConfig(&config); err != nil {
return config, err
}
// Load credentials for aws-sdk
os.Setenv("AWS_ACCESS_KEY_ID", config.AWSAccessKeyID)
os.Setenv("AWS_SECRET_ACCESS_KEY", config.AWSSecretAccessKey)
return config, nil
}
func unmarshalConfig(cfg *Config) error {
r := reflect.TypeOf(cfg).Elem()
for i := 0; i < r.NumField(); i++ {
env := r.Field(i).Tag.Get("mapstructure")
if err := viper.BindEnv(env); err != nil {
return err
}
}
return viper.Unmarshal(cfg)
}
func main() {
config, err := loadConfig(".")
if err != nil {
log.Panic(err)
}
logger, _ := zap.NewProduction()
defer logger.Sync()
logger.Sugar().Infof("%+v", config)
uploader, err := s3.New(config.AWSRegion, config.AWSBucket, config.CDNBase)
if err != nil {
log.Panic(err)
}
layerhub.RunRenderer(logger.Sugar())
pixabayFeed := pixabay.NewImageFeed(config.PixabayKey)
pexelsFeed := pexels.NewImageFeed(config.PixabayKey)
paymentProvider := paypal.NewPaymentProvider(config.PaypalClientID, config.PaypalSecret)
renderer := layerhub.NewRenderer(config.RendererSocket, logger.Sugar(), uploader)
githubClient := github.NewClient(github.Config{
ClientID: config.GithubClientID,
Secret: config.GithubClientSecret,
RedirectURI: config.GithubRedirectURI,
})
googleClient := google.NewClient(google.Config{
ClientID: config.GoogleClientID,
Secret: config.GoogleClientSecret,
RedirectURI: config.GoogleRedirectURI,
})
mysqlDB, err := mysql.New(&mysql.Config{
DSN: config.MySQLDSN,
ConnMaxIdleTime: 15 * time.Minute,
MaxOpenConns: 10,
MaxIdleConns: 5,
})
if err != nil {
log.Panic(err)
}
mongoDB, err := mongodb.New(&mongodb.Config{
URI: config.MongoURL,
DB: config.MongoDBName,
})
if err != nil {
log.Panic(err)
}
defer mongoDB.Close(context.TODO())
redisClient, err := redis.New(&redis.Config{
Addr: config.RedisAddr,
Username: config.RedisUsername,
Password: config.RedisPassword,
ReadTimeout: 5 * time.Second,
WriteTimeout: 5 * time.Second,
IdleTimeout: 5 * time.Minute,
})
if err != nil {
log.Panic(err)
}
defer redisClient.Close(context.TODO())
server := http.NewServer(http.Config{
Core: layerhub.New(layerhub.CoreConfig{
Logger: logger,
DB: mysqlDB,
JSONDB: mongoDB,
Uploader: uploader,
Pixabay: pixabayFeed,
Pexels: pexelsFeed,
PaymentProvider: paymentProvider,
Renderer: renderer,
GithubClient: githubClient,
GoogleClient: googleClient,
}),
SessionDB: redisClient,
ReadTimeout: 15 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 15 * time.Minute,
})
if err := server.ListenAndServe(":" + config.Port); err != nil {
log.Panic(err)
}
}