2021-10-27 05:10:29 +02:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2021-11-18 00:11:28 +01:00
|
|
|
"context"
|
|
|
|
"errors"
|
2021-10-27 05:10:29 +02:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2021-11-18 00:11:28 +01:00
|
|
|
"github.com/go-redis/redis/v8"
|
|
|
|
|
2021-10-31 00:20:41 +02:00
|
|
|
mConfig "github.com/RichardKnop/machinery/v1/config"
|
2021-10-27 05:10:29 +02:00
|
|
|
"github.com/google/uuid"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
ServerHostname = "server.hostname"
|
|
|
|
DatabaseHost = "database.host"
|
|
|
|
DatabaseName = "database.name"
|
|
|
|
DatabaseUser = "database.user"
|
|
|
|
DatabasePassword = "database.password"
|
|
|
|
DatabasePort = "database.port"
|
|
|
|
DatabaseSslMode = "database.sslmode"
|
|
|
|
|
|
|
|
SecurityTokenExpiration = "security.token_expiration"
|
|
|
|
SecuritySecret = "security.secret"
|
|
|
|
|
2021-10-31 00:20:41 +02:00
|
|
|
JobEnabled = "job.enabled"
|
|
|
|
JobBroker = "job.broker"
|
|
|
|
JobStore = "job.store"
|
|
|
|
JobQueueName = "job.queue_name"
|
2021-10-27 05:10:29 +02:00
|
|
|
|
2021-11-18 00:11:28 +01:00
|
|
|
MessageQueue = "message.queue"
|
|
|
|
|
2021-10-27 05:10:29 +02:00
|
|
|
SmtpFrom = "smtp.from"
|
|
|
|
SmtpHost = "smtp.host"
|
|
|
|
SmtpPort = "smtp.port"
|
|
|
|
SmtpUsername = "smtp.username"
|
|
|
|
SmtpPassword = "smtp.password"
|
|
|
|
SmtpSkipVerify = "false"
|
|
|
|
)
|
|
|
|
|
2021-10-31 00:20:41 +02:00
|
|
|
var defaults = map[string]interface{}{
|
2021-10-27 05:10:29 +02:00
|
|
|
ServerHostname: "0.0.0.0:3333",
|
|
|
|
DatabaseHost: "127.0.0.1",
|
|
|
|
DatabaseName: "taskcafe",
|
|
|
|
DatabaseUser: "taskcafe",
|
|
|
|
DatabasePassword: "taskcafe_test",
|
|
|
|
DatabasePort: "5432",
|
|
|
|
DatabaseSslMode: "disable",
|
|
|
|
SecurityTokenExpiration: "15m",
|
|
|
|
SecuritySecret: "",
|
2021-11-18 00:11:28 +01:00
|
|
|
MessageQueue: "localhost:6379",
|
2021-10-31 00:20:41 +02:00
|
|
|
JobEnabled: false,
|
2021-11-18 00:11:28 +01:00
|
|
|
JobBroker: "redis://localhost:6379",
|
|
|
|
JobStore: "redis://localhost:6379",
|
2021-10-31 00:20:41 +02:00
|
|
|
JobQueueName: "taskcafe_tasks",
|
2021-10-27 05:10:29 +02:00
|
|
|
SmtpFrom: "no-reply@example.com",
|
|
|
|
SmtpHost: "localhost",
|
|
|
|
SmtpPort: "587",
|
|
|
|
SmtpUsername: "",
|
|
|
|
SmtpPassword: "",
|
2021-10-31 00:20:41 +02:00
|
|
|
SmtpSkipVerify: false,
|
2021-10-27 05:10:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func InitDefaults() {
|
|
|
|
for key, value := range defaults {
|
|
|
|
viper.SetDefault(key, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type AppConfig struct {
|
2021-11-18 00:11:28 +01:00
|
|
|
Email EmailConfig
|
|
|
|
Security SecurityConfig
|
|
|
|
Database DatabaseConfig
|
|
|
|
Job JobConfig
|
|
|
|
MessageQueue MessageQueueConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
type MessageQueueConfig struct {
|
|
|
|
URI string
|
2021-10-31 00:20:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type JobConfig struct {
|
|
|
|
Enabled bool
|
|
|
|
Broker string
|
|
|
|
QueueName string
|
|
|
|
Store string
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetJobConfig() JobConfig {
|
|
|
|
return JobConfig{
|
|
|
|
Enabled: viper.GetBool(JobEnabled),
|
|
|
|
Broker: viper.GetString(JobBroker),
|
|
|
|
QueueName: viper.GetString(JobQueueName),
|
|
|
|
Store: viper.GetString(JobStore),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cfg *JobConfig) GetJobConfig() mConfig.Config {
|
|
|
|
return mConfig.Config{
|
|
|
|
Broker: cfg.Broker,
|
|
|
|
DefaultQueue: cfg.QueueName,
|
|
|
|
ResultBackend: cfg.Store,
|
2021-11-18 00:11:28 +01:00
|
|
|
/*
|
|
|
|
AMQP: &mConfig.AMQPConfig{
|
|
|
|
Exchange: "machinery_exchange",
|
|
|
|
ExchangeType: "direct",
|
|
|
|
BindingKey: "machinery_task",
|
|
|
|
} */
|
2021-10-31 00:20:41 +02:00
|
|
|
}
|
2021-10-27 05:10:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type EmailConfig struct {
|
|
|
|
Host string
|
|
|
|
Port int
|
|
|
|
From string
|
|
|
|
Username string
|
|
|
|
Password string
|
|
|
|
SiteURL string
|
|
|
|
InsecureSkipVerify bool
|
|
|
|
}
|
|
|
|
|
2021-10-31 00:20:41 +02:00
|
|
|
type DatabaseConfig struct {
|
|
|
|
Host string
|
|
|
|
Port string
|
|
|
|
Name string
|
|
|
|
Username string
|
|
|
|
Password string
|
|
|
|
SslMode string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cfg DatabaseConfig) GetDatabaseConnectionUri() string {
|
|
|
|
connection := fmt.Sprintf("user=%s password=%s host=%s dbname=%s port=%s sslmode=%s",
|
|
|
|
cfg.Username,
|
|
|
|
cfg.Password,
|
|
|
|
cfg.Host,
|
|
|
|
cfg.Name,
|
|
|
|
cfg.Port,
|
|
|
|
cfg.SslMode,
|
|
|
|
)
|
|
|
|
return connection
|
|
|
|
}
|
|
|
|
|
2021-10-27 05:10:29 +02:00
|
|
|
type SecurityConfig struct {
|
|
|
|
AccessTokenExpiration time.Duration
|
|
|
|
Secret []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetAppConfig() (AppConfig, error) {
|
|
|
|
secret := viper.GetString(SecuritySecret)
|
|
|
|
if strings.TrimSpace(secret) == "" {
|
|
|
|
log.Warn("server.secret is not set, generating a random secret")
|
|
|
|
secret = uuid.New().String()
|
|
|
|
}
|
|
|
|
securityCfg, err := GetSecurityConfig(viper.GetString(SecurityTokenExpiration), []byte(secret))
|
|
|
|
if err != nil {
|
|
|
|
return AppConfig{}, err
|
|
|
|
}
|
2021-10-31 00:20:41 +02:00
|
|
|
jobCfg := GetJobConfig()
|
|
|
|
databaseCfg := GetDatabaseConfig()
|
2021-10-27 05:10:29 +02:00
|
|
|
emailCfg := GetEmailConfig()
|
2021-11-18 00:11:28 +01:00
|
|
|
messageCfg := MessageQueueConfig{URI: viper.GetString("message.queue")}
|
2021-10-27 05:10:29 +02:00
|
|
|
return AppConfig{
|
2021-11-18 00:11:28 +01:00
|
|
|
Email: emailCfg,
|
|
|
|
Security: securityCfg,
|
|
|
|
Database: databaseCfg,
|
|
|
|
Job: jobCfg,
|
|
|
|
MessageQueue: messageCfg,
|
|
|
|
}, err
|
2021-10-27 05:10:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetSecurityConfig(accessTokenExp string, secret []byte) (SecurityConfig, error) {
|
|
|
|
exp, err := time.ParseDuration(accessTokenExp)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("issue parsing duration")
|
|
|
|
return SecurityConfig{}, err
|
|
|
|
}
|
|
|
|
return SecurityConfig{AccessTokenExpiration: exp, Secret: secret}, nil
|
|
|
|
}
|
|
|
|
|
2021-11-18 00:11:28 +01:00
|
|
|
func (c MessageQueueConfig) GetMessageQueueClient() (*redis.Client, error) {
|
|
|
|
client := redis.NewClient(&redis.Options{
|
|
|
|
Addr: c.URI,
|
|
|
|
})
|
|
|
|
|
|
|
|
_, err := client.Ping(context.Background()).Result()
|
|
|
|
if !errors.Is(err, nil) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return client, nil
|
|
|
|
}
|
|
|
|
|
2021-10-27 05:10:29 +02:00
|
|
|
func GetEmailConfig() EmailConfig {
|
|
|
|
return EmailConfig{
|
2021-10-31 00:20:41 +02:00
|
|
|
From: viper.GetString(SmtpFrom),
|
|
|
|
Host: viper.GetString(SmtpHost),
|
|
|
|
Port: viper.GetInt(SmtpPort),
|
|
|
|
Username: viper.GetString(SmtpUsername),
|
|
|
|
Password: viper.GetString(SmtpPassword),
|
|
|
|
InsecureSkipVerify: viper.GetBool(SmtpSkipVerify),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetDatabaseConfig() DatabaseConfig {
|
|
|
|
return DatabaseConfig{
|
|
|
|
Username: viper.GetString(DatabaseUser),
|
|
|
|
Password: viper.GetString(DatabasePassword),
|
|
|
|
Port: viper.GetString(DatabasePort),
|
|
|
|
SslMode: viper.GetString(DatabaseSslMode),
|
|
|
|
Name: viper.GetString(DatabaseName),
|
|
|
|
Host: viper.GetString(DatabaseHost),
|
2021-10-27 05:10:29 +02:00
|
|
|
}
|
|
|
|
}
|