Skip to content

Commit

Permalink
Don't need to read config from file
Browse files Browse the repository at this point in the history
  • Loading branch information
杨赫然 committed Nov 4, 2024
1 parent 1b1f3d7 commit b2aa8c5
Showing 1 changed file with 5 additions and 108 deletions.
113 changes: 5 additions & 108 deletions notification-server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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()

Expand Down

0 comments on commit b2aa8c5

Please sign in to comment.