From bd555551ce6d4b26cbce14b778b05d573b4aa4a4 Mon Sep 17 00:00:00 2001 From: Unknwon Date: Wed, 31 Dec 2014 18:37:29 +0800 Subject: fix #801 --- modules/setting/setting.go | 201 +++++++++++++++++++++++---------------------- 1 file changed, 103 insertions(+), 98 deletions(-) (limited to 'modules/setting/setting.go') diff --git a/modules/setting/setting.go b/modules/setting/setting.go index cda50b8e..c4d3d3a6 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -16,9 +16,9 @@ import ( "time" "github.com/Unknwon/com" - "github.com/Unknwon/goconfig" "github.com/macaron-contrib/oauth2" "github.com/macaron-contrib/session" + "gopkg.in/ini.v1" "github.com/gogits/gogs/modules/log" // "github.com/gogits/gogs/modules/ssh" @@ -115,7 +115,7 @@ var ( Langs, Names []string // Global setting objects. - Cfg *goconfig.ConfigFile + Cfg *ini.File ConfRootPath string CustomPath string // Custom directory path. ProdMode bool @@ -156,7 +156,7 @@ func NewConfigContext() { } ConfRootPath = path.Join(workDir, "conf") - Cfg, err = goconfig.LoadConfigFile(path.Join(workDir, "conf/app.ini")) + Cfg, err = ini.Load(path.Join(workDir, "conf/app.ini")) if err != nil { log.Fatal(4, "Fail to parse 'conf/app.ini': %v", err) } @@ -168,15 +168,18 @@ func NewConfigContext() { cfgPath := path.Join(CustomPath, "conf/app.ini") if com.IsFile(cfgPath) { - if err = Cfg.AppendFiles(cfgPath); err != nil { + if err = Cfg.Append(cfgPath); err != nil { log.Fatal(4, "Fail to load custom 'conf/app.ini': %v", err) } } else { log.Warn("No custom 'conf/app.ini' found, please go to '/install'") } - AppName = Cfg.MustValue("", "APP_NAME", "Gogs: Go Git Service") - AppUrl = Cfg.MustValue("server", "ROOT_URL", "http://localhost:3000/") + LogRootPath = Cfg.Section("log").Key("ROOT_PATH").MustString(path.Join(workDir, "log")) + + sec := Cfg.Section("server") + AppName = Cfg.Section("").Key("APP_NAME").MustString("Gogs: Go Git Service") + AppUrl = sec.Key("ROOT_URL").MustString("http://localhost:3000/") if AppUrl[len(AppUrl)-1] != '/' { AppUrl += "/" } @@ -189,43 +192,43 @@ func NewConfigContext() { AppSubUrl = strings.TrimSuffix(url.Path, "/") Protocol = HTTP - if Cfg.MustValue("server", "PROTOCOL") == "https" { + if sec.Key("PROTOCOL").String() == "https" { Protocol = HTTPS - CertFile = Cfg.MustValue("server", "CERT_FILE") - KeyFile = Cfg.MustValue("server", "KEY_FILE") - } - if Cfg.MustValue("server", "PROTOCOL") == "fcgi" { + CertFile = sec.Key("CERT_FILE").String() + KeyFile = sec.Key("KEY_FILE").String() + } else if sec.Key("PROTOCOL").String() == "fcgi" { Protocol = FCGI } - Domain = Cfg.MustValue("server", "DOMAIN", "localhost") - HttpAddr = Cfg.MustValue("server", "HTTP_ADDR", "0.0.0.0") - HttpPort = Cfg.MustValue("server", "HTTP_PORT", "3000") - SshPort = Cfg.MustInt("server", "SSH_PORT", 22) - OfflineMode = Cfg.MustBool("server", "OFFLINE_MODE") - DisableRouterLog = Cfg.MustBool("server", "DISABLE_ROUTER_LOG") - StaticRootPath = Cfg.MustValue("server", "STATIC_ROOT_PATH", workDir) - LogRootPath = Cfg.MustValue("log", "ROOT_PATH", path.Join(workDir, "log")) - EnableGzip = Cfg.MustBool("server", "ENABLE_GZIP") - - switch Cfg.MustValue("server", "LANDING_PAGE", "home") { + Domain = sec.Key("DOMAIN").MustString("localhost") + HttpAddr = sec.Key("HTTP_ADDR").MustString("0.0.0.0") + HttpPort = sec.Key("HTTP_PORT").MustString("3000") + SshPort = sec.Key("SSH_PORT").MustInt(22) + OfflineMode = sec.Key("OFFLINE_MODE").MustBool() + DisableRouterLog = sec.Key("DISABLE_ROUTER_LOG").MustBool() + StaticRootPath = sec.Key("STATIC_ROOT_PATH").MustString(workDir) + EnableGzip = sec.Key("ENABLE_GZIP").MustBool() + + switch sec.Key("LANDING_PAGE").MustString("home") { case "explore": LandingPageUrl = LANDING_PAGE_EXPLORE default: LandingPageUrl = LANDING_PAGE_HOME } - InstallLock = Cfg.MustBool("security", "INSTALL_LOCK") - SecretKey = Cfg.MustValue("security", "SECRET_KEY") - LogInRememberDays = Cfg.MustInt("security", "LOGIN_REMEMBER_DAYS") - CookieUserName = Cfg.MustValue("security", "COOKIE_USERNAME") - CookieRememberName = Cfg.MustValue("security", "COOKIE_REMEMBER_NAME") - ReverseProxyAuthUser = Cfg.MustValue("security", "REVERSE_PROXY_AUTHENTICATION_USER", "X-WEBAUTH-USER") - - AttachmentPath = Cfg.MustValue("attachment", "PATH", "data/attachments") - AttachmentAllowedTypes = Cfg.MustValue("attachment", "ALLOWED_TYPES", "image/jpeg|image/png") - AttachmentMaxSize = Cfg.MustInt64("attachment", "MAX_SIZE", 32) - AttachmentMaxFiles = Cfg.MustInt("attachment", "MAX_FILES", 10) - AttachmentEnabled = Cfg.MustBool("attachment", "ENABLE", true) + sec = Cfg.Section("security") + InstallLock = sec.Key("INSTALL_LOCK").MustBool() + SecretKey = sec.Key("SECRET_KEY").String() + LogInRememberDays = sec.Key("LOGIN_REMEMBER_DAYS").MustInt() + CookieUserName = sec.Key("COOKIE_USERNAME").String() + CookieRememberName = sec.Key("COOKIE_REMEMBER_NAME").String() + ReverseProxyAuthUser = sec.Key("REVERSE_PROXY_AUTHENTICATION_USER").MustString("X-WEBAUTH-USER") + + sec = Cfg.Section("attachment") + AttachmentPath = sec.Key("PATH").MustString("data/attachments") + AttachmentAllowedTypes = sec.Key("ALLOWED_TYPES").MustString("image/jpeg|image/png") + AttachmentMaxSize = sec.Key("MAX_SIZE").MustInt64(32) + AttachmentMaxFiles = sec.Key("MAX_FILES").MustInt(10) + AttachmentEnabled = sec.Key("ENABLE").MustBool(true) TimeFormat = map[string]string{ "ANSIC": time.ANSIC, @@ -243,13 +246,13 @@ func NewConfigContext() { "StampMilli": time.StampMilli, "StampMicro": time.StampMicro, "StampNano": time.StampNano, - }[Cfg.MustValue("time", "FORMAT", "RFC1123")] + }[Cfg.Section("time").Key("FORMAT").MustString("RFC1123")] if err = os.MkdirAll(AttachmentPath, os.ModePerm); err != nil { log.Fatal(4, "Could not create directory %s: %s", AttachmentPath, err) } - RunUser = Cfg.MustValue("", "RUN_USER") + RunUser = Cfg.Section("").Key("RUN_USER").String() curUser := os.Getenv("USER") if len(curUser) == 0 { curUser = os.Getenv("USERNAME") @@ -264,36 +267,37 @@ func NewConfigContext() { if err != nil { log.Fatal(4, "Fail to get home directory: %v", err) } - RepoRootPath = Cfg.MustValue("repository", "ROOT", filepath.Join(homeDir, "gogs-repositories")) + sec = Cfg.Section("repository") + RepoRootPath = sec.Key("ROOT").MustString(filepath.Join(homeDir, "gogs-repositories")) if !filepath.IsAbs(RepoRootPath) { RepoRootPath = filepath.Join(workDir, RepoRootPath) } else { RepoRootPath = filepath.Clean(RepoRootPath) } - if err = os.MkdirAll(RepoRootPath, os.ModePerm); err != nil { log.Fatal(4, "Fail to create repository root path(%s): %v", RepoRootPath, err) } - ScriptType = Cfg.MustValue("repository", "SCRIPT_TYPE", "bash") + ScriptType = sec.Key("SCRIPT_TYPE").MustString("bash") - PictureService = Cfg.MustValueRange("picture", "SERVICE", "server", []string{"server"}) - AvatarUploadPath = Cfg.MustValue("picture", "AVATAR_UPLOAD_PATH", "data/avatars") + sec = Cfg.Section("picture") + PictureService = sec.Key("SERVICE").In("server", []string{"server"}) + AvatarUploadPath = sec.Key("AVATAR_UPLOAD_PATH").MustString("data/avatars") os.MkdirAll(AvatarUploadPath, os.ModePerm) - - switch Cfg.MustValue("picture", "GRAVATAR_SOURCE", "gravatar") { + switch sec.Key("GRAVATAR_SOURCE").MustString("gravatar") { case "duoshuo": GravatarSource = "http://gravatar.duoshuo.com/avatar/" default: GravatarSource = "//1.gravatar.com/avatar/" } - DisableGravatar = Cfg.MustBool("picture", "DISABLE_GRAVATAR") + DisableGravatar = sec.Key("DISABLE_GRAVATAR").MustBool() - MaxGitDiffLines = Cfg.MustInt("git", "MAX_GITDIFF_LINES", 10000) - GitFsckArgs = Cfg.MustValueArray("git", "FSCK_ARGS", " ") - GitGcArgs = Cfg.MustValueArray("git", "GC_ARGS", " ") + sec = Cfg.Section("git") + MaxGitDiffLines = sec.Key("MAX_GITDIFF_LINES").MustInt(10000) + GitFsckArgs = sec.Key("FSCK_ARGS").Strings(" ") + GitGcArgs = sec.Key("GC_ARGS").Strings(" ") - Langs = Cfg.MustValueArray("i18n", "LANGS", ",") - Names = Cfg.MustValueArray("i18n", "NAMES", ",") + Langs = Cfg.Section("i18n").Key("LANGS").Strings(",") + Names = Cfg.Section("i18n").Key("NAMES").Strings(",") HasRobotsTxt = com.IsFile(path.Join(CustomPath, "robots.txt")) } @@ -311,13 +315,13 @@ var Service struct { } func newService() { - Service.ActiveCodeLives = Cfg.MustInt("service", "ACTIVE_CODE_LIVE_MINUTES", 180) - Service.ResetPwdCodeLives = Cfg.MustInt("service", "RESET_PASSWD_CODE_LIVE_MINUTES", 180) - Service.DisableRegistration = Cfg.MustBool("service", "DISABLE_REGISTRATION") - Service.RequireSignInView = Cfg.MustBool("service", "REQUIRE_SIGNIN_VIEW") - Service.EnableCacheAvatar = Cfg.MustBool("service", "ENABLE_CACHE_AVATAR") - Service.EnableReverseProxyAuth = Cfg.MustBool("service", "ENABLE_REVERSE_PROXY_AUTHENTICATION") - Service.EnableReverseProxyAutoRegister = Cfg.MustBool("service", "ENABLE_REVERSE_PROXY_AUTO_REGISTERATION") + Service.ActiveCodeLives = Cfg.Section("service").Key("ACTIVE_CODE_LIVE_MINUTES").MustInt(180) + Service.ResetPwdCodeLives = Cfg.Section("service").Key("RESET_PASSWD_CODE_LIVE_MINUTES").MustInt(180) + Service.DisableRegistration = Cfg.Section("service").Key("DISABLE_REGISTRATION").MustBool() + Service.RequireSignInView = Cfg.Section("service").Key("REQUIRE_SIGNIN_VIEW").MustBool() + Service.EnableCacheAvatar = Cfg.Section("service").Key("ENABLE_CACHE_AVATAR").MustBool() + Service.EnableReverseProxyAuth = Cfg.Section("service").Key("ENABLE_REVERSE_PROXY_AUTHENTICATION").MustBool() + Service.EnableReverseProxyAutoRegister = Cfg.Section("service").Key("ENABLE_REVERSE_PROXY_AUTO_REGISTERATION").MustBool() } var logLevels = map[string]string{ @@ -333,17 +337,17 @@ func newLogService() { log.Info("%s %s", AppName, AppVer) // Get and check log mode. - LogModes = strings.Split(Cfg.MustValue("log", "MODE", "console"), ",") + LogModes = strings.Split(Cfg.Section("log").Key("MODE").MustString("console"), ",") LogConfigs = make([]string, len(LogModes)) for i, mode := range LogModes { mode = strings.TrimSpace(mode) - modeSec := "log." + mode - if _, err := Cfg.GetSection(modeSec); err != nil { + sec, err := Cfg.GetSection("log." + mode) + if err != nil { log.Fatal(4, "Unknown log mode: %s", mode) } // Log level. - levelName := Cfg.MustValueRange("log."+mode, "LEVEL", "Trace", + levelName := Cfg.Section("log."+mode).Key("LEVEL").In("Trace", []string{"Trace", "Debug", "Info", "Warn", "Error", "Critical"}) level, ok := logLevels[levelName] if !ok { @@ -355,42 +359,42 @@ func newLogService() { case "console": LogConfigs[i] = fmt.Sprintf(`{"level":%s}`, level) case "file": - logPath := Cfg.MustValue(modeSec, "FILE_NAME", path.Join(LogRootPath, "gogs.log")) + logPath := sec.Key("FILE_NAME").MustString(path.Join(LogRootPath, "gogs.log")) os.MkdirAll(path.Dir(logPath), os.ModePerm) LogConfigs[i] = fmt.Sprintf( `{"level":%s,"filename":"%s","rotate":%v,"maxlines":%d,"maxsize":%d,"daily":%v,"maxdays":%d}`, level, logPath, - Cfg.MustBool(modeSec, "LOG_ROTATE", true), - Cfg.MustInt(modeSec, "MAX_LINES", 1000000), - 1<