aboutsummaryrefslogtreecommitdiff
path: root/modules/base/conf.go
diff options
context:
space:
mode:
Diffstat (limited to 'modules/base/conf.go')
-rw-r--r--modules/base/conf.go92
1 files changed, 70 insertions, 22 deletions
diff --git a/modules/base/conf.go b/modules/base/conf.go
index fdbf3ad3..863daca6 100644
--- a/modules/base/conf.go
+++ b/modules/base/conf.go
@@ -15,6 +15,8 @@ import (
"github.com/Unknwon/com"
"github.com/Unknwon/goconfig"
+ "github.com/gogits/cache"
+
"github.com/gogits/gogs/modules/log"
)
@@ -26,20 +28,32 @@ type Mailer struct {
}
var (
- AppVer string
- AppName string
- AppLogo string
- AppUrl string
- Domain string
- SecretKey string
+ AppVer string
+ AppName string
+ AppLogo string
+ AppUrl string
+ Domain string
+ SecretKey string
+ RunUser string
+ RepoRootPath string
+
Cfg *goconfig.ConfigFile
MailService *Mailer
+
+ Cache cache.Cache
+ CacheAdapter string
+ CacheConfig string
+
+ LogMode string
+ LogConfig string
)
var Service struct {
- RegisterEmailConfirm bool
- ActiveCodeLives int
- ResetPwdCodeLives int
+ RegisterEmailConfirm bool
+ DisenableRegisteration bool
+ RequireSignInView bool
+ ActiveCodeLives int
+ ResetPwdCodeLives int
}
func exeDir() (string, error) {
@@ -66,19 +80,21 @@ var logLevels = map[string]string{
func newService() {
Service.ActiveCodeLives = Cfg.MustInt("service", "ACTIVE_CODE_LIVE_MINUTES", 180)
Service.ResetPwdCodeLives = Cfg.MustInt("service", "RESET_PASSWD_CODE_LIVE_MINUTES", 180)
+ Service.DisenableRegisteration = Cfg.MustBool("service", "DISENABLE_REGISTERATION", false)
+ Service.RequireSignInView = Cfg.MustBool("service", "REQUIRE_SIGNIN_VIEW", false)
}
func newLogService() {
// Get and check log mode.
- mode := Cfg.MustValue("log", "MODE", "console")
- modeSec := "log." + mode
+ LogMode = Cfg.MustValue("log", "MODE", "console")
+ modeSec := "log." + LogMode
if _, err := Cfg.GetSection(modeSec); err != nil {
- fmt.Printf("Unknown log mode: %s\n", mode)
+ fmt.Printf("Unknown log mode: %s\n", LogMode)
os.Exit(2)
}
// Log level.
- levelName := Cfg.MustValue("log."+mode, "LEVEL", "Trace")
+ levelName := Cfg.MustValue("log."+LogMode, "LEVEL", "Trace")
level, ok := logLevels[levelName]
if !ok {
fmt.Printf("Unknown log level: %s\n", levelName)
@@ -86,14 +102,13 @@ func newLogService() {
}
// Generate log configuration.
- var config string
- switch mode {
+ switch LogMode {
case "console":
- config = fmt.Sprintf(`{"level":%s}`, level)
+ LogConfig = fmt.Sprintf(`{"level":%s}`, level)
case "file":
logPath := Cfg.MustValue(modeSec, "FILE_NAME", "log/gogs.log")
os.MkdirAll(path.Dir(logPath), os.ModePerm)
- config = fmt.Sprintf(
+ LogConfig = fmt.Sprintf(
`{"level":%s,"filename":%s,"rotate":%v,"maxlines":%d,"maxsize",%d,"daily":%v,"maxdays":%d}`, level,
logPath,
Cfg.MustBool(modeSec, "LOG_ROTATE", true),
@@ -102,13 +117,13 @@ func newLogService() {
Cfg.MustBool(modeSec, "DAILY_ROTATE", true),
Cfg.MustInt(modeSec, "MAX_DAYS", 7))
case "conn":
- config = fmt.Sprintf(`{"level":%s,"reconnectOnMsg":%v,"reconnect":%v,"net":%s,"addr":%s}`, level,
+ LogConfig = fmt.Sprintf(`{"level":%s,"reconnectOnMsg":%v,"reconnect":%v,"net":%s,"addr":%s}`, level,
Cfg.MustBool(modeSec, "RECONNECT_ON_MSG", false),
Cfg.MustBool(modeSec, "RECONNECT", false),
Cfg.MustValue(modeSec, "PROTOCOL", "tcp"),
Cfg.MustValue(modeSec, "ADDR", ":7020"))
case "smtp":
- config = fmt.Sprintf(`{"level":%s,"username":%s,"password":%s,"host":%s,"sendTos":%s,"subject":%s}`, level,
+ LogConfig = fmt.Sprintf(`{"level":%s,"username":%s,"password":%s,"host":%s,"sendTos":%s,"subject":%s}`, level,
Cfg.MustValue(modeSec, "USER", "example@example.com"),
Cfg.MustValue(modeSec, "PASSWD", "******"),
Cfg.MustValue(modeSec, "HOST", "127.0.0.1:25"),
@@ -116,8 +131,32 @@ func newLogService() {
Cfg.MustValue(modeSec, "SUBJECT", "Diagnostic message from serve"))
}
- log.NewLogger(Cfg.MustInt64("log", "BUFFER_LEN", 10000), mode, config)
- log.Info("Log Mode: %s(%s)", strings.Title(mode), levelName)
+ log.NewLogger(Cfg.MustInt64("log", "BUFFER_LEN", 10000), LogMode, LogConfig)
+ log.Info("Log Mode: %s(%s)", strings.Title(LogMode), levelName)
+}
+
+func newCacheService() {
+ CacheAdapter = Cfg.MustValue("cache", "ADAPTER", "memory")
+
+ switch CacheAdapter {
+ case "memory":
+ CacheConfig = fmt.Sprintf(`{"interval":%d}`, Cfg.MustInt("cache", "INTERVAL", 60))
+ case "redis", "memcache":
+ CacheConfig = fmt.Sprintf(`{"conn":"%s"}`, Cfg.MustValue("cache", "HOST"))
+ default:
+ fmt.Printf("Unknown cache adapter: %s\n", CacheAdapter)
+ os.Exit(2)
+ }
+
+ var err error
+ Cache, err = cache.NewCache(CacheAdapter, CacheConfig)
+ if err != nil {
+ fmt.Printf("Init cache system failed, adapter: %s, config: %s, %v\n",
+ CacheAdapter, CacheConfig, err)
+ os.Exit(2)
+ }
+
+ log.Info("Cache Service Enabled")
}
func newMailService() {
@@ -144,7 +183,7 @@ func newRegisterMailService() {
log.Info("Register Mail Service Enabled")
}
-func init() {
+func NewConfigContext() {
var err error
workDir, err := exeDir()
if err != nil {
@@ -173,11 +212,20 @@ func init() {
AppUrl = Cfg.MustValue("server", "ROOT_URL")
Domain = Cfg.MustValue("server", "DOMAIN")
SecretKey = Cfg.MustValue("security", "SECRET_KEY")
+ RunUser = Cfg.MustValue("", "RUN_USER")
+
+ // Determine and create root git reposiroty path.
+ RepoRootPath = Cfg.MustValue("repository", "ROOT")
+ if err = os.MkdirAll(RepoRootPath, os.ModePerm); err != nil {
+ fmt.Printf("models.init(fail to create RepoRootPath(%s)): %v\n", RepoRootPath, err)
+ os.Exit(2)
+ }
}
func NewServices() {
newService()
newLogService()
+ newCacheService()
newMailService()
newRegisterMailService()
}