-
Notifications
You must be signed in to change notification settings - Fork 496
/
main.go
118 lines (101 loc) · 2.55 KB
/
main.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package storage
import (
"context"
"io"
"net/http"
"net/url"
"path"
"strings"
log "github.com/sirupsen/logrus"
"github.com/stellar/go/support/errors"
)
type Storage interface {
Exists(path string) (bool, error)
Size(path string) (int64, error)
GetFile(path string) (io.ReadCloser, error)
PutFile(path string, in io.ReadCloser) error
ListFiles(path string) (chan string, chan error)
CanListFiles() bool
Close() error
}
type ConnectOptions struct {
Context context.Context
S3Region string
S3Endpoint string
UnsignedRequests bool
GCSEndpoint string
// When putting file object to s3 bucket, specify the ACL for the object.
S3WriteACL string
// UserAgent is the value of `User-Agent` header. Applicable only for HTTP
// client.
UserAgent string
// Wrap the Storage after connection. For example, to add a caching or
// introspection layer.
Wrap func(Storage) (Storage, error)
}
func ConnectBackend(u string, opts ConnectOptions) (Storage, error) {
if u == "" {
return nil, errors.New("URL is empty")
}
parsed, err := url.Parse(u)
if err != nil {
return nil, err
}
if opts.Context == nil {
opts.Context = context.Background()
}
pth := parsed.Path
var backend Storage
switch parsed.Scheme {
case "s3":
// Inside s3, all paths start _without_ the leading /
pth = strings.TrimPrefix(pth, "/")
backend, err = NewS3Storage(
opts.Context,
parsed.Host,
pth,
opts.S3Region,
opts.S3Endpoint,
opts.UnsignedRequests,
opts.S3WriteACL,
)
case "gcs":
// Inside gcs, all paths start _without_ the leading /
pth = strings.TrimPrefix(pth, "/")
backend, err = NewGCSBackend(
opts.Context,
parsed.Host,
pth,
opts.GCSEndpoint,
)
case "file":
pth = path.Join(parsed.Host, pth)
backend = NewFilesystemStorage(pth)
case "http", "https":
backend = NewHttpStorage(opts.Context, parsed, opts.UserAgent)
default:
err = errors.New("unknown URL scheme: '" + parsed.Scheme + "'")
}
if err == nil && opts.Wrap != nil {
backend, err = opts.Wrap(backend)
}
return backend, err
}
func logReq(r *http.Request) {
if r == nil {
return
}
logFields := log.Fields{"method": r.Method, "url": r.URL.String()}
log.WithFields(logFields).Trace("http: Req")
}
func logResp(r *http.Response) {
if r == nil || r.Request == nil {
return
}
logFields := log.Fields{"method": r.Request.Method, "status": r.Status, "url": r.Request.URL.String()}
if r.StatusCode >= 200 && r.StatusCode < 400 {
log.WithFields(logFields).Trace("http: OK")
} else {
log.WithFields(logFields).Warn("http: Bad")
}
}