Refactoring Factory RabbitMQ

This commit is contained in:
Mario
2022-06-04 17:34:03 +02:00
parent ab5a4f8137
commit caf95dc9da
3 changed files with 16 additions and 20 deletions

View File

@ -5,5 +5,5 @@ core:
logDisableTimestamp: true
logsPath: ./logs
tracing:
rabbitMQEnabled: true
rabbitMQEnabled: false
rabbitMQURI: ""

View File

@ -6,7 +6,7 @@ services:
#network_mode: host # Not work on Mac OS
container_name: beelzebub
restart: always
ports: # Remove me, if use configuration network_mode: host
ports: # Remove me, if you use configuration network_mode: host
- "22:22"
- "8080:8080"
- "80:80"
@ -16,13 +16,3 @@ services:
- rabbitmq
volumes:
- "./configurations:/configurations"
rabbitmq:
image: rabbitmq:3-management-alpine
container_name: 'rabbitmq'
ports:
- "5672:5672"
- "15672:15672"
environment:
RABBITMQ_DEFAULT_USER: ${RABBITMQ_DEFAULT_USER}
RABBITMQ_DEFAULT_PASS: ${RABBITMQ_DEFAULT_PASS}

20
main.go
View File

@ -20,7 +20,7 @@ func main() {
parser := parser.Init("./configurations/beelzebub.yaml", "./configurations/services/")
coreConfigurations, err := parser.ReadConfigurationsCore()
failOnError(err, fmt.Sprintf("Error during coreConfigurations: "))
failOnError(err, fmt.Sprintf("Error during ReadConfigurationsCore: "))
fileLogs := configureLoggingByConfigurations(coreConfigurations.Core.Logging)
defer fileLogs.Close()
@ -33,13 +33,9 @@ func main() {
if !configured {
rabbitMQURI = coreConfigurations.Core.Tracing.RabbitMQURI
}
conn, err := amqp.Dial(rabbitMQURI)
failOnError(err, "Failed to connect to RabbitMQ")
defer conn.Close()
channel, err = conn.Channel()
failOnError(err, "Failed to open a channel")
conn, channel := buildRabbitMQ(rabbitMQURI)
defer channel.Close()
defer conn.Close()
}
// Init Protocol strategies
@ -126,3 +122,13 @@ func configureLoggingByConfigurations(configurations parser.Logging) *os.File {
}
return file
}
func buildRabbitMQ(rabbitMQURI string) (*amqp.Connection, *amqp.Channel) {
conn, err := amqp.Dial(rabbitMQURI)
failOnError(err, "Failed to init Dial rabbitMQ")
channel, err := conn.Channel()
failOnError(err, "Failed to connect on Channel")
return conn, channel
}