-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmain.go
137 lines (115 loc) · 3.48 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
package main
import (
"context"
"fmt"
"net/http"
"os"
"time"
"github.com/algolia/algoliasearch-client-go/v3/algolia/search"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/twreporter/go-mod-lib/pkg/cloudpub"
"github.com/twreporter/go-mod-lib/pkg/slack"
f "github.com/twreporter/logformatter"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
"github.com/twreporter/go-api/configs"
"github.com/twreporter/go-api/controllers"
"github.com/twreporter/go-api/globals"
member "github.com/twreporter/go-api/internal/member_cms"
"github.com/twreporter/go-api/internal/mongo"
"github.com/twreporter/go-api/routers"
"github.com/twreporter/go-api/services"
"github.com/twreporter/go-api/utils"
)
func main() {
var err error
var cf *controllers.ControllerFactory
defer func() {
if err != nil {
if globals.Conf.Environment == "development" {
log.Errorf("%+v", err)
} else {
log.WithField("detail", err).Errorf("%s", f.FormatStack(err))
}
}
}()
globals.Conf, err = configs.LoadConf("")
if err != nil {
err = errors.Wrap(err, "Fatal error config file")
return
}
configLogger()
// set up database connection
log.Info("Connecting to MySQL cloud")
db, err := utils.InitDB(10, 5)
defer db.Close()
if err != nil {
return
}
log.Info("Connecting to MongoDB replica")
session, err := utils.InitMongoDB()
defer session.Close()
if err != nil {
return
}
log.Info("Connection to MongoDB with mongo-go-driver")
ctx := context.Background()
opts := options.Client()
client, err := mongo.NewClient(ctx, opts.ApplyURI(globals.Conf.DB.Mongo.URL).SetReadPreference(readpref.Nearest()))
log.Info("Connecting to Member CMS")
if err := member.NewClient(); err != nil {
log.Infof("connecting to member cms failed. err: %+v", err)
}
if err != nil {
return
}
defer func() {
client.Disconnect(ctx)
}()
// init cloudpub client
pubConfig := &cloudpub.Config{
ProjectID: globals.Conf.Neticrm.ProjectID,
Topic: globals.Conf.Neticrm.Topic,
}
cloudpub.NewPublisher(ctx, pubConfig)
// init slack notify client
slackConfig := &slack.Config{
Webhook: globals.Conf.Neticrm.SlackWebhook,
}
slack.NewClient(slackConfig)
// mailSender := services.NewSMTPMailService() // use office365 to send mails
mailSvc := services.NewAmazonMailService() // use Amazon SES to send mails
sClient := search.NewClient(globals.Conf.Algolia.ApplicationID, globals.Conf.Algolia.APIKey)
cf = controllers.NewControllerFactory(db, session, mailSvc, client, sClient.InitIndex("contacts-index-v3"))
// set up the router
router := routers.SetupRouter(cf)
readTimeout := 5 * time.Second
// Set writeTimeout bigger than 30 secs.
// 30 secs is to ensure donation request is handled correctly.
writeTimeout := 40 * time.Second
s := &http.Server{
Addr: fmt.Sprintf(":%s", globals.LocalhostPort),
Handler: router,
ReadTimeout: readTimeout,
WriteTimeout: writeTimeout,
}
if err = s.ListenAndServe(); err != nil {
err = errors.Wrap(err, "Fail to start HTTP server")
}
return
}
func configLogger() {
env := globals.Conf.Environment
switch env {
// production/staging environments writes the log into standard output
// and delegates log collector (fluentd) in k8s cluster to export to
// stackdriver sink.
case "production", "staging":
log.SetOutput(os.Stdout)
log.SetFormatter(f.NewStackdriverFormatter("go-api", env))
// development environment reports the log location
default:
log.SetReportCaller(true)
}
}