-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split setting.go as multiple files (#6014)
* split setting.go as multiple files * fix comments
- Loading branch information
Showing
7 changed files
with
509 additions
and
442 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright 2019 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package setting | ||
|
||
import ( | ||
"strings" | ||
"time" | ||
|
||
"code.gitea.io/gitea/modules/log" | ||
) | ||
|
||
// Cache represents cache settings | ||
type Cache struct { | ||
Adapter string | ||
Interval int | ||
Conn string | ||
TTL time.Duration | ||
} | ||
|
||
var ( | ||
// CacheService the global cache | ||
CacheService *Cache | ||
) | ||
|
||
func newCacheService() { | ||
sec := Cfg.Section("cache") | ||
CacheService = &Cache{ | ||
Adapter: sec.Key("ADAPTER").In("memory", []string{"memory", "redis", "memcache"}), | ||
} | ||
switch CacheService.Adapter { | ||
case "memory": | ||
CacheService.Interval = sec.Key("INTERVAL").MustInt(60) | ||
case "redis", "memcache": | ||
CacheService.Conn = strings.Trim(sec.Key("HOST").String(), "\" ") | ||
default: | ||
log.Fatal(4, "Unknown cache adapter: %s", CacheService.Adapter) | ||
} | ||
CacheService.TTL = sec.Key("ITEM_TTL").MustDuration(16 * time.Hour) | ||
|
||
log.Info("Cache Service Enabled") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
// Copyright 2019 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package setting | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path" | ||
"path/filepath" | ||
"strings" | ||
|
||
"code.gitea.io/gitea/modules/log" | ||
"github.com/go-xorm/core" | ||
) | ||
|
||
var logLevels = map[string]string{ | ||
"Trace": "0", | ||
"Debug": "1", | ||
"Info": "2", | ||
"Warn": "3", | ||
"Error": "4", | ||
"Critical": "5", | ||
} | ||
|
||
func getLogLevel(section string, key string, defaultValue string) string { | ||
validLevels := []string{"Trace", "Debug", "Info", "Warn", "Error", "Critical"} | ||
return Cfg.Section(section).Key(key).In(defaultValue, validLevels) | ||
} | ||
|
||
func newLogService() { | ||
log.Info("Gitea v%s%s", AppVer, AppBuiltWith) | ||
|
||
LogModes = strings.Split(Cfg.Section("log").Key("MODE").MustString("console"), ",") | ||
LogConfigs = make([]string, len(LogModes)) | ||
|
||
useConsole := false | ||
for i := 0; i < len(LogModes); i++ { | ||
LogModes[i] = strings.TrimSpace(LogModes[i]) | ||
if LogModes[i] == "console" { | ||
useConsole = true | ||
} | ||
} | ||
|
||
if !useConsole { | ||
log.DelLogger("console") | ||
} | ||
|
||
for i, mode := range LogModes { | ||
sec, err := Cfg.GetSection("log." + mode) | ||
if err != nil { | ||
sec, _ = Cfg.NewSection("log." + mode) | ||
} | ||
|
||
// Log level. | ||
levelName := getLogLevel("log."+mode, "LEVEL", LogLevel) | ||
level, ok := logLevels[levelName] | ||
if !ok { | ||
log.Fatal(4, "Unknown log level: %s", levelName) | ||
} | ||
|
||
// Generate log configuration. | ||
switch mode { | ||
case "console": | ||
LogConfigs[i] = fmt.Sprintf(`{"level":%s}`, level) | ||
case "file": | ||
logPath := sec.Key("FILE_NAME").MustString(path.Join(LogRootPath, "gitea.log")) | ||
if err = os.MkdirAll(path.Dir(logPath), os.ModePerm); err != nil { | ||
panic(err.Error()) | ||
} | ||
|
||
LogConfigs[i] = fmt.Sprintf( | ||
`{"level":%s,"filename":"%s","rotate":%v,"maxsize":%d,"daily":%v,"maxdays":%d}`, level, | ||
logPath, | ||
sec.Key("LOG_ROTATE").MustBool(true), | ||
1<<uint(sec.Key("MAX_SIZE_SHIFT").MustInt(28)), | ||
sec.Key("DAILY_ROTATE").MustBool(true), | ||
sec.Key("MAX_DAYS").MustInt(7)) | ||
case "conn": | ||
LogConfigs[i] = fmt.Sprintf(`{"level":%s,"reconnectOnMsg":%v,"reconnect":%v,"net":"%s","addr":"%s"}`, level, | ||
sec.Key("RECONNECT_ON_MSG").MustBool(), | ||
sec.Key("RECONNECT").MustBool(), | ||
sec.Key("PROTOCOL").In("tcp", []string{"tcp", "unix", "udp"}), | ||
sec.Key("ADDR").MustString(":7020")) | ||
case "smtp": | ||
LogConfigs[i] = fmt.Sprintf(`{"level":%s,"username":"%s","password":"%s","host":"%s","sendTos":["%s"],"subject":"%s"}`, level, | ||
sec.Key("USER").MustString("example@example.com"), | ||
sec.Key("PASSWD").MustString("******"), | ||
sec.Key("HOST").MustString("127.0.0.1:25"), | ||
strings.Replace(sec.Key("RECEIVERS").MustString("example@example.com"), ",", "\",\"", -1), | ||
sec.Key("SUBJECT").MustString("Diagnostic message from serve")) | ||
case "database": | ||
LogConfigs[i] = fmt.Sprintf(`{"level":%s,"driver":"%s","conn":"%s"}`, level, | ||
sec.Key("DRIVER").String(), | ||
sec.Key("CONN").String()) | ||
} | ||
|
||
log.NewLogger(Cfg.Section("log").Key("BUFFER_LEN").MustInt64(10000), mode, LogConfigs[i]) | ||
log.Info("Log Mode: %s(%s)", strings.Title(mode), levelName) | ||
} | ||
} | ||
|
||
// NewXORMLogService initializes xorm logger service | ||
func NewXORMLogService(disableConsole bool) { | ||
logModes := strings.Split(Cfg.Section("log").Key("MODE").MustString("console"), ",") | ||
var logConfigs string | ||
for _, mode := range logModes { | ||
mode = strings.TrimSpace(mode) | ||
|
||
if disableConsole && mode == "console" { | ||
continue | ||
} | ||
|
||
sec, err := Cfg.GetSection("log." + mode) | ||
if err != nil { | ||
sec, _ = Cfg.NewSection("log." + mode) | ||
} | ||
|
||
// Log level. | ||
levelName := getLogLevel("log."+mode, "LEVEL", LogLevel) | ||
level, ok := logLevels[levelName] | ||
if !ok { | ||
log.Fatal(4, "Unknown log level: %s", levelName) | ||
} | ||
|
||
// Generate log configuration. | ||
switch mode { | ||
case "console": | ||
logConfigs = fmt.Sprintf(`{"level":%s}`, level) | ||
case "file": | ||
logPath := sec.Key("FILE_NAME").MustString(path.Join(LogRootPath, "xorm.log")) | ||
if err = os.MkdirAll(path.Dir(logPath), os.ModePerm); err != nil { | ||
panic(err.Error()) | ||
} | ||
logPath = path.Join(filepath.Dir(logPath), "xorm.log") | ||
|
||
logConfigs = fmt.Sprintf( | ||
`{"level":%s,"filename":"%s","rotate":%v,"maxsize":%d,"daily":%v,"maxdays":%d}`, level, | ||
logPath, | ||
sec.Key("LOG_ROTATE").MustBool(true), | ||
1<<uint(sec.Key("MAX_SIZE_SHIFT").MustInt(28)), | ||
sec.Key("DAILY_ROTATE").MustBool(true), | ||
sec.Key("MAX_DAYS").MustInt(7)) | ||
case "conn": | ||
logConfigs = fmt.Sprintf(`{"level":%s,"reconnectOnMsg":%v,"reconnect":%v,"net":"%s","addr":"%s"}`, level, | ||
sec.Key("RECONNECT_ON_MSG").MustBool(), | ||
sec.Key("RECONNECT").MustBool(), | ||
sec.Key("PROTOCOL").In("tcp", []string{"tcp", "unix", "udp"}), | ||
sec.Key("ADDR").MustString(":7020")) | ||
case "smtp": | ||
logConfigs = fmt.Sprintf(`{"level":%s,"username":"%s","password":"%s","host":"%s","sendTos":"%s","subject":"%s"}`, level, | ||
sec.Key("USER").MustString("example@example.com"), | ||
sec.Key("PASSWD").MustString("******"), | ||
sec.Key("HOST").MustString("127.0.0.1:25"), | ||
sec.Key("RECEIVERS").MustString("[]"), | ||
sec.Key("SUBJECT").MustString("Diagnostic message from serve")) | ||
case "database": | ||
logConfigs = fmt.Sprintf(`{"level":%s,"driver":"%s","conn":"%s"}`, level, | ||
sec.Key("DRIVER").String(), | ||
sec.Key("CONN").String()) | ||
} | ||
|
||
log.NewXORMLogger(Cfg.Section("log").Key("BUFFER_LEN").MustInt64(10000), mode, logConfigs) | ||
if !disableConsole { | ||
log.Info("XORM Log Mode: %s(%s)", strings.Title(mode), levelName) | ||
} | ||
|
||
var lvl core.LogLevel | ||
switch levelName { | ||
case "Trace", "Debug": | ||
lvl = core.LOG_DEBUG | ||
case "Info": | ||
lvl = core.LOG_INFO | ||
case "Warn": | ||
lvl = core.LOG_WARNING | ||
case "Error", "Critical": | ||
lvl = core.LOG_ERR | ||
} | ||
log.XORMLogger.SetLevel(lvl) | ||
} | ||
|
||
if len(logConfigs) == 0 { | ||
log.DiscardXORMLogger() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
// Copyright 2019 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package setting | ||
|
||
import ( | ||
"net/mail" | ||
|
||
"code.gitea.io/gitea/modules/log" | ||
shellquote "github.com/kballard/go-shellquote" | ||
) | ||
|
||
// Mailer represents mail service. | ||
type Mailer struct { | ||
// Mailer | ||
QueueLength int | ||
Name string | ||
From string | ||
FromName string | ||
FromEmail string | ||
SendAsPlainText bool | ||
MailerType string | ||
|
||
// SMTP sender | ||
Host string | ||
User, Passwd string | ||
DisableHelo bool | ||
HeloHostname string | ||
SkipVerify bool | ||
UseCertificate bool | ||
CertFile, KeyFile string | ||
IsTLSEnabled bool | ||
|
||
// Sendmail sender | ||
SendmailPath string | ||
SendmailArgs []string | ||
} | ||
|
||
var ( | ||
// MailService the global mailer | ||
MailService *Mailer | ||
) | ||
|
||
func newMailService() { | ||
sec := Cfg.Section("mailer") | ||
// Check mailer setting. | ||
if !sec.Key("ENABLED").MustBool() { | ||
return | ||
} | ||
|
||
MailService = &Mailer{ | ||
QueueLength: sec.Key("SEND_BUFFER_LEN").MustInt(100), | ||
Name: sec.Key("NAME").MustString(AppName), | ||
SendAsPlainText: sec.Key("SEND_AS_PLAIN_TEXT").MustBool(false), | ||
MailerType: sec.Key("MAILER_TYPE").In("", []string{"smtp", "sendmail", "dummy"}), | ||
|
||
Host: sec.Key("HOST").String(), | ||
User: sec.Key("USER").String(), | ||
Passwd: sec.Key("PASSWD").String(), | ||
DisableHelo: sec.Key("DISABLE_HELO").MustBool(), | ||
HeloHostname: sec.Key("HELO_HOSTNAME").String(), | ||
SkipVerify: sec.Key("SKIP_VERIFY").MustBool(), | ||
UseCertificate: sec.Key("USE_CERTIFICATE").MustBool(), | ||
CertFile: sec.Key("CERT_FILE").String(), | ||
KeyFile: sec.Key("KEY_FILE").String(), | ||
IsTLSEnabled: sec.Key("IS_TLS_ENABLED").MustBool(), | ||
|
||
SendmailPath: sec.Key("SENDMAIL_PATH").MustString("sendmail"), | ||
} | ||
MailService.From = sec.Key("FROM").MustString(MailService.User) | ||
|
||
if sec.HasKey("ENABLE_HTML_ALTERNATIVE") { | ||
log.Warn("ENABLE_HTML_ALTERNATIVE is deprecated, use SEND_AS_PLAIN_TEXT") | ||
MailService.SendAsPlainText = !sec.Key("ENABLE_HTML_ALTERNATIVE").MustBool(false) | ||
} | ||
|
||
if sec.HasKey("USE_SENDMAIL") { | ||
log.Warn("USE_SENDMAIL is deprecated, use MAILER_TYPE=sendmail") | ||
if MailService.MailerType == "" && sec.Key("USE_SENDMAIL").MustBool(false) { | ||
MailService.MailerType = "sendmail" | ||
} | ||
} | ||
|
||
parsed, err := mail.ParseAddress(MailService.From) | ||
if err != nil { | ||
log.Fatal(4, "Invalid mailer.FROM (%s): %v", MailService.From, err) | ||
} | ||
MailService.FromName = parsed.Name | ||
MailService.FromEmail = parsed.Address | ||
|
||
if MailService.MailerType == "" { | ||
MailService.MailerType = "smtp" | ||
} | ||
|
||
if MailService.MailerType == "sendmail" { | ||
MailService.SendmailArgs, err = shellquote.Split(sec.Key("SENDMAIL_ARGS").String()) | ||
if err != nil { | ||
log.Error(4, "Failed to parse Sendmail args: %v", CustomConf, err) | ||
} | ||
} | ||
|
||
log.Info("Mail Service Enabled") | ||
} | ||
|
||
func newRegisterMailService() { | ||
if !Cfg.Section("service").Key("REGISTER_EMAIL_CONFIRM").MustBool() { | ||
return | ||
} else if MailService == nil { | ||
log.Warn("Register Mail Service: Mail Service is not enabled") | ||
return | ||
} | ||
Service.RegisterEmailConfirm = true | ||
log.Info("Register Mail Service Enabled") | ||
} | ||
|
||
func newNotifyMailService() { | ||
if !Cfg.Section("service").Key("ENABLE_NOTIFY_MAIL").MustBool() { | ||
return | ||
} else if MailService == nil { | ||
log.Warn("Notify Mail Service: Mail Service is not enabled") | ||
return | ||
} | ||
Service.EnableNotifyMail = true | ||
log.Info("Notify Mail Service Enabled") | ||
} |
Oops, something went wrong.