Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,26 @@ REDIS_SENTINEL_USERNAME=
REDIS_SENTINEL_PASSWORD=
REDIS_SENTINEL_SOCKET_TIMEOUT=0.1

DB_USERNAME=postgres
DB_PASSWORD=difyai123456
DB_HOST=localhost
DB_PORT=5432
DB_TYPE=postgresql

# PostgreSQL configuration
POSTGRES_USER=postgres
POSTGRES_PASSWORD=difyai123456
POSTGRES_HOST=localhost
POSTGRES_PORT=5432

# MySQL configuration
MYSQL_USER=root
MYSQL_PASSWORD=difyai123456
MYSQL_HOST=localhost
MYSQL_PORT=3306

# OceanBase configuration
OCEANBASE_USER=root@test
OCEANBASE_PASSWORD=difyai123456
OCEANBASE_HOST=localhost
OCEANBASE_PORT=2881

DB_DATABASE=dify_plugin
# Specifies the SSL mode for the database connection.
# Possible values include 'disable', 'require', 'verify-ca', and 'verify-full'.
Expand Down
5 changes: 5 additions & 0 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ func main() {

config.SetDefault()

// Load database configuration based on DB_TYPE
if err := config.LoadDBConfig(); err != nil {
log.Panic("Error loading DB configuration: %s", err.Error())
}

if err := config.Validate(); err != nil {
log.Panic("Invalid configuration: %s", err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion internal/db/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func Init(config *app.Config) {
Charset: config.DBCharset,
Extras: config.DBExtras,
})
} else if config.DBType == "mysql" {
} else if config.DBType == "mysql" || config.DBType == "oceanbase" {
DifyPluginDB, err = mysql.InitPluginDB(&mysql.MySQLConfig{
Host: config.DBHost,
Port: int(config.DBPort),
Expand Down
46 changes: 42 additions & 4 deletions internal/types/app/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package app

import (
"fmt"
"os"
"strconv"

"github.com/go-playground/validator/v10"
)
Expand Down Expand Up @@ -119,10 +121,10 @@ type Config struct {

// database
DBType string `envconfig:"DB_TYPE" default:"postgresql"`
DBUsername string `envconfig:"DB_USERNAME" validate:"required"`
DBPassword string `envconfig:"DB_PASSWORD" validate:"required"`
DBHost string `envconfig:"DB_HOST" validate:"required"`
DBPort uint16 `envconfig:"DB_PORT" validate:"required"`
DBUsername string
DBPassword string
DBHost string
DBPort uint16
DBDatabase string `envconfig:"DB_DATABASE" validate:"required"`
DBDefaultDatabase string `envconfig:"DB_DEFAULT_DATABASE" validate:"required"`
DBSslMode string `envconfig:"DB_SSL_MODE" validate:"required,oneof=disable require"`
Expand Down Expand Up @@ -282,3 +284,39 @@ const (
PLATFORM_LOCAL PlatformType = "local"
PLATFORM_SERVERLESS PlatformType = "serverless"
)

// LoadDBConfig loads database configuration based on DB_TYPE
// Only updates 4 fields: DBUsername, DBPassword, DBHost, DBPort
func (c *Config) LoadDBConfig() error {
var prefix string
switch c.DBType {
case "mysql":
prefix = "MYSQL"
case "postgresql":
prefix = "POSTGRES"
case "oceanbase":
prefix = "OCEANBASE"
default:
return nil
}

if user, ok := os.LookupEnv(prefix + "_USER"); ok {
c.DBUsername = user
}
if password, ok := os.LookupEnv(prefix + "_PASSWORD"); ok {
c.DBPassword = password
}
if host, ok := os.LookupEnv(prefix + "_HOST"); ok {
c.DBHost = host
}

if portStr, ok := os.LookupEnv(prefix + "_PORT"); ok {
port, err := strconv.ParseUint(portStr, 10, 16)
if err != nil {
return fmt.Errorf("invalid %s_PORT value: %q", prefix, portStr)
}
c.DBPort = uint16(port)
}

return nil
}
2 changes: 1 addition & 1 deletion internal/types/app/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (config *Config) SetDefault() {
setDefaultInt(&config.DifyInvocationReadTimeout, 240000)
if config.DBType == "postgresql" {
setDefaultString(&config.DBDefaultDatabase, "postgres")
} else if config.DBType == "mysql" {
} else if config.DBType == "mysql" || config.DBType == "oceanbase" {
setDefaultString(&config.DBDefaultDatabase, "mysql")
}
setDefaultBoolPtr(&config.HealthApiLogEnabled, true)
Expand Down