/
dos.go
67 lines (54 loc) · 1.55 KB
/
dos.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
package cacher
import (
"context"
"hash"
"sync"
"github.com/goproxy/goproxy"
)
// DOS implements the `goproxy.Cacher` by using the DigitalOcean Spaces.
type DOS struct {
// Endpoint is the endpoint of the DigitalOcean Spaces.
//
// If the `Endpoint` is empty, the "https://nyc3.digitaloceanspaces.com"
// is used.
Endpoint string `mapstructure:"endpoint"`
// AccessKey is the access key of the DigitalOcean.
AccessKey string `mapstructure:"access_key"`
// SecretKey is the secret key of the DigitalOcean.
SecretKey string `mapstructure:"secret_key"`
// SpaceName is the name of the space.
SpaceName string `mapstructure:"space_name"`
// Root is the root of the caches.
Root string `mapstructure:"root"`
loadOnce sync.Once
minio *MinIO
}
// load loads the stuff of the m up.
func (d *DOS) load() {
endpoint := d.Endpoint
if endpoint == "" {
endpoint = "https://nyc3.digitaloceanspaces.com"
}
d.minio = &MinIO{
Endpoint: endpoint,
AccessKeyID: d.AccessKey,
SecretAccessKey: d.SecretKey,
BucketName: d.SpaceName,
VirtualHosted: true,
Root: d.Root,
}
}
// NewHash implements the `goproxy.Cacher`.
func (d *DOS) NewHash() hash.Hash {
return d.minio.NewHash()
}
// Cache implements the `goproxy.Cacher`.
func (d *DOS) Cache(ctx context.Context, name string) (goproxy.Cache, error) {
d.loadOnce.Do(d.load)
return d.minio.Cache(ctx, name)
}
// SetCache implements the `goproxy.Cacher`.
func (d *DOS) SetCache(ctx context.Context, c goproxy.Cache) error {
d.loadOnce.Do(d.load)
return d.minio.SetCache(ctx, c)
}