-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
101 lines (86 loc) · 2.28 KB
/
config.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
package config
import (
"log"
"os"
"strconv"
"github.com/cloudinary/cloudinary-go/v2"
"github.com/gorilla/websocket"
"github.com/raihan2bd/vidverse/initializers"
"github.com/raihan2bd/vidverse/internal/mail"
"github.com/raihan2bd/vidverse/repository"
dbrepo "github.com/raihan2bd/vidverse/repository/dbRepo"
"gorm.io/gorm"
"context"
firebase "firebase.google.com/go"
"google.golang.org/api/option"
)
type Application struct {
DB *gorm.DB
CLD *cloudinary.Cloudinary
DBMethods repository.DatabaseRepo
NotificationChan chan *NotificationEvent
Mailer mail.Mail
FirebaseApp *firebase.App
}
type NotificationEvent struct {
BroadcasterID uint
Action string
Data interface{}
Conn *websocket.Conn
}
func LoadConfig() (*Application, error) {
var (
cld *cloudinary.Cloudinary
db *gorm.DB
err error
)
db, err = initializers.ConnectToDB()
if err != nil {
return nil, err
}
cld, err = initializers.ConnectToCloudinary()
if err != nil {
return nil, err
}
err = initializers.SyncDatabase()
if err != nil {
return nil, err
}
mailPort, _ := strconv.Atoi(os.Getenv("MAIL_PORT"))
m := mail.Mail{
Domain: os.Getenv("MAIL_DOMAIN"),
Host: os.Getenv("MAIL_HOST"),
Port: mailPort,
Username: os.Getenv("MAIL_USERNAME"),
Password: os.Getenv("MAIL_PASSWORD"),
Encryption: os.Getenv("MAIL_ENCRIPTION"),
FromName: os.Getenv("MAIL_FROM_NAME"),
FromAddress: os.Getenv("MAIL_FROM_ADDRESS"),
}
ctx := context.Background()
// Initialize Firebase and set it in the config.
app, err := initFirebase(ctx)
if err != nil {
log.Fatalf("error initializing Firebase: %v", err)
}
return &Application{
DB: db,
DBMethods: dbrepo.NewPostgresRepo(initializers.DB, initializers.CLD),
CLD: cld,
NotificationChan: make(chan *NotificationEvent),
Mailer: m,
FirebaseApp: app,
}, nil
}
func initFirebase(ctx context.Context) (*firebase.App, error) {
// Initialize the Firebase Admin SDK.
firebaseConfig := &firebase.Config{
ProjectID: "vidverse-21e69",
}
opt := option.WithCredentialsFile("./service-account.json")
app, err := firebase.NewApp(ctx, firebaseConfig, opt)
if err != nil {
return nil, err
}
return app, nil
}