summaryrefslogtreecommitdiffstats
path: root/modules/setting/setting.go
diff options
context:
space:
mode:
authorwxiaoguang <wxiaoguang@gmail.com>2023-06-23 00:27:18 +0800
committerGitHub <noreply@github.com>2023-06-22 16:27:18 +0000
commit061b68e9959ebe45c7e3b9da9fa6bc9db87f4bb2 (patch)
tree5b8af2361e0aa32bdd9bf3c0b433d01ca7b3a0fd /modules/setting/setting.go
parent734fd93f59370846aa957ca57a054329d7d936dd (diff)
downloadgitea-061b68e9959ebe45c7e3b9da9fa6bc9db87f4bb2.tar.gz
gitea-061b68e9959ebe45c7e3b9da9fa6bc9db87f4bb2.zip
Refactor path & config system (#25330) (#25416)
Backport #25330 # The problem There were many "path tricks": * By default, Gitea uses its program directory as its work path * Gitea tries to use the "work path" to guess its "custom path" and "custom conf (app.ini)" * Users might want to use other directories as work path * The non-default work path should be passed to Gitea by GITEA_WORK_DIR or "--work-path" * But some Gitea processes are started without these values * The "serv" process started by OpenSSH server * The CLI sub-commands started by site admin * The paths are guessed by SetCustomPathAndConf again and again * The default values of "work path / custom path / custom conf" can be changed when compiling # The solution * Use `InitWorkPathAndCommonConfig` to handle these path tricks, and use test code to cover its behaviors. * When Gitea's web server runs, write the WORK_PATH to "app.ini", this value must be the most correct one, because if this value is not right, users would find that the web UI doesn't work and then they should be able to fix it. * Then all other sub-commands can use the WORK_PATH in app.ini to initialize their paths. * By the way, when Gitea starts for git protocol, it shouldn't output any log, otherwise the git protocol gets broken and client blocks forever. The "work path" priority is: WORK_PATH in app.ini > cmd arg --work-path > env var GITEA_WORK_DIR > builtin default The "app.ini" searching order is: cmd arg --config > cmd arg "work path / custom path" > env var "work path / custom path" > builtin default ## ⚠️ BREAKING If your instance's "work path / custom path / custom conf" doesn't meet the requirements (eg: work path must be absolute), Gitea will report a fatal error and exit. You need to set these values according to the error log.
Diffstat (limited to 'modules/setting/setting.go')
-rw-r--r--modules/setting/setting.go131
1 files changed, 14 insertions, 117 deletions
diff --git a/modules/setting/setting.go b/modules/setting/setting.go
index 6eaddbe2b5..0d69847dbe 100644
--- a/modules/setting/setting.go
+++ b/modules/setting/setting.go
@@ -5,12 +5,8 @@
package setting
import (
- "errors"
"fmt"
"os"
- "os/exec"
- "path"
- "path/filepath"
"runtime"
"strings"
"time"
@@ -28,19 +24,9 @@ var (
// AppStartTime store time gitea has started
AppStartTime time.Time
- // AppPath represents the path to the gitea binary
- AppPath string
- // AppWorkPath is the "working directory" of Gitea. It maps to the environment variable GITEA_WORK_DIR.
- // If that is not set it is the default set here by the linker or failing that the directory of AppPath.
- //
- // AppWorkPath is used as the base path for several other paths.
- AppWorkPath string
-
// Other global setting objects
CfgProvider ConfigProvider
- CustomPath string // Custom directory path
- CustomConf string
RunMode string
RunUser string
IsProd bool
@@ -51,62 +37,6 @@ var (
IsInTesting = false
)
-func getAppPath() (string, error) {
- var appPath string
- var err error
- if IsWindows && filepath.IsAbs(os.Args[0]) {
- appPath = filepath.Clean(os.Args[0])
- } else {
- appPath, err = exec.LookPath(os.Args[0])
- }
-
- if err != nil {
- if !errors.Is(err, exec.ErrDot) {
- return "", err
- }
- appPath, err = filepath.Abs(os.Args[0])
- }
- if err != nil {
- return "", err
- }
- appPath, err = filepath.Abs(appPath)
- if err != nil {
- return "", err
- }
- // Note: we don't use path.Dir here because it does not handle case
- // which path starts with two "/" in Windows: "//psf/Home/..."
- return strings.ReplaceAll(appPath, "\\", "/"), err
-}
-
-func getWorkPath(appPath string) string {
- workPath := AppWorkPath
-
- if giteaWorkPath, ok := os.LookupEnv("GITEA_WORK_DIR"); ok {
- workPath = giteaWorkPath
- }
- if len(workPath) == 0 {
- i := strings.LastIndex(appPath, "/")
- if i == -1 {
- workPath = appPath
- } else {
- workPath = appPath[:i]
- }
- }
- workPath = strings.ReplaceAll(workPath, "\\", "/")
- if !filepath.IsAbs(workPath) {
- log.Info("Provided work path %s is not absolute - will be made absolute against the current working directory", workPath)
-
- absPath, err := filepath.Abs(workPath)
- if err != nil {
- log.Error("Unable to absolute %s against the current working directory %v. Will absolute against the AppPath %s", workPath, err, appPath)
- workPath = filepath.Join(appPath, workPath)
- } else {
- workPath = absPath
- }
- }
- return strings.ReplaceAll(workPath, "\\", "/")
-}
-
func init() {
IsWindows = runtime.GOOS == "windows"
if AppVer == "" {
@@ -116,12 +46,6 @@ func init() {
// We can rely on log.CanColorStdout being set properly because modules/log/console_windows.go comes before modules/setting/setting.go lexicographically
// By default set this logger at Info - we'll change it later, but we need to start with something.
log.SetConsoleLogger(log.DEFAULT, "console", log.INFO)
-
- var err error
- if AppPath, err = getAppPath(); err != nil {
- log.Fatal("Failed to get app path: %v", err)
- }
- AppWorkPath = getWorkPath(AppPath)
}
// IsRunUserMatchCurrentUser returns false if configured run user does not match
@@ -137,36 +61,6 @@ func IsRunUserMatchCurrentUser(runUser string) (string, bool) {
return currentUser, runUser == currentUser
}
-// SetCustomPathAndConf will set CustomPath and CustomConf with reference to the
-// GITEA_CUSTOM environment variable and with provided overrides before stepping
-// back to the default
-func SetCustomPathAndConf(providedCustom, providedConf, providedWorkPath string) {
- if len(providedWorkPath) != 0 {
- AppWorkPath = filepath.ToSlash(providedWorkPath)
- }
- if giteaCustom, ok := os.LookupEnv("GITEA_CUSTOM"); ok {
- CustomPath = giteaCustom
- }
- if len(providedCustom) != 0 {
- CustomPath = providedCustom
- }
- if len(CustomPath) == 0 {
- CustomPath = path.Join(AppWorkPath, "custom")
- } else if !filepath.IsAbs(CustomPath) {
- CustomPath = path.Join(AppWorkPath, CustomPath)
- }
-
- if len(providedConf) != 0 {
- CustomConf = providedConf
- }
- if len(CustomConf) == 0 {
- CustomConf = path.Join(CustomPath, "conf/app.ini")
- } else if !filepath.IsAbs(CustomConf) {
- CustomConf = path.Join(CustomPath, CustomConf)
- log.Warn("Using 'custom' directory as relative origin for configuration file: '%s'", CustomConf)
- }
-}
-
// PrepareAppDataPath creates app data directory if necessary
func PrepareAppDataPath() error {
// FIXME: There are too many calls to MkdirAll in old code. It is incorrect.
@@ -196,20 +90,23 @@ func PrepareAppDataPath() error {
return nil
}
-func Init(opts *Options) {
- if opts.CustomConf == "" {
- opts.CustomConf = CustomConf
- }
+func InitCfgProvider(file string, extraConfigs ...string) {
var err error
- CfgProvider, err = NewConfigProviderFromFile(opts)
+ if CfgProvider, err = NewConfigProviderFromFile(file, extraConfigs...); err != nil {
+ log.Fatal("Unable to init config provider from %q: %v", file, err)
+ }
CfgProvider.DisableSaving() // do not allow saving the CfgProvider into file, it will be polluted by the "MustXxx" calls
- if err != nil {
- log.Fatal("newConfigProviderFromFile[%v]: %v", opts, err)
+}
+
+func MustInstalled() {
+ if !InstallLock {
+ log.Fatal(`Unable to load config file for a installed Gitea instance, you should either use "--config" to set your config file (app.ini), or run "gitea web" command to install Gitea.`)
}
- if !opts.DisableLoadCommonSettings {
- if err := loadCommonSettingsFrom(CfgProvider); err != nil {
- log.Fatal("loadCommonSettingsFrom[%v]: %v", opts, err)
- }
+}
+
+func LoadCommonSettings() {
+ if err := loadCommonSettingsFrom(CfgProvider); err != nil {
+ log.Fatal("Unable to load settings from config: %v", err)
}
}