You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

storage.go 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package setting
  4. import (
  5. "path/filepath"
  6. "reflect"
  7. )
  8. // Storage represents configuration of storages
  9. type Storage struct {
  10. Type string
  11. Path string
  12. Section ConfigSection
  13. ServeDirect bool
  14. }
  15. // MapTo implements the Mappable interface
  16. func (s *Storage) MapTo(v interface{}) error {
  17. pathValue := reflect.ValueOf(v).Elem().FieldByName("Path")
  18. if pathValue.IsValid() && pathValue.Kind() == reflect.String {
  19. pathValue.SetString(s.Path)
  20. }
  21. if s.Section != nil {
  22. return s.Section.MapTo(v)
  23. }
  24. return nil
  25. }
  26. func getStorage(rootCfg ConfigProvider, name, typ string, targetSec ConfigSection) Storage {
  27. const sectionName = "storage"
  28. sec := rootCfg.Section(sectionName)
  29. // Global Defaults
  30. sec.Key("MINIO_ENDPOINT").MustString("localhost:9000")
  31. sec.Key("MINIO_ACCESS_KEY_ID").MustString("")
  32. sec.Key("MINIO_SECRET_ACCESS_KEY").MustString("")
  33. sec.Key("MINIO_BUCKET").MustString("gitea")
  34. sec.Key("MINIO_LOCATION").MustString("us-east-1")
  35. sec.Key("MINIO_USE_SSL").MustBool(false)
  36. sec.Key("MINIO_INSECURE_SKIP_VERIFY").MustBool(false)
  37. sec.Key("MINIO_CHECKSUM_ALGORITHM").MustString("default")
  38. if targetSec == nil {
  39. targetSec, _ = rootCfg.NewSection(name)
  40. }
  41. var storage Storage
  42. storage.Section = targetSec
  43. storage.Type = typ
  44. overrides := make([]ConfigSection, 0, 3)
  45. nameSec, err := rootCfg.GetSection(sectionName + "." + name)
  46. if err == nil {
  47. overrides = append(overrides, nameSec)
  48. }
  49. typeSec, err := rootCfg.GetSection(sectionName + "." + typ)
  50. if err == nil {
  51. overrides = append(overrides, typeSec)
  52. nextType := typeSec.Key("STORAGE_TYPE").String()
  53. if len(nextType) > 0 {
  54. storage.Type = nextType // Support custom STORAGE_TYPE
  55. }
  56. }
  57. overrides = append(overrides, sec)
  58. for _, override := range overrides {
  59. for _, key := range override.Keys() {
  60. if !targetSec.HasKey(key.Name()) {
  61. _, _ = targetSec.NewKey(key.Name(), key.Value())
  62. }
  63. }
  64. if len(storage.Type) == 0 {
  65. storage.Type = override.Key("STORAGE_TYPE").String()
  66. }
  67. }
  68. storage.ServeDirect = storage.Section.Key("SERVE_DIRECT").MustBool(false)
  69. // Specific defaults
  70. storage.Path = storage.Section.Key("PATH").MustString(filepath.Join(AppDataPath, name))
  71. if !filepath.IsAbs(storage.Path) {
  72. storage.Path = filepath.Join(AppWorkPath, storage.Path)
  73. storage.Section.Key("PATH").SetValue(storage.Path)
  74. }
  75. storage.Section.Key("MINIO_BASE_PATH").MustString(name + "/")
  76. return storage
  77. }