1
0
mirror of https://github.com/haiwen/seafile-server.git synced 2025-09-17 23:59:44 +00:00

Read database option from env (#712)

* Read database option from env

* Don't need to read config from file

---------

Co-authored-by: 杨赫然 <heran.yang@seafile.com>
This commit is contained in:
feiniks
2024-11-05 12:42:36 +08:00
committed by GitHub
parent 56f220be0a
commit 31887e99e9

View File

@@ -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)
@@ -88,66 +63,16 @@ 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.Fatalf("Failed to load database from env: %v", err)
}
section, err := config.GetSection("Database")
if err != nil {
log.Fatal("No database section in ccnet.conf.")
}
var dbEngine string = "mysql"
key, err := section.GetKey("ENGINE")
if err == nil {
dbEngine = key.String()
}
if !strings.EqualFold(dbEngine, "mysql") {
log.Fatalf("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 == "" {
log.Fatal("No database host in ccnet.conf.")
}
// user is required.
if key, err = section.GetKey("USER"); err != nil {
log.Fatal("No database user in ccnet.conf.")
}
user := key.String()
password := ""
if key, err = section.GetKey("PASSWD"); err == nil {
password = key.String()
} else if unixSocket == "" {
log.Fatal("No database password in ccnet.conf.")
}
if key, err = section.GetKey("DB"); err != nil {
log.Fatal("No database db_name in ccnet.conf.")
}
dbName := 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()
}
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)
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", user, password, unixSocket, dbName)
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 {
@@ -161,6 +86,56 @@ func loadCcnetDB() {
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 password == "" {
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 main() {
flag.Parse()