-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.go
64 lines (51 loc) · 1.81 KB
/
cache.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
package services
import (
"encoding/json"
"fmt"
"github.com/cyops-se/safe-import/si-gatekeeper/common"
"github.com/cyops-se/safe-import/si-gatekeeper/types"
"github.com/cyops-se/safe-import/usvc"
)
type CacheService struct {
usvc.Usvc
}
func (svc *CacheService) Initialize(broker *usvc.UsvcBroker) {
svc.InitializeService(broker, 1, "si-gatekeeper", "proxy", "Proxies request from si-inner to si-outer")
svc.RegisterMethod("allitems", svc.allItems)
svc.RegisterMethod("byfieldname", svc.byFieldName)
svc.RegisterMethod("update", svc.update)
svc.RegisterMethod("prune", svc.prune)
// We don't use settings right now
if err := svc.LoadSettings(); err != nil {
svc.SaveSettings() // Save default settings. Though we don't actually use the settings right now...
}
svc.Executor = svc.execute
svc.SetTaskIdleTime(60 * 1) // every minute
svc.execute()
}
func (svc *CacheService) execute() {
}
func (svc *CacheService) allItems(payload string) (interface{}, error) {
var items []types.CachedItem
common.DB.Find(&items)
return items, nil
}
func (svc *CacheService) byFieldName(payload string) (interface{}, error) {
var args types.ByNameRequest
if err := json.Unmarshal([]byte(payload), &args); err != nil {
svc.LogGeneric("error", "Marshalling request to JSON failed: %#v", err)
return nil, err
}
var items []types.CachedItem
if result := common.DB.Where(map[string]interface{}{args.Name: args.Value}).Find(&items); result.Error != nil {
svc.LogGeneric("error", "Failed to query database, error: %#v", result.Error)
return nil, result.Error
}
return items, nil
}
func (svc *CacheService) update(payload string) (interface{}, error) {
return nil, fmt.Errorf("Method not yet implemented")
}
func (svc *CacheService) prune(payload string) (interface{}, error) {
return nil, fmt.Errorf("Method not yet implemented")
}