-
Notifications
You must be signed in to change notification settings - Fork 3
/
apps.go
56 lines (44 loc) · 1.14 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
// Copyright (c) ClaceIO, LLC
// SPDX-License-Identifier: Apache-2.0
package server
import (
"fmt"
"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
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) AddApp(app *app.App) {
a.mu.Lock()
defer a.mu.Unlock()
a.appMap[utils.CreateAppPathDomain(app.Path, app.Domain)] = app
}
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) DeleteApp(pathDomain utils.AppPathDomain) error {
a.mu.Lock()
defer a.mu.Unlock()
delete(a.appMap, pathDomain)
return nil
}