From 1b1f3d754fdcd77849040fcf8703b04dce74a87a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E8=B5=AB=E7=84=B6?= Date: Mon, 4 Nov 2024 10:28:19 +0800 Subject: [PATCH] Read database option from env --- notification-server/server.go | 146 ++++++++++++++++++++++++++-------- 1 file changed, 112 insertions(+), 34 deletions(-) diff --git a/notification-server/server.go b/notification-server/server.go index ad2b6821..f19eb982 100644 --- a/notification-server/server.go +++ b/notification-server/server.go @@ -88,77 +88,155 @@ func loadNotifConfig() { } func loadCcnetDB() { - ccnetConfPath := filepath.Join(configDir, "ccnet.conf") - config, err := ini.Load(ccnetConfPath) + option, err := loadDBOptionFromEnv() if err != nil { - log.Fatalf("Failed to load ccnet.conf: %v", err) + log.Infof("Failed to load database from env: %v", err) + option, err = loadDBOptionFromFile() + if err != nil { + log.Fatalf("Failed to load database: %v", err) + } + } + + var dsn string + if option.UnixSocket == "" { + dsn = fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?tls=%t&readTimeout=60s&writeTimeout=60s", option.User, option.Password, option.Host, option.Port, option.CcnetDbName, option.UseTLS) + } else { + dsn = fmt.Sprintf("%s:%s@unix(%s)/%s?readTimeout=60s&writeTimeout=60s", option.User, option.Password, option.UnixSocket, option.CcnetDbName) + } + ccnetDB, err = sql.Open("mysql", dsn) + if err != nil { + log.Fatalf("Failed to open database: %v", err) + } + if err := ccnetDB.Ping(); err != nil { + log.Fatalf("Failed to connected to mysql: %v", err) + } + ccnetDB.SetConnMaxLifetime(5 * time.Minute) + ccnetDB.SetMaxOpenConns(8) + ccnetDB.SetMaxIdleConns(8) +} + +type DBOption struct { + User string + Password string + Host string + Port int + CcnetDbName string + SeafileDbName string + UnixSocket string + UseTLS bool +} + +func loadDBOptionFromEnv() (*DBOption, error) { + user := os.Getenv("SEAFILE_MYSQL_DB_USER") + if user == "" { + return nil, fmt.Errorf("failed to read SEAFILE_MYSQL_DB_USER") + } + password := os.Getenv("SEAFILE_MYSQL_DB_PASSWORD") + if user == "" { + return nil, fmt.Errorf("failed to read SEAFILE_MYSQL_DB_PASSWORD") + } + host := os.Getenv("SEAFILE_MYSQL_DB_HOST") + if host == "" { + return nil, fmt.Errorf("failed to read SEAFILE_MYSQL_DB_HOST") + } + ccnetDbName := os.Getenv("SEAFILE_MYSQL_DB_CCNET_DB_NAME") + if ccnetDbName == "" { + ccnetDbName = "ccnet_db" + log.Infof("Failed to read SEAFILE_MYSQL_DB_CCNET_DB_NAME, use ccnet_db by default") + } + seafileDbName := os.Getenv("SEAFILE_MYSQL_DB_SEAFILE_DB_NAME") + if seafileDbName == "" { + seafileDbName = "seafile_db" + log.Infof("Failed to read SEAFILE_MYSQL_DB_SEAFILE_DB_NAME, use seafile_db by default") + } + + log.Infof("Database: user = %s", user) + log.Infof("Database: host = %s", host) + log.Infof("Database: ccnet_db_name = %s", ccnetDbName) + log.Infof("Database: seafile_db_name = %s", seafileDbName) + + option := new(DBOption) + option.User = user + option.Password = password + option.Host = host + option.Port = 3306 + option.CcnetDbName = ccnetDbName + option.SeafileDbName = seafileDbName + return option, nil +} + +func loadDBOptionFromFile() (*DBOption, error) { + confPath := filepath.Join(configDir, "seafile.conf") + config, err := ini.Load(confPath) + if err != nil { + return nil, fmt.Errorf("failed to load seafile.conf: %v", err) } - section, err := config.GetSection("Database") + section, err := config.GetSection("database") if err != nil { - log.Fatal("No database section in ccnet.conf.") + return nil, fmt.Errorf("no database section in seafile.conf") } var dbEngine string = "mysql" - key, err := section.GetKey("ENGINE") + key, err := section.GetKey("type") if err == nil { dbEngine = key.String() } if !strings.EqualFold(dbEngine, "mysql") { - log.Fatalf("Unsupported database %s.", dbEngine) + return nil, fmt.Errorf("unsupported database %s", dbEngine) } unixSocket := "" - if key, err = section.GetKey("UNIX_SOCKET"); err == nil { + if key, err = section.GetKey("unix_socket"); err == nil { unixSocket = key.String() } host := "" - if key, err = section.GetKey("HOST"); err == nil { + if key, err = section.GetKey("host"); err == nil { host = key.String() } else if unixSocket == "" { - log.Fatal("No database host in ccnet.conf.") + return nil, fmt.Errorf("no database host in seafile.conf") } // user is required. - if key, err = section.GetKey("USER"); err != nil { - log.Fatal("No database user in ccnet.conf.") + if key, err = section.GetKey("user"); err != nil { + return nil, fmt.Errorf("no database user in seafile.conf") } user := key.String() password := "" - if key, err = section.GetKey("PASSWD"); err == nil { + if key, err = section.GetKey("password"); err == nil { password = key.String() } else if unixSocket == "" { - log.Fatal("No database password in ccnet.conf.") + return nil, fmt.Errorf("no database password in seafile.conf") + } + if key, err = section.GetKey("db_name"); err != nil { + return nil, fmt.Errorf("no database db_name in seafile.conf") } - if key, err = section.GetKey("DB"); err != nil { - log.Fatal("No database db_name in ccnet.conf.") + seafileDbName := key.String() + if key, err = section.GetKey("ccnet_db_name"); err != nil { + return nil, fmt.Errorf("no database ccnet_db_name in seafile.conf") } - dbName := key.String() + ccnetDbName := key.String() port := 3306 - if key, err = section.GetKey("PORT"); err == nil { + if key, err = section.GetKey("port"); err == nil { port, _ = key.Int() } useTLS := false if key, err = section.GetKey("USE_SSL"); err == nil { useTLS, _ = key.Bool() } - var dsn string - if unixSocket == "" { - dsn = fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?tls=%t&readTimeout=60s&writeTimeout=60s", user, password, host, port, dbName, useTLS) - } else { - dsn = fmt.Sprintf("%s:%s@unix(%s)/%s?readTimeout=60s&writeTimeout=60s", user, password, unixSocket, dbName) - } - ccnetDB, err = sql.Open("mysql", dsn) - if err != nil { - log.Fatalf("Failed to open database: %v", err) - } - if err := ccnetDB.Ping(); err != nil { - log.Fatalf("Failed to connected to mysql: %v", err) - } - ccnetDB.SetConnMaxLifetime(5 * time.Minute) - ccnetDB.SetMaxOpenConns(8) - ccnetDB.SetMaxIdleConns(8) + + option := new(DBOption) + option.User = user + option.Password = password + option.Host = host + option.Port = port + option.CcnetDbName = ccnetDbName + option.SeafileDbName = seafileDbName + option.UnixSocket = unixSocket + option.UseTLS = useTLS + + return option, nil } func main() {