|
5 | 5 | package setting |
6 | 6 |
|
7 | 7 | import ( |
8 | | - "strings" |
| 8 | + "path/filepath" |
| 9 | + "reflect" |
9 | 10 |
|
10 | | - "code.gitea.io/gitea/modules/log" |
11 | 11 | ini "gopkg.in/ini.v1" |
12 | 12 | ) |
13 | 13 |
|
14 | | -// enumerate all storage types |
15 | | -const ( |
16 | | - LocalStorageType = "local" |
17 | | - MinioStorageType = "minio" |
18 | | -) |
19 | | - |
20 | 14 | // Storage represents configuration of storages |
21 | 15 | type Storage struct { |
22 | 16 | Type string |
23 | 17 | Path string |
| 18 | + Section *ini.Section |
24 | 19 | ServeDirect bool |
25 | | - Minio struct { |
26 | | - Endpoint string |
27 | | - AccessKeyID string |
28 | | - SecretAccessKey string |
29 | | - UseSSL bool |
30 | | - Bucket string |
31 | | - Location string |
32 | | - BasePath string |
| 20 | +} |
| 21 | + |
| 22 | +// MapTo implements the Mappable interface |
| 23 | +func (s *Storage) MapTo(v interface{}) error { |
| 24 | + pathValue := reflect.ValueOf(v).FieldByName("Path") |
| 25 | + if pathValue.IsValid() && pathValue.Kind() == reflect.String { |
| 26 | + pathValue.SetString(s.Path) |
| 27 | + } |
| 28 | + if s.Section != nil { |
| 29 | + return s.Section.MapTo(v) |
33 | 30 | } |
| 31 | + return nil |
34 | 32 | } |
35 | 33 |
|
36 | | -var ( |
37 | | - storages = make(map[string]Storage) |
38 | | -) |
| 34 | +func getStorage(name, typ string, overrides ...*ini.Section) Storage { |
| 35 | + sectionName := "storage" |
| 36 | + if len(name) > 0 { |
| 37 | + sectionName = sectionName + "." + typ |
| 38 | + } |
| 39 | + sec := Cfg.Section(sectionName) |
| 40 | + |
| 41 | + if len(overrides) == 0 { |
| 42 | + overrides = []*ini.Section{ |
| 43 | + Cfg.Section(sectionName + "." + name), |
| 44 | + } |
| 45 | + } |
39 | 46 |
|
40 | | -func getStorage(sec *ini.Section) Storage { |
41 | 47 | var storage Storage |
42 | | - storage.Type = sec.Key("STORAGE_TYPE").MustString(LocalStorageType) |
| 48 | + |
| 49 | + storage.Type = sec.Key("STORAGE_TYPE").MustString("") |
43 | 50 | storage.ServeDirect = sec.Key("SERVE_DIRECT").MustBool(false) |
44 | | - switch storage.Type { |
45 | | - case LocalStorageType: |
46 | | - case MinioStorageType: |
47 | | - storage.Minio.Endpoint = sec.Key("MINIO_ENDPOINT").MustString("localhost:9000") |
48 | | - storage.Minio.AccessKeyID = sec.Key("MINIO_ACCESS_KEY_ID").MustString("") |
49 | | - storage.Minio.SecretAccessKey = sec.Key("MINIO_SECRET_ACCESS_KEY").MustString("") |
50 | | - storage.Minio.Bucket = sec.Key("MINIO_BUCKET").MustString("gitea") |
51 | | - storage.Minio.Location = sec.Key("MINIO_LOCATION").MustString("us-east-1") |
52 | | - storage.Minio.UseSSL = sec.Key("MINIO_USE_SSL").MustBool(false) |
53 | | - } |
54 | | - return storage |
55 | | -} |
56 | 51 |
|
57 | | -func newStorageService() { |
58 | | - sec := Cfg.Section("storage") |
59 | | - storages["default"] = getStorage(sec) |
| 52 | + // Global Defaults |
| 53 | + sec.Key("MINIO_ENDPOINT").MustString("localhost:9000") |
| 54 | + sec.Key("MINIO_ACCESS_KEY_ID").MustString("") |
| 55 | + sec.Key("MINIO_SECRET_ACCESS_KEY").MustString("") |
| 56 | + sec.Key("MINIO_BUCKET").MustString("gitea") |
| 57 | + sec.Key("MINIO_LOCATION").MustString("us-east-1") |
| 58 | + sec.Key("MINIO_USE_SSL").MustBool(false) |
| 59 | + |
| 60 | + storage.Section = sec |
60 | 61 |
|
61 | | - for _, sec := range Cfg.Section("storage").ChildSections() { |
62 | | - name := strings.TrimPrefix(sec.Name(), "storage.") |
63 | | - if name == "default" || name == LocalStorageType || name == MinioStorageType { |
64 | | - log.Error("storage name %s is system reserved!", name) |
65 | | - continue |
| 62 | + for _, override := range overrides { |
| 63 | + for _, key := range storage.Section.Keys() { |
| 64 | + if !override.HasKey(key.Name()) { |
| 65 | + _, _ = override.NewKey(key.Name(), key.Value()) |
| 66 | + } |
66 | 67 | } |
67 | | - storages[name] = getStorage(sec) |
| 68 | + storage.ServeDirect = override.Key("SERVE_DIRECT").MustBool(false) |
| 69 | + storage.Section = override |
68 | 70 | } |
| 71 | + |
| 72 | + // Specific defaults |
| 73 | + storage.Path = storage.Section.Key("PATH").MustString(filepath.Join(AppDataPath, name)) |
| 74 | + if !filepath.IsAbs(storage.Path) { |
| 75 | + storage.Path = filepath.Join(AppWorkPath, storage.Path) |
| 76 | + storage.Section.Key("PATH").SetValue(storage.Path) |
| 77 | + } |
| 78 | + storage.Section.Key("MINIO_BASE_PATH").MustString(name + "/") |
| 79 | + |
| 80 | + return storage |
69 | 81 | } |
0 commit comments