From b2aa8c5427adb8c65f7b0bb6746dceb263b394d6 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 16:26:16 +0800 Subject: [PATCH] Don't need to read config from file --- notification-server/server.go | 113 ++-------------------------------- 1 file changed, 5 insertions(+), 108 deletions(-) diff --git a/notification-server/server.go b/notification-server/server.go index f19eb982..7b6dfead 100644 --- a/notification-server/server.go +++ b/notification-server/server.go @@ -19,7 +19,6 @@ import ( "github.com/gorilla/mux" "github.com/gorilla/websocket" log "github.com/sirupsen/logrus" - "gopkg.in/ini.v1" ) var configDir string @@ -46,36 +45,12 @@ func init() { } func loadNotifConfig() { - notifyConfPath := filepath.Join(configDir, "seafile.conf") - - opts := ini.LoadOptions{} - opts.SpaceBeforeInlineComment = true - config, err := ini.LoadSources(opts, notifyConfPath) - if err != nil { - log.Fatalf("Failed to load notification.conf: %v", err) - } - - section, err := config.GetSection("notification") - if err != nil { - log.Fatal("No notification section in seafile.conf.") - } - host = "0.0.0.0" port = 8083 - logLevel := "info" - if key, err := section.GetKey("host"); err == nil { - host = key.String() - } - - if key, err := section.GetKey("port"); err == nil { - n, err := key.Uint() - if err == nil { - port = uint32(n) - } - } - if key, err := section.GetKey("log_level"); err == nil { - logLevel = key.String() + logLevel := os.Getenv("NOTIFICATION_SERVER_LOG_LEVEL") + if logLevel == "" { + logLevel = "info" } level, err := log.ParseLevel(logLevel) @@ -90,11 +65,7 @@ func loadNotifConfig() { func loadCcnetDB() { option, err := loadDBOptionFromEnv() if err != nil { - log.Infof("Failed to load database from env: %v", err) - option, err = loadDBOptionFromFile() - if err != nil { - log.Fatalf("Failed to load database: %v", err) - } + log.Fatalf("Failed to load database from env: %v", err) } var dsn string @@ -132,7 +103,7 @@ func loadDBOptionFromEnv() (*DBOption, error) { return nil, fmt.Errorf("failed to read SEAFILE_MYSQL_DB_USER") } password := os.Getenv("SEAFILE_MYSQL_DB_PASSWORD") - if user == "" { + if password == "" { return nil, fmt.Errorf("failed to read SEAFILE_MYSQL_DB_PASSWORD") } host := os.Getenv("SEAFILE_MYSQL_DB_HOST") @@ -165,80 +136,6 @@ func loadDBOptionFromEnv() (*DBOption, error) { 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") - if err != nil { - return nil, fmt.Errorf("no database section in seafile.conf") - } - - var dbEngine string = "mysql" - key, err := section.GetKey("type") - if err == nil { - dbEngine = key.String() - } - - if !strings.EqualFold(dbEngine, "mysql") { - return nil, fmt.Errorf("unsupported database %s", dbEngine) - } - - unixSocket := "" - if key, err = section.GetKey("unix_socket"); err == nil { - unixSocket = key.String() - } - - host := "" - if key, err = section.GetKey("host"); err == nil { - host = key.String() - } else if unixSocket == "" { - return nil, fmt.Errorf("no database host in seafile.conf") - } - // user is required. - 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("password"); err == nil { - password = key.String() - } else if unixSocket == "" { - 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") - } - 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") - } - ccnetDbName := key.String() - port := 3306 - 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() - } - - 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() { flag.Parse()