github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/setting/storage.go (about) 1 // Copyright 2023 The GitBundle Inc. All rights reserved. 2 // Copyright 2017 The Gitea Authors. All rights reserved. 3 // Use of this source code is governed by a MIT-style 4 // license that can be found in the LICENSE file. 5 6 package setting 7 8 import ( 9 "path/filepath" 10 "reflect" 11 12 ini "gopkg.in/ini.v1" 13 ) 14 15 // Storage represents configuration of storages 16 type Storage struct { 17 Type string 18 Path string 19 Section *ini.Section 20 ServeDirect bool 21 } 22 23 // MapTo implements the Mappable interface 24 func (s *Storage) MapTo(v interface{}) error { 25 pathValue := reflect.ValueOf(v).Elem().FieldByName("Path") 26 if pathValue.IsValid() && pathValue.Kind() == reflect.String { 27 pathValue.SetString(s.Path) 28 } 29 if s.Section != nil { 30 return s.Section.MapTo(v) 31 } 32 return nil 33 } 34 35 func getStorage(name, typ string, targetSec *ini.Section) Storage { 36 const sectionName = "storage" 37 sec := Cfg.Section(sectionName) 38 39 // Global Defaults 40 sec.Key("MINIO_ENDPOINT").MustString("localhost:9000") 41 sec.Key("MINIO_ACCESS_KEY_ID").MustString("") 42 sec.Key("MINIO_SECRET_ACCESS_KEY").MustString("") 43 sec.Key("MINIO_BUCKET").MustString("gitbundle") 44 sec.Key("MINIO_LOCATION").MustString("us-east-1") 45 sec.Key("MINIO_USE_SSL").MustBool(false) 46 47 if targetSec == nil { 48 targetSec, _ = Cfg.NewSection(name) 49 } 50 51 var storage Storage 52 storage.Section = targetSec 53 storage.Type = typ 54 55 overrides := make([]*ini.Section, 0, 3) 56 nameSec, err := Cfg.GetSection(sectionName + "." + name) 57 if err == nil { 58 overrides = append(overrides, nameSec) 59 } 60 61 typeSec, err := Cfg.GetSection(sectionName + "." + typ) 62 if err == nil { 63 overrides = append(overrides, typeSec) 64 nextType := typeSec.Key("STORAGE_TYPE").String() 65 if len(nextType) > 0 { 66 storage.Type = nextType // Support custom STORAGE_TYPE 67 } 68 } 69 overrides = append(overrides, sec) 70 71 for _, override := range overrides { 72 for _, key := range override.Keys() { 73 if !targetSec.HasKey(key.Name()) { 74 _, _ = targetSec.NewKey(key.Name(), key.Value()) 75 } 76 } 77 if len(storage.Type) == 0 { 78 storage.Type = override.Key("STORAGE_TYPE").String() 79 } 80 } 81 storage.ServeDirect = storage.Section.Key("SERVE_DIRECT").MustBool(false) 82 83 // Specific defaults 84 storage.Path = storage.Section.Key("PATH").MustString(filepath.Join(AppDataPath, name)) 85 if !filepath.IsAbs(storage.Path) { 86 storage.Path = filepath.Join(AppWorkPath, storage.Path) 87 storage.Section.Key("PATH").SetValue(storage.Path) 88 } 89 storage.Section.Key("MINIO_BASE_PATH").MustString(name + "/") 90 91 return storage 92 }