-
Notifications
You must be signed in to change notification settings - Fork 2
/
apps.go
112 lines (89 loc) · 2.2 KB
/
apps.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
// Copyright (c) ClaceIO, LLC
// SPDX-License-Identifier: Apache-2.0
package server
import (
"fmt"
"strings"
"sync"
"github.com/claceio/clace/internal/app"
"github.com/claceio/clace/internal/utils"
)
// AppStore is a store of apps. Apps are initialized lazily, the first GetApp call on each app
// will load the app from the database.
type AppStore struct {
*utils.Logger
server *Server
allApps []utils.AppInfo
mu sync.RWMutex
appMap map[utils.AppPathDomain]*app.App
}
func NewAppStore(logger *utils.Logger, server *Server) *AppStore {
return &AppStore{
Logger: logger,
server: server,
appMap: make(map[utils.AppPathDomain]*app.App),
}
}
func (a *AppStore) GetAllApps() ([]utils.AppInfo, error) {
a.mu.Lock()
defer a.mu.Unlock()
if a.allApps != nil {
return a.allApps, nil
}
var err error
a.allApps, err = a.server.db.GetAllApps(true)
if err != nil {
return nil, err
}
return a.allApps, nil
}
func (a *AppStore) ClearAllAppCache() {
a.mu.Lock()
defer a.mu.Unlock()
a.allApps = nil
}
func (a *AppStore) GetApp(pathDomain utils.AppPathDomain) (*app.App, error) {
a.mu.RLock()
defer a.mu.RUnlock()
app, ok := a.appMap[pathDomain]
if !ok {
return nil, fmt.Errorf("app not found: %s", pathDomain)
}
return app, nil
}
func (a *AppStore) AddApp(app *app.App) {
a.mu.Lock()
defer a.mu.Unlock()
a.appMap[utils.CreateAppPathDomain(app.Path, app.Domain)] = app
a.allApps = nil
}
func (a *AppStore) DeleteLinkedApps(pathDomain utils.AppPathDomain) error {
a.mu.Lock()
defer a.mu.Unlock()
linkedAppPrefix := pathDomain.Path + utils.INTERNAL_APP_DELIM
for key, app := range a.appMap {
if app.Domain == pathDomain.Domain && strings.HasPrefix(app.Path, linkedAppPrefix) {
delete(a.appMap, key)
}
}
delete(a.appMap, pathDomain)
a.allApps = nil
return nil
}
func (a *AppStore) DeleteApps(pathDomain []utils.AppPathDomain) {
a.mu.Lock()
defer a.mu.Unlock()
for _, pd := range pathDomain {
delete(a.appMap, pd)
}
a.allApps = nil
}
func (a *AppStore) UpdateApps(apps []*app.App) {
a.mu.Lock()
defer a.mu.Unlock()
for _, app := range apps {
app.ResetFS() // clear the transaction for DbFS
a.appMap[utils.CreateAppPathDomain(app.Path, app.Domain)] = app
}
a.allApps = nil
}