/
service.go
168 lines (150 loc) · 4.58 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package module
import (
"cmp"
"context"
"path/filepath"
"slices"
"strings"
"sync"
"github.com/pkg/errors"
"github.com/samber/lo"
"golang.org/x/exp/maps"
"projectforge.dev/projectforge/app/lib/filesystem"
"projectforge.dev/projectforge/app/lib/search/result"
"projectforge.dev/projectforge/app/util"
)
var nativeModuleKeys = []string{
"android", "audit", "core", "csharp", "database", "databaseui", "desktop", "docbrowse", "export", "expression",
"filesystem", "graphql", "har", "help", "ios", "jsx", "marketing", "migration", "metamodel", util.DatabaseMySQL,
"notebook", "notarize", "oauth", "openapi", "playwright", util.DatabasePostgreSQL, "process", "queue",
"readonlydb", "richedit", "sandbox", "schedule", "scripting", "search", util.DatabaseSQLite, util.DatabaseSQLServer,
"themecatalog", "types", "upgrade", "user", "wasmclient", "wasmserver", "websocket",
}
type Service struct {
local filesystem.FileLoader
config filesystem.FileLoader
cache map[string]*Module
cacheMu sync.Mutex
filesystems map[string]filesystem.FileLoader
}
func NewService(ctx context.Context, root string, logger util.Logger) (*Service, error) {
local, err := filesystem.NewFileSystem("module", false, "")
if err != nil {
return nil, errors.Wrap(err, "unable to load config filesystem")
}
config, err := filesystem.NewFileSystem(filepath.Join(root, "module"), false, "")
if err != nil {
return nil, errors.Wrap(err, "unable to load config filesystem")
}
fs := map[string]filesystem.FileLoader{}
ret := &Service{local: local, config: config, cache: map[string]*Module{}, filesystems: fs}
_, err = ret.LoadNative(ctx, logger, nativeModuleKeys...)
if err != nil {
return nil, errors.Wrap(err, "unable to load native modules")
}
return ret, nil
}
func (s *Service) GetFilesystem(key string) filesystem.FileLoader {
mod, err := s.Get(key)
if err != nil {
if s.local.IsDir(key) {
return s.local
}
return s.config
}
return mod.Files
}
func (s *Service) AddIfNeeded(ctx context.Context, key string, path string, url string, logger util.Logger) (Modules, bool, error) {
s.cacheMu.Lock()
ret, ok := s.cache[key]
s.cacheMu.Unlock()
if ok {
if ret.URL != url {
logger.Warnf("module [%s] is loaded with url [%s] but there is another reference with url [%s]", ret.Key, ret.URL, url)
}
return Modules{ret}, false, nil
}
mods, err := s.load(ctx, key, path, url, logger)
if err != nil {
return nil, false, err
}
lo.ForEach(mods, func(mod *Module, _ int) {
s.cacheMu.Lock()
s.cache[mod.Key] = mod
s.cacheMu.Unlock()
})
return mods, true, nil
}
func (s *Service) Get(key string) (*Module, error) {
s.cacheMu.Lock()
ret, ok := s.cache[key]
s.cacheMu.Unlock()
if !ok {
return nil, errors.Errorf("no module available with key [%s]", key)
}
return ret, nil
}
func (s *Service) GetModules(keys ...string) (Modules, error) {
var ret Modules
for _, m := range keys {
mod, err := s.Get(m)
if err != nil {
return nil, err
}
ret = append(ret, mod)
}
return ret.Sort(), nil
}
func (s *Service) Keys() []string {
s.cacheMu.Lock()
defer s.cacheMu.Unlock()
return util.ArraySorted(lo.Keys(s.cache))
}
func (s *Service) Modules() Modules {
s.cacheMu.Lock()
defer s.cacheMu.Unlock()
var ret Modules = maps.Values(s.cache)
return ret.Sort()
}
func (s *Service) ModulesSorted() Modules {
ret := s.Modules()
slices.SortFunc(ret, func(l *Module, r *Module) int {
return cmp.Compare(strings.ToLower(l.Name), strings.ToLower(r.Name))
})
return ret
}
func (s *Service) ModulesVisible() Modules {
return lo.Filter(s.ModulesSorted(), func(m *Module, _ int) bool {
return !m.Hidden
})
}
func (s *Service) Deps() map[string][]string {
return lo.Associate(s.Modules(), func(m *Module) (string, []string) {
return m.Key, m.Requires
})
}
func (s *Service) Dangerous() []string {
return lo.FilterMap(s.ModulesSorted(), func(m *Module, _ int) (string, bool) {
return m.Key, m.Dangerous
})
}
func (s *Service) Register(ctx context.Context, root string, key string, path string, u string, logger util.Logger) ([]string, error) {
ret := &util.StringSlice{}
_, added, err := s.AddIfNeeded(ctx, key, filepath.Join(root, path), u, logger)
if err != nil {
return nil, err
}
if added {
ret.Push(key)
}
return ret.Slice, nil
}
func (s *Service) Search(_ context.Context, q string, _ util.Logger) (result.Results, error) {
return lo.FilterMap(s.Modules(), func(mod *Module, _ int) (*result.Result, bool) {
res := result.NewResult("module", mod.Key, mod.WebPath(), mod.Title(), mod.IconSafe(), mod, mod, q)
if len(res.Matches) > 0 {
return res, true
}
return nil, false
}), nil
}