-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
159 lines (144 loc) · 4.18 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"os"
//"context"
//"math/rand"
//"time"
// External
"github.com/joho/godotenv"
"github.com/rodaxx/ecommerce-catalogue/config"
"github.com/rodaxx/ecommerce-catalogue/database"
"github.com/rodaxx/ecommerce-catalogue/server"
"github.com/rodaxx/ecommerce-catalogue/utils"
)
type MessagePattern struct {
Pattern struct {
Cmd string `json:"cmd"`
} `json:"pattern"`
Data interface{} `json:"data"`
ID string `json:"id"`
}
func main() {
err := godotenv.Load(".env")
if err != nil {
log.Fatal("Error loading .env file")
}
PORT := os.Getenv("PORT")
DATABASE_URL := os.Getenv("DATABASE_URL")
repo,err:= database.NewMySQLRepository(DATABASE_URL)
if err!= nil {
log.Fatal(err)
}
server:= server.NewCatalogueServer(repo)
if err!= nil {
log.Fatal(err)
}
log.Println("Starting server on port", PORT)
config.SetupRabbitMQ()
// Procesar mensajes
forever := make(chan bool)
//consumir los mensajes de la cola
messages, err := config.RMQChannel.Consume(
config.RMQQueue.Name, // Nombre de la cola
"", // Nombre del consumidor
true, // Auto-ack (auto acknowledge)
false, // Exclusiva
false, // No-local
false, // No-wait
nil, // Argumentos adicionales
)
if err != nil {
log.Fatal(err)
}
go func() {
for msg := range messages {
// Convertir el cuerpo del mensaje a la estructura Mensaje
var message MessagePattern
if err := json.Unmarshal(msg.Body, &message); err != nil {
log.Printf("Error al decodificar el mensaje: %v", err)
continue
}
switch message.Pattern.Cmd {
case "FindAllProducts":
products,err:=server.GetProducts(context.Background())
if err != nil {
fmt.Println("Error al obtener productos:", err)
return
}
resultJSON, err := json.MarshalIndent(products, "", " ")
if err != nil {
fmt.Println("Error al convertir a JSON:", err)
return
}
utils.SendResponse(config.RMQChannel, msg, string(resultJSON));
case "GetProductById":
productID, ok := message.Data.(map[string]interface{})["productID"].(float64)
if !ok {
fmt.Println("Error: productID no encontrado o no es un número")
return
}
// Obtener el producto por ID
product, err := server.GetProductById(context.Background(),uint(productID))
if err != nil {
fmt.Println("Error al obtener el producto por ID:", err)
return
}
resultJSON, err := json.MarshalIndent(product, "", " ")
if err != nil {
fmt.Println("Error al convertir a JSON:", err)
return
}
utils.SendResponse(config.RMQChannel, msg, string(resultJSON));
case "Purchase":
productID, ok := message.Data.(map[string]interface{})["productID"].(float64)
if !ok {
fmt.Println("Error: productID no encontrado o no es un número")
return
}
newQuantity, ok := message.Data.(map[string]interface{})["newQuantity"].(float64)
if !ok {
fmt.Println("Error: newQuantity no encontrado o no es un número")
return
}
err=server.UpdateProductById(context.Background(),uint(productID),uint(newQuantity))
if err != nil{
log.Fatal(err)
}
product, err := server.GetProductById(context.Background(),uint(productID))
if err != nil {
fmt.Println("Error al obtener el producto por ID:", err)
return
}
resultJSON, err := json.MarshalIndent(product, "", " ")
if err != nil {
fmt.Println("Error al convertir a JSON:", err)
return
}
utils.SendResponse(config.RMQChannel, msg, string(resultJSON));
case "FindProductsForBranch":
branchName, ok := message.Data.(map[string]interface{})["branchName"].(string)
if !ok {
fmt.Println("Error: branchName no encontrado")
return
}
products,err:=server.GetProductsForBranch(context.Background(),branchName)
if err != nil {
fmt.Println("Error al obtener productos:", err)
return
}
resultJSON, err := json.MarshalIndent(products, "", " ")
if err != nil {
fmt.Println("Error al convertir a JSON:", err)
return
}
utils.SendResponse(config.RMQChannel, msg, string(resultJSON));
}
}
}()
fmt.Println("Microservicio escuchando. Presiona CTRL+C para salir.")
<-forever
}