-
Notifications
You must be signed in to change notification settings - Fork 18
/
main.go
66 lines (53 loc) · 1.57 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
package main
import (
"crypto/tls"
"fmt"
"net"
"net/http"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/go-chi/render"
"github.com/irahardianto/monorepo-microservices/package/log"
"github.com/irahardianto/monorepo-microservices/showtimes/router"
"github.com/irahardianto/monorepo-microservices/showtimes/storage/mongodb"
"github.com/spf13/viper"
mgo "gopkg.in/mgo.v2"
)
func init() {
viper.AddConfigPath("config")
viper.SetConfigName("config")
viper.SetConfigType("yaml")
if err := viper.ReadInConfig(); err != nil {
log.Fatal("Error reading config file", err)
}
}
func main() {
//URI without ssl=true
var mongoURI = viper.GetString("database.atlasConnectionString")
dialInfo, err := mgo.ParseURL(mongoURI)
if err != nil {
panic(err)
}
tlsConfig := &tls.Config{}
tlsConfig.InsecureSkipVerify = true
dialInfo.DialServer = func(addr *mgo.ServerAddr) (net.Conn, error) {
conn, err := tls.Dial("tcp", addr.String(), tlsConfig)
return conn, err
}
session, err := mgo.DialWithInfo(dialInfo)
if err != nil {
log.Fatal("error while creating session", err)
}
s := &mongodb.Storage{session.DB(viper.GetString("database.mongoDbName"))}
defer session.Close()
r := chi.NewRouter()
r.Use(middleware.RequestID)
r.Use(middleware.Recoverer)
r.Use(middleware.Logger)
r.Use(middleware.URLFormat)
r.Use(render.SetContentType(render.ContentTypeJSON))
router := router.InitRouter(r, s)
if err := http.ListenAndServe(fmt.Sprintf(":%s", viper.GetString("server.port")), router); err != nil {
log.Fatal("error while serve http server", err)
}
}