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<