Skip to content

Commit b2aa8c5

Browse files
author
杨赫然
committed
Don't need to read config from file
1 parent 1b1f3d7 commit b2aa8c5

File tree

1 file changed

+5
-108
lines changed

1 file changed

+5
-108
lines changed

notification-server/server.go

Lines changed: 5 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"github.com/gorilla/mux"
2020
"github.com/gorilla/websocket"
2121
log "github.com/sirupsen/logrus"
22-
"gopkg.in/ini.v1"
2322
)
2423

2524
var configDir string
@@ -46,36 +45,12 @@ func init() {
4645
}
4746

4847
func loadNotifConfig() {
49-
notifyConfPath := filepath.Join(configDir, "seafile.conf")
50-
51-
opts := ini.LoadOptions{}
52-
opts.SpaceBeforeInlineComment = true
53-
config, err := ini.LoadSources(opts, notifyConfPath)
54-
if err != nil {
55-
log.Fatalf("Failed to load notification.conf: %v", err)
56-
}
57-
58-
section, err := config.GetSection("notification")
59-
if err != nil {
60-
log.Fatal("No notification section in seafile.conf.")
61-
}
62-
6348
host = "0.0.0.0"
6449
port = 8083
65-
logLevel := "info"
66-
if key, err := section.GetKey("host"); err == nil {
67-
host = key.String()
68-
}
69-
70-
if key, err := section.GetKey("port"); err == nil {
71-
n, err := key.Uint()
72-
if err == nil {
73-
port = uint32(n)
74-
}
75-
}
7650

77-
if key, err := section.GetKey("log_level"); err == nil {
78-
logLevel = key.String()
51+
logLevel := os.Getenv("NOTIFICATION_SERVER_LOG_LEVEL")
52+
if logLevel == "" {
53+
logLevel = "info"
7954
}
8055

8156
level, err := log.ParseLevel(logLevel)
@@ -90,11 +65,7 @@ func loadNotifConfig() {
9065
func loadCcnetDB() {
9166
option, err := loadDBOptionFromEnv()
9267
if err != nil {
93-
log.Infof("Failed to load database from env: %v", err)
94-
option, err = loadDBOptionFromFile()
95-
if err != nil {
96-
log.Fatalf("Failed to load database: %v", err)
97-
}
68+
log.Fatalf("Failed to load database from env: %v", err)
9869
}
9970

10071
var dsn string
@@ -132,7 +103,7 @@ func loadDBOptionFromEnv() (*DBOption, error) {
132103
return nil, fmt.Errorf("failed to read SEAFILE_MYSQL_DB_USER")
133104
}
134105
password := os.Getenv("SEAFILE_MYSQL_DB_PASSWORD")
135-
if user == "" {
106+
if password == "" {
136107
return nil, fmt.Errorf("failed to read SEAFILE_MYSQL_DB_PASSWORD")
137108
}
138109
host := os.Getenv("SEAFILE_MYSQL_DB_HOST")
@@ -165,80 +136,6 @@ func loadDBOptionFromEnv() (*DBOption, error) {
165136
return option, nil
166137
}
167138

168-
func loadDBOptionFromFile() (*DBOption, error) {
169-
confPath := filepath.Join(configDir, "seafile.conf")
170-
config, err := ini.Load(confPath)
171-
if err != nil {
172-
return nil, fmt.Errorf("failed to load seafile.conf: %v", err)
173-
}
174-
175-
section, err := config.GetSection("database")
176-
if err != nil {
177-
return nil, fmt.Errorf("no database section in seafile.conf")
178-
}
179-
180-
var dbEngine string = "mysql"
181-
key, err := section.GetKey("type")
182-
if err == nil {
183-
dbEngine = key.String()
184-
}
185-
186-
if !strings.EqualFold(dbEngine, "mysql") {
187-
return nil, fmt.Errorf("unsupported database %s", dbEngine)
188-
}
189-
190-
unixSocket := ""
191-
if key, err = section.GetKey("unix_socket"); err == nil {
192-
unixSocket = key.String()
193-
}
194-
195-
host := ""
196-
if key, err = section.GetKey("host"); err == nil {
197-
host = key.String()
198-
} else if unixSocket == "" {
199-
return nil, fmt.Errorf("no database host in seafile.conf")
200-
}
201-
// user is required.
202-
if key, err = section.GetKey("user"); err != nil {
203-
return nil, fmt.Errorf("no database user in seafile.conf")
204-
}
205-
user := key.String()
206-
password := ""
207-
if key, err = section.GetKey("password"); err == nil {
208-
password = key.String()
209-
} else if unixSocket == "" {
210-
return nil, fmt.Errorf("no database password in seafile.conf")
211-
}
212-
if key, err = section.GetKey("db_name"); err != nil {
213-
return nil, fmt.Errorf("no database db_name in seafile.conf")
214-
}
215-
seafileDbName := key.String()
216-
if key, err = section.GetKey("ccnet_db_name"); err != nil {
217-
return nil, fmt.Errorf("no database ccnet_db_name in seafile.conf")
218-
}
219-
ccnetDbName := key.String()
220-
port := 3306
221-
if key, err = section.GetKey("port"); err == nil {
222-
port, _ = key.Int()
223-
}
224-
useTLS := false
225-
if key, err = section.GetKey("USE_SSL"); err == nil {
226-
useTLS, _ = key.Bool()
227-
}
228-
229-
option := new(DBOption)
230-
option.User = user
231-
option.Password = password
232-
option.Host = host
233-
option.Port = port
234-
option.CcnetDbName = ccnetDbName
235-
option.SeafileDbName = seafileDbName
236-
option.UnixSocket = unixSocket
237-
option.UseTLS = useTLS
238-
239-
return option, nil
240-
}
241-
242139
func main() {
243140
flag.Parse()
244141

0 commit comments

Comments
 (0)