-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
117 lines (96 loc) · 3.38 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
package main
import (
"context"
"log"
"path/filepath"
"github.com/gin-contrib/multitemplate"
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/cookie"
"github.com/gin-gonic/gin"
addressHttpHandler "github.com/merembablas/catat-pendatang/address/delivery/http"
addressRepository "github.com/merembablas/catat-pendatang/address/repository"
addressUsecase "github.com/merembablas/catat-pendatang/address/usecase"
"github.com/merembablas/catat-pendatang/middleware"
peopleHttpHandler "github.com/merembablas/catat-pendatang/people/delivery/http"
peopleRepository "github.com/merembablas/catat-pendatang/people/repository"
peopleUsecase "github.com/merembablas/catat-pendatang/people/usecase"
userHttpHandler "github.com/merembablas/catat-pendatang/user/delivery/http"
userRepository "github.com/merembablas/catat-pendatang/user/repository"
userUsecase "github.com/merembablas/catat-pendatang/user/usecase"
"github.com/spf13/viper"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func init() {
viper.SetConfigFile(".env")
err := viper.ReadInConfig()
if err != nil {
log.Fatalf("Error while reading config file %s", err)
}
}
func main() {
mConn := getMongoClient()
ur := userRepository.NewMongoUserRepository(mConn)
ar := addressRepository.NewMongoAddressRepository(mConn)
pr := peopleRepository.NewMongoPeopleRepository(mConn)
uu := userUsecase.NewUserUsecase(ur)
au := addressUsecase.NewAddressUsecase(ar)
pu := peopleUsecase.NewPeopleUsecase(pr)
r := gin.Default()
middl := middleware.InitMiddleware()
store := cookie.NewStore([]byte(viper.GetString("SECRET")))
r.Use(middl.CORS)
r.Static("/assets", viper.GetString("STATIC_PATH"))
r.HTMLRender = loadTemplates(viper.GetString("TEMPLATE_PATH"))
r.Use(sessions.Sessions("data", store))
userHttpHandler.NewUserHandler(r, uu, middl)
addressHttpHandler.NewAddressHandler(r, au)
peopleHttpHandler.NewPeopleHandler(r, pu, middl)
r.Run(":8989")
}
func getMongoClient() *mongo.Client {
clientOptions := options.Client().ApplyURI(viper.GetString("MONGO_URI"))
client, err := mongo.NewClient(clientOptions)
if err != nil {
log.Fatal(err)
}
err = client.Connect(context.Background())
if err != nil {
log.Fatal(err)
}
return client
}
func loadTemplates(templatesDir string) multitemplate.Renderer {
r := multitemplate.NewRenderer()
publicLayouts, err := filepath.Glob(templatesDir + "/layouts/public-base.html")
if err != nil {
panic(err.Error())
}
publics, err := filepath.Glob(templatesDir + "/public/*.html")
if err != nil {
panic(err.Error())
}
// Generate our templates map from our publicLayouts/ and public/ directories
for _, public := range publics {
layoutCopy := make([]string, len(publicLayouts))
copy(layoutCopy, publicLayouts)
files := append(layoutCopy, public)
r.AddFromFiles(filepath.Base(public), files...)
}
dashboardLayouts, err := filepath.Glob(templatesDir + "/layouts/dashboard-base.html")
if err != nil {
panic(err.Error())
}
admins, err := filepath.Glob(templatesDir + "/dashboard/*.html")
if err != nil {
panic(err.Error())
}
// Generate our templates map from our dashboardLayouts/ and dashboard/ directories
for _, admin := range admins {
layoutCopy := make([]string, len(dashboardLayouts))
copy(layoutCopy, dashboardLayouts)
files := append(layoutCopy, admin)
r.AddFromFiles(filepath.Base(admin), files...)
}
return r
}