Skip to content

Commit

Permalink
refractor: bucket service
Browse files Browse the repository at this point in the history
  • Loading branch information
imstevez committed Aug 31, 2023
1 parent bbed9cd commit d34b4c6
Show file tree
Hide file tree
Showing 15 changed files with 903 additions and 860 deletions.
5 changes: 2 additions & 3 deletions s3/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/bittorrent/go-btfs/s3/ctxmu"
"github.com/bittorrent/go-btfs/s3/responses"
"github.com/bittorrent/go-btfs/s3/services/accesskey"
"github.com/bittorrent/go-btfs/s3/services/bucket"
"github.com/bittorrent/go-btfs/s3/services/object"
"github.com/bittorrent/go-btfs/s3/services/sign"
"net/http"
Expand All @@ -26,14 +25,14 @@ type Handlers struct {

acksvc accesskey.Service
sigsvc sign.Service
bucsvc bucket.Service
bucsvc object.Service
objsvc object.Service
}

func NewHandlers(
acksvc accesskey.Service,
sigsvc sign.Service,
bucsvc bucket.Service,
bucsvc object.Service,
objsvc object.Service,
options ...Option,
) (handlers *Handlers) {
Expand Down
4 changes: 2 additions & 2 deletions s3/handlers/handlers_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/bittorrent/go-btfs/s3/requests"
"github.com/bittorrent/go-btfs/s3/responses"
"github.com/bittorrent/go-btfs/s3/s3utils"
"github.com/bittorrent/go-btfs/s3/services/bucket"
"github.com/bittorrent/go-btfs/s3/services/object"
"net/http"
)

