-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
40 changed files
with
1,781 additions
and
591 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ build: | |
|
||
.PHONY: run | ||
run: | ||
go run | ||
go run ./... | ||
|
||
.PHONY: lint | ||
lint: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package s3manager | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/gofiber/fiber/v2" | ||
) | ||
|
||
// HandleBucketList renders all buckets as an HTML list. | ||
func (s *S3Manager) HandleBucketList(c *fiber.Ctx) error { | ||
buckets, err := s.s3.ListBuckets(c.Context()) | ||
if err != nil { | ||
return fmt.Errorf("error listing buckets: %w", err) | ||
} | ||
|
||
return c.Render("bucket-list", fiber.Map{ | ||
"Buckets": buckets, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package s3manager_test | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"io" | ||
"net/http" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/cloudlena/s3manager/internal/s3manager" | ||
"github.com/cloudlena/s3manager/internal/s3manager/mocks" | ||
"github.com/gofiber/fiber/v2" | ||
"github.com/gofiber/template/html/v2" | ||
"github.com/matryer/is" | ||
"github.com/minio/minio-go/v7" | ||
) | ||
|
||
func TestHandleBucketList(t *testing.T) { | ||
t.Parallel() | ||
|
||
cases := []struct { | ||
it string | ||
listBucketsVal []minio.BucketInfo | ||
listBucketsErr error | ||
expectedStatusCode int | ||
expectedBodyContains string | ||
}{ | ||
{ | ||
it: "renders a list of buckets", | ||
listBucketsVal: []minio.BucketInfo{{Name: "BUCKET-NAME"}}, | ||
expectedStatusCode: http.StatusOK, | ||
expectedBodyContains: "BUCKET-NAME", | ||
}, | ||
{ | ||
it: "renders placeholder if no buckets", | ||
expectedStatusCode: http.StatusOK, | ||
expectedBodyContains: "No buckets yet", | ||
}, | ||
{ | ||
it: "returns error if there is an S3 error", | ||
listBucketsErr: errors.New("mocked s3 error"), | ||
expectedStatusCode: http.StatusInternalServerError, | ||
expectedBodyContains: "mocked s3 error", | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
tc := tc | ||
t.Run(tc.it, func(t *testing.T) { | ||
t.Parallel() | ||
is := is.New(t) | ||
|
||
s3 := &mocks.S3Mock{ | ||
ListBucketsFunc: func(ctx context.Context) ([]minio.BucketInfo, error) { | ||
return tc.listBucketsVal, tc.listBucketsErr | ||
}, | ||
} | ||
server := s3manager.New(s3, true, "", "") | ||
|
||
engine := html.New("../../views", ".html.gotmpl") | ||
app := fiber.New(fiber.Config{ | ||
Views: engine, | ||
}) | ||
app.Get("/bucket-list", server.HandleBucketList) | ||
|
||
req, err := http.NewRequest(fiber.MethodGet, "/bucket-list", nil) | ||
is.NoErr(err) | ||
|
||
resp, err := app.Test(req) | ||
is.NoErr(err) | ||
defer resp.Body.Close() | ||
body, err := io.ReadAll(resp.Body) | ||
is.NoErr(err) | ||
|
||
is.Equal(resp.StatusCode, tc.expectedStatusCode) // status code | ||
is.True(strings.Contains(string(body), tc.expectedBodyContains)) // body | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package s3manager | ||
|
||
import ( | ||
"github.com/gofiber/fiber/v2" | ||
) | ||
|
||
// HandleBucketsView renders all buckets on an HTML page. | ||
func (s *S3Manager) HandleBucketsView(c *fiber.Ctx) error { | ||
return c.Render("buckets", fiber.Map{}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package s3manager_test | ||
|
||
import ( | ||
"io" | ||
"net/http" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/cloudlena/s3manager/internal/s3manager" | ||
"github.com/gofiber/fiber/v2" | ||
"github.com/gofiber/template/html/v2" | ||
"github.com/matryer/is" | ||
) | ||
|
||
func TestHandleBucketsView(t *testing.T) { | ||
t.Parallel() | ||
|
||
cases := []struct { | ||
it string | ||
expectedStatusCode int | ||
expectedBodyContains string | ||
}{ | ||
{ | ||
it: "renders the buckets page", | ||
expectedStatusCode: http.StatusOK, | ||
expectedBodyContains: "S3 Manager", | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
tc := tc | ||
t.Run(tc.it, func(t *testing.T) { | ||
t.Parallel() | ||
is := is.New(t) | ||
|
||
server := s3manager.New(nil, true, "", "") | ||
|
||
engine := html.New("../../views", ".html.gotmpl") | ||
app := fiber.New(fiber.Config{ | ||
Views: engine, | ||
}) | ||
app.Get("/buckets", server.HandleBucketsView) | ||
|
||
req, err := http.NewRequest(fiber.MethodGet, "/buckets", nil) | ||
is.NoErr(err) | ||
|
||
resp, err := app.Test(req) | ||
is.NoErr(err) | ||
defer resp.Body.Close() | ||
body, err := io.ReadAll(resp.Body) | ||
is.NoErr(err) | ||
|
||
is.Equal(resp.StatusCode, tc.expectedStatusCode) // status code | ||
is.True(strings.Contains(string(body), tc.expectedBodyContains)) // body | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package s3manager | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/gofiber/fiber/v2" | ||
"github.com/minio/minio-go/v7" | ||
) | ||
|
||
// HandleCreateBucket creates a new bucket. | ||
func (s *S3Manager) HandleCreateBucket(c *fiber.Ctx) error { | ||
name := c.FormValue("name") | ||
if strings.TrimSpace(name) == "" { | ||
return fiber.NewError(fiber.StatusBadRequest, "name is required") | ||
} | ||
|
||
err := s.s3.MakeBucket(c.Context(), name, minio.MakeBucketOptions{}) | ||
if err != nil { | ||
return fmt.Errorf("error making bucket: %w", err) | ||
} | ||
|
||
c.Response().Header.Set("HX-Trigger", "bucketListChanged") | ||
return c.SendStatus(fiber.StatusCreated) | ||
} |
Oops, something went wrong.