From 820e28c9044bbd9d398dd1617323665bb0935e13 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sun, 10 Feb 2019 09:37:37 +0800 Subject: [PATCH] Split setting.go as multiple files (#6014) * split setting.go as multiple files * fix comments --- modules/setting/cache.go | 43 ++++ modules/setting/log.go | 186 ++++++++++++++++ modules/setting/mailer.go | 126 +++++++++++ modules/setting/service.go | 89 ++++++++ modules/setting/session.go | 35 +++ modules/setting/setting.go | 442 ------------------------------------- modules/setting/webhook.go | 30 +++ 7 files changed, 509 insertions(+), 442 deletions(-) create mode 100644 modules/setting/cache.go create mode 100644 modules/setting/log.go create mode 100644 modules/setting/mailer.go create mode 100644 modules/setting/service.go create mode 100644 modules/setting/session.go create mode 100644 modules/setting/webhook.go diff --git a/modules/setting/cache.go b/modules/setting/cache.go new file mode 100644 index 000000000..cbe73cf4e --- /dev/null +++ b/modules/setting/cache.go @@ -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") +} diff --git a/modules/setting/log.go b/modules/setting/log.go new file mode 100644 index 000000000..7b0832c0c --- /dev/null +++ b/modules/setting/log.go @@ -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<