Added debug message and rabbitmq connection

master
Alex 2021-06-16 21:43:11 +02:00
parent a459d74935
commit 56620852e7
3 changed files with 76 additions and 2 deletions

View File

@ -10,6 +10,7 @@ type Config struct {
Server server
Database database
Mail mail
RabbitMq rabbitmq
Settings settings
}
@ -30,6 +31,12 @@ type mail struct {
Key string
}
type rabbitmq struct {
Host string
Username string
Password string
}
type settings struct {
ExpiredTime int
UsernameMinLen int

View File

@ -22,7 +22,7 @@ func InitDatabase() {
sqlDB, err := sql.Open("mysql", getConnectionString())
if err != nil {
log.Fatalln("DB connection failed:", err)
log.Fatalln("Database connection failed:", err)
}
sqlDB.SetMaxIdleConns(10)
@ -33,8 +33,10 @@ func InitDatabase() {
}), &gorm.Config{})
if err != nil {
log.Fatalln("Failed to open gorm", err)
log.Fatalln("Failed to open gorm ", err)
}
DB = gormDB
log.Debugln("Database connected")
}

View File

@ -0,0 +1,65 @@
package rabbitmq
import (
"fmt"
"git.umbach.dev/app-idea/rest-api/modules/config"
log "github.com/sirupsen/logrus"
"github.com/streadway/amqp"
)
var Conn *amqp.Connection
func getConnectionString() string {
cfg := &config.Cfg.RabbitMq
return fmt.Sprintf("amqp://%s:%s@%s/", cfg.Username, cfg.Password, cfg.Host)
}
func Init() {
conn, err := amqp.Dial(getConnectionString())
if err != nil {
log.Fatalln("Failed to connect to RabbitMQ", err)
}
Conn = conn
ch, err := conn.Channel()
if err != nil {
log.Fatalln("Failed to open a channel", err)
}
defer ch.Close()
log.Debug("RabbitMQ connected")
q, err := ch.QueueDeclare(
"hello", // name
false, // durable
false, // delete when unused
false, // exclusive
false, // no-wait
nil, // arguments
)
if err != nil {
log.Fatalln("Failed to declare a queue", err)
}
body := "Hello World!"
err = ch.Publish(
"", // exchange
q.Name, // routing key
false, // mandatory
false, // immediate
amqp.Publishing{
ContentType: "text/plain",
Body: []byte(body),
})
if err != nil {
log.Fatalln("Failed to publish a message", err)
}
}