71
71
AppSubURLDepth int // Number of slashes
72
72
AppPath string
73
73
AppDataPath string
74
+ AppWorkPath string
74
75
75
76
// Server settings
76
77
Protocol Scheme
@@ -525,53 +526,57 @@ func DateLang(lang string) string {
525
526
return "en"
526
527
}
527
528
528
- // execPath returns the executable path.
529
- func execPath () (string , error ) {
530
- execFile := os .Args [0 ]
531
- if IsWindows && filepath .IsAbs (execFile ) {
532
- return filepath .Clean (execFile ), nil
529
+ func getAppPath () (string , error ) {
530
+ var appPath string
531
+ var err error
532
+ if IsWindows && filepath .IsAbs (os .Args [0 ]) {
533
+ appPath = filepath .Clean (os .Args [0 ])
534
+ } else {
535
+ appPath , err = exec .LookPath (os .Args [0 ])
533
536
}
534
- file , err := exec . LookPath ( execFile )
537
+
535
538
if err != nil {
536
539
return "" , err
537
540
}
538
- return filepath .Abs (file )
541
+ appPath , err = filepath .Abs (appPath )
542
+ if err != nil {
543
+ return "" , err
544
+ }
545
+ // Note: we don't use path.Dir here because it does not handle case
546
+ // which path starts with two "/" in Windows: "//psf/Home/..."
547
+ return strings .Replace (appPath , "\\ " , "/" , - 1 ), err
548
+ }
549
+
550
+ func getWorkPath (appPath string ) string {
551
+ workPath := ""
552
+ giteaWorkPath := os .Getenv ("GITEA_WORK_DIR" )
553
+ gogsWorkPath := os .Getenv ("GOGS_WORK_DIR" )
554
+
555
+ if len (giteaWorkPath ) > 0 {
556
+ workPath = giteaWorkPath
557
+ } else if len (gogsWorkPath ) > 0 {
558
+ log .Warn (`Usage of GOGS_WORK_DIR is deprecated and will be *removed* in a future release, please consider changing to GITEA_WORK_DIR` )
559
+ workPath = gogsWorkPath
560
+ } else {
561
+ i := strings .LastIndex (appPath , "/" )
562
+ if i == - 1 {
563
+ workPath = appPath
564
+ } else {
565
+ workPath = appPath [:i ]
566
+ }
567
+ }
568
+ return strings .Replace (workPath , "\\ " , "/" , - 1 )
539
569
}
540
570
541
571
func init () {
542
572
IsWindows = runtime .GOOS == "windows"
543
573
log .NewLogger (0 , "console" , `{"level": 0}` )
544
574
545
575
var err error
546
- if AppPath , err = execPath (); err != nil {
576
+ if AppPath , err = getAppPath (); err != nil {
547
577
log .Fatal (4 , "Failed to get app path: %v" , err )
548
578
}
549
-
550
- // Note: we don't use path.Dir here because it does not handle case
551
- // which path starts with two "/" in Windows: "//psf/Home/..."
552
- AppPath = strings .Replace (AppPath , "\\ " , "/" , - 1 )
553
- }
554
-
555
- // WorkDir returns absolute path of work directory.
556
- func WorkDir () (string , error ) {
557
- wd := os .Getenv ("GITEA_WORK_DIR" )
558
- if len (wd ) > 0 {
559
- return wd , nil
560
- }
561
- // Use GOGS_WORK_DIR if available, for backward compatibility
562
- // TODO: drop in 1.1.0 ?
563
- wd = os .Getenv ("GOGS_WORK_DIR" )
564
- if len (wd ) > 0 {
565
- log .Warn (`Usage of GOGS_WORK_DIR is deprecated and will be *removed* in a future release,
566
- please consider changing to GITEA_WORK_DIR` )
567
- return wd , nil
568
- }
569
-
570
- i := strings .LastIndex (AppPath , "/" )
571
- if i == - 1 {
572
- return AppPath , nil
573
- }
574
- return AppPath [:i ], nil
579
+ AppWorkPath = getWorkPath (AppPath )
575
580
}
576
581
577
582
func forcePathSeparator (path string ) {
@@ -612,30 +617,27 @@ func createPIDFile(pidPath string) {
612
617
// NewContext initializes configuration context.
613
618
// NOTE: do not print any log except error.
614
619
func NewContext () {
615
- workDir , err := WorkDir ()
616
- if err != nil {
617
- log .Fatal (4 , "Failed to get work directory: %v" , err )
618
- }
619
-
620
620
Cfg = ini .Empty ()
621
621
622
622
CustomPath = os .Getenv ("GITEA_CUSTOM" )
623
623
if len (CustomPath ) == 0 {
624
- CustomPath = workDir + "/custom"
624
+ CustomPath = path .Join (AppWorkPath , "custom" )
625
+ } else if ! filepath .IsAbs (CustomPath ) {
626
+ CustomPath = path .Join (AppWorkPath , CustomPath )
625
627
}
626
628
627
629
if len (CustomPID ) > 0 {
628
630
createPIDFile (CustomPID )
629
631
}
630
632
631
633
if len (CustomConf ) == 0 {
632
- CustomConf = CustomPath + "/ conf/app.ini"
634
+ CustomConf = path . Join ( CustomPath , " conf/app.ini")
633
635
} else if ! filepath .IsAbs (CustomConf ) {
634
- CustomConf = filepath .Join (workDir , CustomConf )
636
+ CustomConf = path .Join (CustomPath , CustomConf )
635
637
}
636
638
637
639
if com .IsFile (CustomConf ) {
638
- if err = Cfg .Append (CustomConf ); err != nil {
640
+ if err : = Cfg .Append (CustomConf ); err != nil {
639
641
log .Fatal (4 , "Failed to load custom conf '%s': %v" , CustomConf , err )
640
642
}
641
643
} else {
@@ -649,7 +651,7 @@ func NewContext() {
649
651
}
650
652
homeDir = strings .Replace (homeDir , "\\ " , "/" , - 1 )
651
653
652
- LogRootPath = Cfg .Section ("log" ).Key ("ROOT_PATH" ).MustString (path .Join (workDir , "log" ))
654
+ LogRootPath = Cfg .Section ("log" ).Key ("ROOT_PATH" ).MustString (path .Join (AppWorkPath , "log" ))
653
655
forcePathSeparator (LogRootPath )
654
656
655
657
sec := Cfg .Section ("server" )
@@ -716,8 +718,8 @@ func NewContext() {
716
718
LocalURL = sec .Key ("LOCAL_ROOT_URL" ).MustString (defaultLocalURL )
717
719
OfflineMode = sec .Key ("OFFLINE_MODE" ).MustBool ()
718
720
DisableRouterLog = sec .Key ("DISABLE_ROUTER_LOG" ).MustBool ()
719
- StaticRootPath = sec .Key ("STATIC_ROOT_PATH" ).MustString (workDir )
720
- AppDataPath = sec .Key ("APP_DATA_PATH" ).MustString ("data" )
721
+ StaticRootPath = sec .Key ("STATIC_ROOT_PATH" ).MustString (AppWorkPath )
722
+ AppDataPath = sec .Key ("APP_DATA_PATH" ).MustString (path . Join ( AppWorkPath , "data" ) )
721
723
EnableGzip = sec .Key ("ENABLE_GZIP" ).MustBool ()
722
724
EnablePprof = sec .Key ("ENABLE_PPROF" ).MustBool (false )
723
725
@@ -783,7 +785,7 @@ func NewContext() {
783
785
}
784
786
LFS .ContentPath = sec .Key ("LFS_CONTENT_PATH" ).MustString (filepath .Join (AppDataPath , "lfs" ))
785
787
if ! filepath .IsAbs (LFS .ContentPath ) {
786
- LFS .ContentPath = filepath .Join (workDir , LFS .ContentPath )
788
+ LFS .ContentPath = filepath .Join (AppWorkPath , LFS .ContentPath )
787
789
}
788
790
789
791
if LFS .StartServer {
@@ -915,7 +917,7 @@ func NewContext() {
915
917
sec = Cfg .Section ("attachment" )
916
918
AttachmentPath = sec .Key ("PATH" ).MustString (path .Join (AppDataPath , "attachments" ))
917
919
if ! filepath .IsAbs (AttachmentPath ) {
918
- AttachmentPath = path .Join (workDir , AttachmentPath )
920
+ AttachmentPath = path .Join (AppWorkPath , AttachmentPath )
919
921
}
920
922
AttachmentAllowedTypes = strings .Replace (sec .Key ("ALLOWED_TYPES" ).MustString ("image/jpeg,image/png,application/zip,application/gzip" ), "|" , "," , - 1 )
921
923
AttachmentMaxSize = sec .Key ("MAX_SIZE" ).MustInt64 (4 )
@@ -969,7 +971,7 @@ func NewContext() {
969
971
RepoRootPath = sec .Key ("ROOT" ).MustString (path .Join (homeDir , "gitea-repositories" ))
970
972
forcePathSeparator (RepoRootPath )
971
973
if ! filepath .IsAbs (RepoRootPath ) {
972
- RepoRootPath = path .Join (workDir , RepoRootPath )
974
+ RepoRootPath = path .Join (AppWorkPath , RepoRootPath )
973
975
} else {
974
976
RepoRootPath = path .Clean (RepoRootPath )
975
977
}
@@ -985,14 +987,14 @@ func NewContext() {
985
987
}
986
988
987
989
if ! filepath .IsAbs (Repository .Upload .TempPath ) {
988
- Repository .Upload .TempPath = path .Join (workDir , Repository .Upload .TempPath )
990
+ Repository .Upload .TempPath = path .Join (AppWorkPath , Repository .Upload .TempPath )
989
991
}
990
992
991
993
sec = Cfg .Section ("picture" )
992
994
AvatarUploadPath = sec .Key ("AVATAR_UPLOAD_PATH" ).MustString (path .Join (AppDataPath , "avatars" ))
993
995
forcePathSeparator (AvatarUploadPath )
994
996
if ! filepath .IsAbs (AvatarUploadPath ) {
995
- AvatarUploadPath = path .Join (workDir , AvatarUploadPath )
997
+ AvatarUploadPath = path .Join (AppWorkPath , AvatarUploadPath )
996
998
}
997
999
switch source := sec .Key ("GRAVATAR_SOURCE" ).MustString ("gravatar" ); source {
998
1000
case "duoshuo" :
@@ -1254,7 +1256,7 @@ func NewXORMLogService(disableConsole bool) {
1254
1256
if err = os .MkdirAll (path .Dir (logPath ), os .ModePerm ); err != nil {
1255
1257
panic (err .Error ())
1256
1258
}
1257
- logPath = filepath .Join (filepath .Dir (logPath ), "xorm.log" )
1259
+ logPath = path .Join (filepath .Dir (logPath ), "xorm.log" )
1258
1260
1259
1261
logConfigs = fmt .Sprintf (
1260
1262
`{"level":%s,"filename":"%s","rotate":%v,"maxlines":%d,"maxsize":%d,"daily":%v,"maxdays":%d}` , level ,
@@ -1341,7 +1343,10 @@ func newCacheService() {
1341
1343
func newSessionService () {
1342
1344
SessionConfig .Provider = Cfg .Section ("session" ).Key ("PROVIDER" ).In ("memory" ,
1343
1345
[]string {"memory" , "file" , "redis" , "mysql" })
1344
- SessionConfig .ProviderConfig = strings .Trim (Cfg .Section ("session" ).Key ("PROVIDER_CONFIG" ).String (), "\" " )
1346
+ SessionConfig .ProviderConfig = strings .Trim (Cfg .Section ("session" ).Key ("PROVIDER_CONFIG" ).MustString (path .Join (AppDataPath , "sessions" )), "\" " )
1347
+ if ! filepath .IsAbs (SessionConfig .ProviderConfig ) {
1348
+ SessionConfig .ProviderConfig = path .Join (AppWorkPath , SessionConfig .ProviderConfig )
1349
+ }
1345
1350
SessionConfig .CookieName = Cfg .Section ("session" ).Key ("COOKIE_NAME" ).MustString ("i_like_gitea" )
1346
1351
SessionConfig .CookiePath = AppSubURL
1347
1352
SessionConfig .Secure = Cfg .Section ("session" ).Key ("COOKIE_SECURE" ).MustBool (false )
0 commit comments