forked from gomods/athens
-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.go
68 lines (59 loc) · 2 KB
/
storage.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
package azurecdn
import (
"bytes"
"context"
"fmt"
"io"
"net/url"
"github.com/opentracing/opentracing-go"
"github.com/gomods/athens/pkg/config/env"
moduploader "github.com/gomods/athens/pkg/storage/module"
)
type client interface {
UploadWithContext(ctx context.Context, path, contentType string, content io.Reader) error
}
// Storage implements (github.com/gomods/athens/pkg/storage).Saver and
// also provides a function to fetch the location of a module
type Storage struct {
cl client
baseURI *url.URL
}
// New creates a new azure CDN saver
func New(accountName, accountKey, containerName string) (*Storage, error) {
u, err := url.Parse(fmt.Sprintf("https://%s.blob.core.windows.net", accountName))
if err != nil {
return nil, err
}
cl, err := newBlobStoreClient(u, accountName, accountKey, containerName)
if err != nil {
return nil, err
}
return &Storage{cl: cl, baseURI: u}, nil
}
// newWithClient creates a new azure CDN saver
func newWithClient(accountName, cl client) (*Storage, error) {
u, err := url.Parse(fmt.Sprintf("https://%s.blob.core.windows.net", accountName))
if err != nil {
return nil, err
}
return &Storage{cl: cl, baseURI: u}, nil
}
// BaseURL returns the base URL that stores all modules. It can be used
// in the "meta" tag redirect response to vgo.
//
// For example:
//
// <meta name="go-import" content="gomods.com/athens mod BaseURL()">
func (s Storage) BaseURL() *url.URL {
return env.CDNEndpointWithDefault(s.baseURI)
}
// Save implements the (github.com/gomods/athens/pkg/storage).Saver interface.
func (s *Storage) Save(ctx context.Context, module, version string, mod []byte, zip io.Reader, info []byte) error {
sp, ctx := opentracing.StartSpanFromContext(ctx, "storage.azurecdn.Save")
sp.Finish()
err := moduploader.Upload(ctx, module, version, bytes.NewReader(info), bytes.NewReader(mod), zip, s.cl.UploadWithContext)
// TODO: take out lease on the /list file and add the version to it
//
// Do that only after module source+metadata is uploaded
return err
}