package rabbitmq
import (
"cmp"
"errors"
"os"
"strconv"
)
// Config is a type that defines required data for connecting to RabbitMQ server
type Config struct {
host string
port int
user string
password string
ConnectionString string
}
// NewConfig is the function that validates and returns Config instance
func NewConfig() (*Config, error) {
config := new(Config)
// Get host from RABBITMQ_HOST env variable
config.host = cmp.Or(os.Getenv("RABBITMQ_HOST"), "localhost")
// Get user from RABBITMQ_USER env variable
config.user = cmp.Or(os.Getenv("RABBITMQ_USER"), "guest")
// Get password from RABBITMQ_PASSWORD env variable
config.password = cmp.Or(os.Getenv("RABBITMQ_PASSWORD"), "guest")
// Get port from RABBITMQ_PORT env variable and validate its value
var portAtoiErr error
config.port, portAtoiErr = strconv.Atoi(cmp.Or(os.Getenv("RABBITMQ_PORT"), "5672"))
if portAtoiErr != nil {
return config, portAtoiErr
}
if config.port <= 0 || config.port >= 65536 {
return config, errors.New("RabbitMQ port value must be between 1 and 65535")
}
config.ConnectionString = "amqp://" + config.user + ":" + config.password + "@" + config.host + ":" + strconv.Itoa(config.port) + "/"
return config, nil
}
package redis
import (
"cmp"
"errors"
"os"
"strconv"
)
// Config is a type that defines required data for connecting to Redis server
type Config struct {
Host string
Port int
Password string
Database int
}
// NewConfig is the function that validates and returns Config instance
func NewConfig() (*Config, error) {
config := Config{}
// Get host from REDIS_HOST env variable
config.Host = cmp.Or(os.Getenv("REDIS_HOST"), "localhost")
// Get port from REDIS_PORT env variable
var portAtoiErr error
config.Port, portAtoiErr = strconv.Atoi(cmp.Or(os.Getenv("REDIS_PORT"), "6379"))
if portAtoiErr != nil {
return nil, portAtoiErr
}
if config.Port <= 0 || config.Port >= 65536 {
return nil, errors.New("Redis port value must be between 1 and 65535")
}
// Get database from REDIS_DATABASE env variable
var databaseAtoiErr error
config.Database, databaseAtoiErr = strconv.Atoi(cmp.Or(os.Getenv("REDIS_DATABASE"), "0"))
if databaseAtoiErr != nil {
return nil, databaseAtoiErr
}
if config.Database < 0 {
return nil, errors.New("Redis database value must be a positive integer")
}
// Get password from REDIS_PASSWORD env variable
config.Password = cmp.Or(os.Getenv("REDIS_PASSWORD"), "")
return &config, nil
}