-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
107 lines (90 loc) · 2.66 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
package main
import (
"accounting-service/api/handlers/transaction/pull"
"accounting-service/api/handlers/transaction/push"
"accounting-service/core/environment"
"accounting-service/core/services/channel"
"accounting-service/core/services/company"
"accounting-service/core/services/transaction"
"accounting-service/core/uuid"
companyEvents "accounting-service/events/handlers/company"
"accounting-service/store/kafka/consumer"
"accounting-service/store/kafka/producer"
"accounting-service/store/kafka/topics"
"accounting-service/store/postgres"
"accounting-service/store/redis"
"context"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"log"
"os"
)
// TODO: This main.go file will be moved to cmd/accounting
func main() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
// Getting app context
ctx := context.Background()
// Initializes available env
envs := environment.New(
os.Getenv("PORT"),
os.Getenv("REDIS_URL"),
os.Getenv("REDIS_PASSWORD"),
os.Getenv("DB_URL"),
os.Getenv("KAFKA_BROKER_URL"),
os.Getenv("KAFKA_GROUP_ID"),
)
// FYI: Will look for better way to handle dependency injection in Go
cache := redis.New(envs, ctx)
db := postgres.New(envs)
kafkaProducer := producer.New(envs)
// Migrates databases if they are not available
db.Migrate()
// Initialize services
channelService := channel.New(db)
// This creates one channel names mtn-momo
channelService.Seed()
transactionService := transaction.New(cache, db)
companyService := company.New(db)
// Kafka company event handler. This handles all Kafka requests related to companies
companyEventHandler := companyEvents.New(companyService, kafkaProducer)
kafkaTopics := topics.New(envs, companyEventHandler)
kafkaConsumer := consumer.New(envs, kafkaTopics)
/**
Consumer blocks, it's like an endless loop that is waiting for messages.
It is being initialized in Go routine for not to block everything else
*/
go kafkaConsumer.Consume()
// Initialise a UUID generator.
uuidGenerator := uuid.New()
// Transaction push controller
pushController := push.New(
envs,
transactionService,
channelService,
companyService,
uuidGenerator,
kafkaProducer,
)
// Transaction pull controller
pullController := pull.New(
envs,
transactionService,
channelService,
companyService,
uuidGenerator,
kafkaProducer,
)
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
})
r.POST("/api/v1/payment/push", pushController.HandleTransactionPushRequest)
r.POST("/api/v1/payment/pull", pullController.HandleTransactionPullRequest)
r.Static("/api-docs", "./swaggerui")
r.Run(":" + envs.Port)
}