Expand Down Expand Up @@ -202,7 +202,7 @@ func (h *Handlers) HeadBucketHandler(w http.ResponseWriter, r *http.Request) {
ack := cctx.GetAccessKey(r)

err = h.bucsvc.CheckACL(ack, req.Bucket, s3action.HeadBucketAction)
if errors.Is(err, bucket.ErrNotFound) {
if errors.Is(err, object.ErrBucketNotFound) {
responses.WriteErrorResponse(w, r, responses.ErrNoSuchBucket)
return
}
Expand Down
9 changes: 4 additions & 5 deletions s3/handlers/handlers_multipart.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/bittorrent/go-btfs/s3/requests"
"github.com/bittorrent/go-btfs/s3/responses"
"github.com/bittorrent/go-btfs/s3/s3utils"
"github.com/bittorrent/go-btfs/s3/services/bucket"
"github.com/bittorrent/go-btfs/s3/services/object"
"github.com/bittorrent/go-btfs/s3/utils"
"github.com/bittorrent/go-btfs/s3/utils/hash"
Expand Down Expand Up @@ -58,7 +57,7 @@ func (h *Handlers) CreateMultipartUploadHandler(w http.ResponseWriter, r *http.R
defer unlock()

err = h.bucsvc.CheckACL(ack, bucname, action.CreateMultipartUploadAction)
if errors.Is(err, bucket.ErrNotFound) {
if errors.Is(err, object.ErrBucketNotFound) {
responses.WriteErrorResponse(w, r, responses.ErrNoSuchBucket)
return
}
Expand Down Expand Up @@ -158,7 +157,7 @@ func (h *Handlers) UploadPartHandler(w http.ResponseWriter, r *http.Request) {
defer unlock()

err = h.bucsvc.CheckACL(ack, bucname, action.PutObjectAction)
if errors.Is(err, bucket.ErrNotFound) {
if errors.Is(err, object.ErrBucketNotFound) {
responses.WriteErrorResponse(w, r, responses.ErrNoSuchBucket)
return
}
Expand Down Expand Up @@ -220,7 +219,7 @@ func (h *Handlers) AbortMultipartUploadHandler(w http.ResponseWriter, r *http.Re
defer unlock()

err = h.bucsvc.CheckACL(ack, bucname, action.AbortMultipartUploadAction)
if errors.Is(err, bucket.ErrNotFound) {
if errors.Is(err, object.ErrBucketNotFound) {
responses.WriteErrorResponse(w, r, responses.ErrNoSuchBucket)
return
}
Expand Down Expand Up @@ -303,7 +302,7 @@ func (h *Handlers) CompleteMultipartUploadHandler(w http.ResponseWriter, r *http
defer unlock()

err = h.bucsvc.CheckACL(ack, bucname, action.CompleteMultipartUploadAction)
if errors.Is(err, bucket.ErrNotFound) {
if errors.Is(err, object.ErrBucketNotFound) {
responses.WriteErrorResponse(w, r, responses.ErrNoSuchBucket)
return
}
Expand Down
21 changes: 10 additions & 11 deletions s3/handlers/handlers_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/bittorrent/go-btfs/s3/requests"
"github.com/bittorrent/go-btfs/s3/responses"
"github.com/bittorrent/go-btfs/s3/s3utils"
"github.com/bittorrent/go-btfs/s3/services/bucket"
"github.com/bittorrent/go-btfs/s3/services/object"
"github.com/bittorrent/go-btfs/s3/utils/hash"
)
Expand Down Expand Up @@ -91,7 +90,7 @@ func (h *Handlers) PutObjectHandler(w http.ResponseWriter, r *http.Request) {
defer unlock()

err = h.bucsvc.CheckACL(ack, bucname, action.PutObjectAction)
if errors.Is(err, bucket.ErrNotFound) {
if errors.Is(err, object.ErrBucketNotFound) {
responses.WriteErrorResponse(w, r, responses.ErrNoSuchBucket)
return
}
Expand Down Expand Up @@ -133,7 +132,7 @@ func (h *Handlers) HeadObjectHandler(w http.ResponseWriter, r *http.Request) {
}

err = h.bucsvc.CheckACL(ack, bucname, action.HeadObjectAction)
if errors.Is(err, bucket.ErrNotFound) {
if errors.Is(err, object.ErrBucketNotFound) {
responses.WriteErrorResponse(w, r, responses.ErrNoSuchBucket)
return
}
Expand Down Expand Up @@ -192,7 +191,7 @@ func (h *Handlers) CopyObjectHandler(w http.ResponseWriter, r *http.Request) {
return
}
err = h.bucsvc.CheckACL(ack, dstBucket, action.CopyObjectAction)
if errors.Is(err, bucket.ErrNotFound) {
if errors.Is(err, object.ErrBucketNotFound) {
responses.WriteErrorResponse(w, r, responses.ErrNoSuchBucket)
return
}
Expand Down Expand Up @@ -222,7 +221,7 @@ func (h *Handlers) CopyObjectHandler(w http.ResponseWriter, r *http.Request) {
return
}
err = h.bucsvc.CheckACL(ack, srcBucket, action.CopyObjectAction)
if errors.Is(err, bucket.ErrNotFound) {
if errors.Is(err, object.ErrBucketNotFound) {
responses.WriteErrorResponse(w, r, responses.ErrNoSuchBucket)
return
}
Expand Down Expand Up @@ -320,7 +319,7 @@ func (h *Handlers) DeleteObjectHandler(w http.ResponseWriter, r *http.Request) {
}

err = h.bucsvc.CheckACL(ack, bucname, action.DeleteObjectAction)
if errors.Is(err, bucket.ErrNotFound) {
if errors.Is(err, object.ErrBucketNotFound) {
responses.WriteErrorResponse(w, r, responses.ErrNoSuchBucket)
return
}
Expand Down Expand Up @@ -380,7 +379,7 @@ func (h *Handlers) DeleteObjectsHandler(w http.ResponseWriter, r *http.Request)
}

err = h.bucsvc.CheckACL(ack, bucname, action.DeleteObjectAction)
if errors.Is(err, bucket.ErrNotFound) {
if errors.Is(err, object.ErrBucketNotFound) {
responses.WriteErrorResponse(w, r, responses.ErrNoSuchBucket)
return
}
Expand Down Expand Up @@ -440,7 +439,7 @@ func (h *Handlers) GetObjectHandler(w http.ResponseWriter, r *http.Request) {
}

err = h.bucsvc.CheckACL(ack, bucname, action.GetObjectAction)
if errors.Is(err, bucket.ErrNotFound) {
if errors.Is(err, object.ErrBucketNotFound) {
responses.WriteErrorResponse(w, r, responses.ErrNoSuchBucket)
return
}
Expand Down Expand Up @@ -497,7 +496,7 @@ func (h *Handlers) GetObjectACLHandler(w http.ResponseWriter, r *http.Request) {
}

err = h.bucsvc.CheckACL(ack, bucname, action.GetBucketAclAction)
if errors.Is(err, bucket.ErrNotFound) {
if errors.Is(err, object.ErrBucketNotFound) {
responses.WriteErrorResponse(w, r, responses.ErrNoSuchBucket)
return
}
Expand Down Expand Up @@ -556,7 +555,7 @@ func (h *Handlers) ListObjectsHandler(w http.ResponseWriter, r *http.Request) {
defer runlock()

err = h.bucsvc.CheckACL(ack, bucname, action.ListObjectsAction)
if errors.Is(err, bucket.ErrNotFound) {
if errors.Is(err, object.ErrBucketNotFound) {
responses.WriteErrorResponse(w, r, responses.ErrNoSuchBucket)
return
}
Expand Down Expand Up @@ -591,7 +590,7 @@ func (h *Handlers) ListObjectsV2Handler(w http.ResponseWriter, r *http.Request)
}

err = h.bucsvc.CheckACL(ack, bucname, action.ListObjectsAction)
if errors.Is(err, bucket.ErrNotFound) {
if errors.Is(err, object.ErrBucketNotFound) {
responses.WriteErrorResponse(w, r, responses.ErrNoSuchBucket)
return
}
Expand Down
4 changes: 2 additions & 2 deletions s3/providers/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ var (
)

type Providerser interface {
GetFileStore() FileStorer
GetStateStore() StateStorer
FileStore() FileStorer
StateStore() StateStorer
}

type FileStorer interface {
Expand Down
4 changes: 2 additions & 2 deletions s3/providers/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ func NewProviders(stateStore StateStorer, fileStore FileStorer, options ...Optio
return
}

func (p *Providers) GetStateStore() StateStorer {
func (p *Providers) StateStore() StateStorer {
return p.stateStore
}

func (p *Providers) GetFileStore() FileStorer {
func (p *Providers) FileStore() FileStorer {
return p.fileStore
}
3 changes: 1 addition & 2 deletions s3/responses/wirters.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/bittorrent/go-btfs/s3/consts"
"github.com/bittorrent/go-btfs/s3/services/bucket"
"github.com/bittorrent/go-btfs/s3/services/object"
"net/http"
)
Expand All @@ -25,7 +24,7 @@ func WriteDeleteBucketResponse(w http.ResponseWriter) {
return
}

func WriteListBucketsResponse(w http.ResponseWriter, r *http.Request, bucketMetas []*bucket.Bucket) {
func WriteListBucketsResponse(w http.ResponseWriter, r *http.Request, bucketMetas []*object.Bucket) {
var buckets []*s3.Bucket
for _, b := range bucketMetas {
buckets = append(buckets, &s3.Bucket{
Expand Down
12 changes: 6 additions & 6 deletions s3/services/accesskey/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (svc *service) Generate() (record *AccessKey, err error) {
CreatedAt: now,
UpdatedAt: now,
}
err = svc.providers.GetStateStore().Put(svc.getStoreKey(record.Key), record)
err = svc.providers.StateStore().Put(svc.getStoreKey(record.Key), record)
return
}

Expand Down Expand Up @@ -89,7 +89,7 @@ func (svc *service) Delete(key string) (err error) {

func (svc *service) Get(key string) (ack *AccessKey, err error) {
ack = &AccessKey{}
err = svc.providers.GetStateStore().Get(svc.getStoreKey(key), ack)
err = svc.providers.StateStore().Get(svc.getStoreKey(key), ack)
if err != nil && !errors.Is(err, providers.ErrStateStoreNotFound) {
return
}
Expand All @@ -100,9 +100,9 @@ func (svc *service) Get(key string) (ack *AccessKey, err error) {
}

func (svc *service) List() (list []*AccessKey, err error) {
err = svc.providers.GetStateStore().Iterate(svc.storeKeyPrefix, func(key, _ []byte) (stop bool, er error) {
err = svc.providers.StateStore().Iterate(svc.storeKeyPrefix, func(key, _ []byte) (stop bool, er error) {
record := &AccessKey{}
er = svc.providers.GetStateStore().Get(string(key), record)
er = svc.providers.StateStore().Get(string(key), record)
if er != nil {
return
}
Expand Down Expand Up @@ -149,7 +149,7 @@ func (svc *service) update(key string, args *updateArgs) (err error) {
record := &AccessKey{}
stk := svc.getStoreKey(key)

err = svc.providers.GetStateStore().Get(stk, record)
err = svc.providers.StateStore().Get(stk, record)
if err != nil && !errors.Is(err, storage.ErrNotFound) {
return
}
Expand All @@ -170,7 +170,7 @@ func (svc *service) update(key string, args *updateArgs) (err error) {

record.UpdatedAt = time.Now()

err = svc.providers.GetStateStore().Put(stk, record)
err = svc.providers.StateStore().Put(stk, record)

return
}
32 changes: 0 additions & 32 deletions s3/services/bucket/proto.go

This file was deleted.

Loading

0 comments on commit d34b4c6

Please sign in to comment.