Skip to content

Instantly share code, notes, and snippets.

@pattanun-np
Created September 20, 2024 17:13
Show Gist options
  • Select an option

  • Save pattanun-np/522f5fe283d2ef8bc7802dce042ed71c to your computer and use it in GitHub Desktop.

Select an option

Save pattanun-np/522f5fe283d2ef8bc7802dce042ed71c to your computer and use it in GitHub Desktop.
package main
import (
"encoding/json"
"log"
"payment"
"github.com/streadway/amqp"
)
func main() {
conn, err := amqp.Dial("amqp://guest:guest@localhost:5672/")
if err != nil {
log.Fatalf("Failed to connect to RabbitMQ: %s", err)
}
defer conn.Close()
ch, err := conn.Channel()
if err != nil {
log.Fatalf("Failed to open a channel: %s", err)
}
defer ch.Close()
q, err := ch.QueueDeclare(
"payment_queue",
false,
false,
false,
false,
nil,
)
if err != nil {
log.Fatalf("Failed to declare a queue: %s", err)
}
msgs, err := ch.Consume(
q.Name,
"",
true,
false,
false,
false,
nil,
)
if err != nil {
log.Fatalf("Failed to register a consumer: %s", err)
}
paymentService := payment.PaymentService{} // เพิ่มการเชื่อมต่อกับฐานข้อมูลที่นี่
forever := make(chan bool)
go func() {
for d := range msgs {
var req payment.PaymentRequest
err := json.Unmarshal(d.Body, &req)
if err != nil {
log.Printf("Error decoding JSON: %s", err)
continue
}
err = paymentService.ProcessPayment(req)
if err != nil {
log.Printf("Error processing payment: %s", err)
}
}
}()
log.Printf(" [*] Waiting for messages. To exit press CTRL+C")
<-forever
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment