feat: add support for using TLS to connect to Redis
This commit is contained in:
@@ -149,6 +149,8 @@ func RunCLI() error {
|
||||
fmt.Println(" Invalid Redis DB. Must be between 0 and 15.")
|
||||
}
|
||||
|
||||
cfg.Redis.EnableTLS = promptConfirm(reader, "Enable Redis TLS?")
|
||||
|
||||
fmt.Println()
|
||||
fmt.Print("Testing Redis connection... ")
|
||||
if err := TestRedisConnection(&cfg.Redis); err != nil {
|
||||
@@ -205,6 +207,7 @@ func RunCLI() error {
|
||||
fmt.Println("── Configuration Summary ──")
|
||||
fmt.Printf("Database: %s@%s:%d/%s\n", cfg.Database.User, cfg.Database.Host, cfg.Database.Port, cfg.Database.DBName)
|
||||
fmt.Printf("Redis: %s:%d\n", cfg.Redis.Host, cfg.Redis.Port)
|
||||
fmt.Printf("Redis TLS: %s\n", map[bool]string{true: "enabled", false: "disabled"}[cfg.Redis.EnableTLS])
|
||||
fmt.Printf("Admin: %s\n", cfg.Admin.Email)
|
||||
fmt.Printf("Server: :%d\n", cfg.Server.Port)
|
||||
fmt.Println()
|
||||
|
||||
@@ -176,10 +176,11 @@ func testDatabase(c *gin.Context) {
|
||||
|
||||
// TestRedisRequest represents Redis test request
|
||||
type TestRedisRequest struct {
|
||||
Host string `json:"host" binding:"required"`
|
||||
Port int `json:"port" binding:"required"`
|
||||
Password string `json:"password"`
|
||||
DB int `json:"db"`
|
||||
Host string `json:"host" binding:"required"`
|
||||
Port int `json:"port" binding:"required"`
|
||||
Password string `json:"password"`
|
||||
DB int `json:"db"`
|
||||
EnableTLS bool `json:"enable_tls"`
|
||||
}
|
||||
|
||||
// testRedis tests Redis connection
|
||||
@@ -205,10 +206,11 @@ func testRedis(c *gin.Context) {
|
||||
}
|
||||
|
||||
cfg := &RedisConfig{
|
||||
Host: req.Host,
|
||||
Port: req.Port,
|
||||
Password: req.Password,
|
||||
DB: req.DB,
|
||||
Host: req.Host,
|
||||
Port: req.Port,
|
||||
Password: req.Password,
|
||||
DB: req.DB,
|
||||
EnableTLS: req.EnableTLS,
|
||||
}
|
||||
|
||||
if err := TestRedisConnection(cfg); err != nil {
|
||||
|
||||
@@ -3,6 +3,7 @@ package setup
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"crypto/tls"
|
||||
"database/sql"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
@@ -79,10 +80,11 @@ type DatabaseConfig struct {
|
||||
}
|
||||
|
||||
type RedisConfig struct {
|
||||
Host string `json:"host" yaml:"host"`
|
||||
Port int `json:"port" yaml:"port"`
|
||||
Password string `json:"password" yaml:"password"`
|
||||
DB int `json:"db" yaml:"db"`
|
||||
Host string `json:"host" yaml:"host"`
|
||||
Port int `json:"port" yaml:"port"`
|
||||
Password string `json:"password" yaml:"password"`
|
||||
DB int `json:"db" yaml:"db"`
|
||||
EnableTLS bool `json:"enable_tls" yaml:"enable_tls"`
|
||||
}
|
||||
|
||||
type AdminConfig struct {
|
||||
@@ -199,11 +201,20 @@ func TestDatabaseConnection(cfg *DatabaseConfig) error {
|
||||
|
||||
// TestRedisConnection tests the Redis connection
|
||||
func TestRedisConnection(cfg *RedisConfig) error {
|
||||
rdb := redis.NewClient(&redis.Options{
|
||||
opts := &redis.Options{
|
||||
Addr: fmt.Sprintf("%s:%d", cfg.Host, cfg.Port),
|
||||
Password: cfg.Password,
|
||||
DB: cfg.DB,
|
||||
})
|
||||
}
|
||||
|
||||
if cfg.EnableTLS {
|
||||
opts.TLSConfig = &tls.Config{
|
||||
MinVersion: tls.VersionTLS12,
|
||||
ServerName: cfg.Host,
|
||||
}
|
||||
}
|
||||
|
||||
rdb := redis.NewClient(opts)
|
||||
defer func() {
|
||||
if err := rdb.Close(); err != nil {
|
||||
log.Printf("failed to close redis client: %v", err)
|
||||
@@ -485,10 +496,11 @@ func AutoSetupFromEnv() error {
|
||||
SSLMode: getEnvOrDefault("DATABASE_SSLMODE", "disable"),
|
||||
},
|
||||
Redis: RedisConfig{
|
||||
Host: getEnvOrDefault("REDIS_HOST", "localhost"),
|
||||
Port: getEnvIntOrDefault("REDIS_PORT", 6379),
|
||||
Password: getEnvOrDefault("REDIS_PASSWORD", ""),
|
||||
DB: getEnvIntOrDefault("REDIS_DB", 0),
|
||||
Host: getEnvOrDefault("REDIS_HOST", "localhost"),
|
||||
Port: getEnvIntOrDefault("REDIS_PORT", 6379),
|
||||
Password: getEnvOrDefault("REDIS_PASSWORD", ""),
|
||||
DB: getEnvIntOrDefault("REDIS_DB", 0),
|
||||
EnableTLS: getEnvOrDefault("REDIS_ENABLE_TLS", "false") == "true",
|
||||
},
|
||||
Admin: AdminConfig{
|
||||
Email: getEnvOrDefault("ADMIN_EMAIL", "admin@sub2api.local"),
|
||||
|
||||
Reference in New Issue
Block a user