-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
85 lines (73 loc) · 2.1 KB
/
server.go
File metadata and controls
85 lines (73 loc) · 2.1 KB
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
package actionscacheservice
import (
"time"
"github.com/actions-oss/cache-service/contracts"
"github.com/google/uuid"
)
type CacheRecord struct {
ID int64
Key string
Ref string
Version string
LastUpdated time.Time
Storage string
}
type CacheDatabase interface {
Add(record CacheRecord)
Find(reference, key, version string) *CacheRecord
}
type BlobStorage interface {
CreateSignedURL(filename string, write bool) string
}
type CacheService struct {
Ref string
DefaultRef string
Database CacheDatabase
Storage BlobStorage
}
func (c *CacheService) CreateCacheEntry(body *contracts.CreateCacheEntryRequest) *contracts.CreateCacheEntryResponse {
storage := uuid.NewString()
c.Database.Add(CacheRecord{
Key: body.Key,
LastUpdated: time.Now(),
Ref: c.Ref,
Version: body.Version,
Storage: storage,
})
return &contracts.CreateCacheEntryResponse{
Ok: true,
SignedUploadUrl: c.Storage.CreateSignedURL("cache/"+storage, true),
}
}
func (c *CacheService) FinalizeCacheEntryUpload(body *contracts.FinalizeCacheEntryUploadRequest) *contracts.FinalizeCacheEntryUploadResponse {
rec := c.Database.Find(c.Ref, body.Key, body.Version)
return &contracts.FinalizeCacheEntryUploadResponse{
Ok: true,
EntryId: int64(rec.ID),
}
}
func (c *CacheService) GetCacheEntryDownloadURL(body *contracts.GetCacheEntryDownloadURLRequest) *contracts.GetCacheEntryDownloadURLResponse {
restoreKeys := append([]string{body.Key}, body.RestoreKeys...)
version := body.Version
defaultRef := c.DefaultRef
reference := c.Ref
refs := []string{reference}
if reference != defaultRef {
refs = append(refs, defaultRef)
}
for _, cref := range refs {
for _, key := range restoreKeys {
record := c.Database.Find(cref, key, version)
if record != nil {
return &contracts.GetCacheEntryDownloadURLResponse{
Ok: true,
MatchedKey: key,
SignedDownloadUrl: c.Storage.CreateSignedURL("cache/"+record.Storage, false),
}
}
}
}
return &contracts.GetCacheEntryDownloadURLResponse{
Ok: false,
}
}