mirror of https://github.com/cloudreve/Cloudreve
feat(database): add support for SSL connections and database URL configuration
parent
fec549f5ec
commit
72ec062cb0
|
@ -58,6 +58,16 @@ func NewRawEntClient(l logging.Logger, config conf.ConfigProvider) (*ent.Client,
|
|||
client *sql.Driver
|
||||
)
|
||||
|
||||
// Check if the database type is supported.
|
||||
if confDBType != conf.SQLiteDB && confDBType != conf.MySqlDB && confDBType != conf.PostgresDB {
|
||||
return nil, fmt.Errorf("unsupported database type: %s", confDBType)
|
||||
}
|
||||
// If Database connection string provided, use it directly.
|
||||
if dbConfig.DatabaseURL != "" {
|
||||
l.Info("Connect to database with connection string %q.", dbConfig.DatabaseURL)
|
||||
client, err = sql.Open(string(confDBType), dbConfig.DatabaseURL)
|
||||
} else {
|
||||
|
||||
switch confDBType {
|
||||
case conf.SQLiteDB:
|
||||
dbFile := util.RelativePath(dbConfig.DBFile)
|
||||
|
@ -65,7 +75,7 @@ func NewRawEntClient(l logging.Logger, config conf.ConfigProvider) (*ent.Client,
|
|||
client, err = sql.Open("sqlite3", util.RelativePath(dbConfig.DBFile))
|
||||
case conf.PostgresDB:
|
||||
l.Info("Connect to Postgres database %q.", dbConfig.Host)
|
||||
client, err = sql.Open("postgres", fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=disable",
|
||||
client, err = sql.Open("postgres", fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=allow",
|
||||
dbConfig.Host,
|
||||
dbConfig.User,
|
||||
dbConfig.Password,
|
||||
|
@ -97,6 +107,7 @@ func NewRawEntClient(l logging.Logger, config conf.ConfigProvider) (*ent.Client,
|
|||
return nil, fmt.Errorf("failed to open database: %w", err)
|
||||
}
|
||||
|
||||
}
|
||||
// Set connection pool
|
||||
db := client.DB()
|
||||
db.SetMaxIdleConns(50)
|
||||
|
|
|
@ -3,6 +3,7 @@ package cache
|
|||
import (
|
||||
"bytes"
|
||||
"encoding/gob"
|
||||
"github.com/cloudreve/Cloudreve/v4/pkg/conf"
|
||||
"github.com/cloudreve/Cloudreve/v4/pkg/logging"
|
||||
"strconv"
|
||||
"time"
|
||||
|
@ -44,7 +45,8 @@ func deserializer(value []byte) (any, error) {
|
|||
}
|
||||
|
||||
// NewRedisStore 创建新的redis存储
|
||||
func NewRedisStore(l logging.Logger, size int, network, address, user, password, database string) *RedisStore {
|
||||
func NewRedisStore(l logging.Logger, size int, configProvider conf.ConfigProvider) *RedisStore {
|
||||
redisConfig := configProvider.Redis()
|
||||
return &RedisStore{
|
||||
pool: &redis.Pool{
|
||||
MaxIdle: size,
|
||||
|
@ -54,17 +56,19 @@ func NewRedisStore(l logging.Logger, size int, network, address, user, password,
|
|||
return err
|
||||
},
|
||||
Dial: func() (redis.Conn, error) {
|
||||
db, err := strconv.Atoi(database)
|
||||
db, err := strconv.Atoi(redisConfig.DB)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
c, err := redis.Dial(
|
||||
network,
|
||||
address,
|
||||
redisConfig.Network,
|
||||
redisConfig.Server,
|
||||
redis.DialDatabase(db),
|
||||
redis.DialPassword(password),
|
||||
redis.DialUsername(user),
|
||||
redis.DialPassword(redisConfig.Password),
|
||||
redis.DialUsername(redisConfig.User),
|
||||
redis.DialUseTLS(redisConfig.UseSSL),
|
||||
redis.DialTLSSkipVerify(redisConfig.TLSSkipVerify),
|
||||
)
|
||||
if err != nil {
|
||||
l.Panic("Failed to create Redis connection: %s", err)
|
||||
|
|
|
@ -24,6 +24,10 @@ type Database struct {
|
|||
Port int
|
||||
Charset string
|
||||
UnixSocket bool
|
||||
// 允许直接使用DATABASE_URL来配置数据库连接
|
||||
DatabaseURL string
|
||||
// SSLMode 允许使用SSL连接数据库, 用户可以在sslmode string中添加证书等配置
|
||||
SSLMode string
|
||||
}
|
||||
|
||||
type SysMode string
|
||||
|
@ -70,6 +74,8 @@ type Redis struct {
|
|||
User string
|
||||
Password string
|
||||
DB string
|
||||
UseSSL bool
|
||||
TLSSkipVerify bool
|
||||
}
|
||||
|
||||
// 跨域配置
|
||||
|
@ -89,6 +95,8 @@ var RedisConfig = &Redis{
|
|||
Server: "",
|
||||
Password: "",
|
||||
DB: "0",
|
||||
UseSSL: false,
|
||||
TLSSkipVerify: true,
|
||||
}
|
||||
|
||||
// DatabaseConfig 数据库配置
|
||||
|
@ -97,6 +105,8 @@ var DatabaseConfig = &Database{
|
|||
DBFile: util.DataPath("cloudreve.db"),
|
||||
Port: 3306,
|
||||
UnixSocket: false,
|
||||
DatabaseURL: util.DataPath(""),
|
||||
SSLMode: "allow",
|
||||
}
|
||||
|
||||
// SystemConfig 系统公用配置
|
||||
|
|
Loading…
Reference in New Issue