-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add HTML presenter for access component (#12)
- Loading branch information
Showing
11 changed files
with
332 additions
and
0 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
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,102 @@ | ||
package html | ||
|
||
import ( | ||
"html/template" | ||
"net/http" | ||
"path" | ||
|
||
echo "github.com/labstack/echo/v4" | ||
|
||
"github.com/teran/archived/service" | ||
) | ||
|
||
type Handlers interface { | ||
ContainerIndex(c echo.Context) error | ||
VersionIndex(c echo.Context) error | ||
|
||
Register(e *echo.Echo) | ||
} | ||
|
||
type handlers struct { | ||
svc service.AccessService | ||
templateDir string | ||
} | ||
|
||
func New(svc service.AccessService, templateDir string) Handlers { | ||
return &handlers{ | ||
svc: svc, | ||
templateDir: templateDir, | ||
} | ||
} | ||
|
||
func (h *handlers) ContainerIndex(c echo.Context) error { | ||
containers, err := h.svc.ListContainers(c.Request().Context()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return c.Render(http.StatusOK, "container-list.html", containers) | ||
} | ||
|
||
func (h *handlers) VersionIndex(c echo.Context) error { | ||
container := c.Param("container") | ||
versions, err := h.svc.ListVersions(c.Request().Context(), container) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
type data struct { | ||
Container string | ||
Versions []string | ||
} | ||
|
||
return c.Render(http.StatusOK, "version-list.html", &data{ | ||
Container: container, | ||
Versions: versions, | ||
}) | ||
} | ||
|
||
func (h *handlers) ObjectIndex(c echo.Context) error { | ||
container := c.Param("container") | ||
version := c.Param("version") | ||
|
||
objects, err := h.svc.ListObjects(c.Request().Context(), container, version) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
type data struct { | ||
Container string | ||
Version string | ||
Objects []string | ||
} | ||
return c.Render(http.StatusOK, "object-list.html", &data{ | ||
Container: container, | ||
Version: version, | ||
Objects: objects, | ||
}) | ||
} | ||
|
||
func (h *handlers) GetObject(c echo.Context) error { | ||
container := c.Param("container") | ||
version := c.Param("version") | ||
object := c.Param("object") | ||
|
||
url, err := h.svc.GetObjectURL(c.Request().Context(), container, version, object) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return c.Redirect(http.StatusFound, url) | ||
} | ||
|
||
func (h *handlers) Register(e *echo.Echo) { | ||
e.Renderer = &renderer{ | ||
templates: template.Must(template.ParseGlob(path.Join(h.templateDir, "*.html"))), | ||
} | ||
|
||
e.GET("/", h.ContainerIndex) | ||
e.GET("/:container/", h.VersionIndex) | ||
e.GET("/:container/:version/", h.ObjectIndex) | ||
e.GET("/:container/:version/:object", h.GetObject) | ||
} |
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,100 @@ | ||
package html | ||
|
||
import ( | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"os" | ||
"testing" | ||
|
||
echo "github.com/labstack/echo/v4" | ||
"github.com/labstack/echo/v4/middleware" | ||
"github.com/stretchr/testify/suite" | ||
"github.com/teran/archived/service" | ||
) | ||
|
||
func (s *handlersTestSuite) TestContainerIndex() { | ||
s.serviceMock.On("ListContainers").Return([]string{"test-container-1"}, nil).Once() | ||
|
||
s.compareHTMLResponse(s.srv.URL, "testdata/index.html.sample") | ||
} | ||
|
||
func (s *handlersTestSuite) TestVersionIndex() { | ||
s.serviceMock.On("ListVersions", "test-container-1").Return([]string{"20241011121314"}, nil).Once() | ||
|
||
s.compareHTMLResponse(s.srv.URL+"/test-container-1/", "testdata/versions.html.sample") | ||
} | ||
|
||
func (s *handlersTestSuite) TestObjectIndex() { | ||
s.serviceMock.On("ListObjects", "test-container-1", "20241011121314").Return([]string{"test-object-dir/file.txt"}, nil).Once() | ||
|
||
s.compareHTMLResponse(s.srv.URL+"/test-container-1/20241011121314/", "testdata/objects.html.sample") | ||
} | ||
|
||
func (s *handlersTestSuite) TestGetObject() { | ||
s.serviceMock.On("GetObjectURL", "test-container-1", "20241011121314", "test-dir/filename.txt").Return("https://example.com/some-addr", nil).Once() | ||
|
||
req, err := http.NewRequest(http.MethodGet, s.srv.URL+"/test-container-1/20241011121314/test-dir/filename.txt", nil) | ||
s.Require().NoError(err) | ||
|
||
client := &http.Client{ | ||
CheckRedirect: func(req *http.Request, via []*http.Request) error { | ||
return http.ErrUseLastResponse | ||
}, | ||
} | ||
|
||
resp, err := client.Do(req) | ||
s.Require().NoError(err) | ||
|
||
v := resp.Header.Get("Location") | ||
s.Require().Equal("https://example.com/some-addr", v) | ||
} | ||
|
||
// Definitions ... | ||
type handlersTestSuite struct { | ||
suite.Suite | ||
|
||
srv *httptest.Server | ||
|
||
serviceMock *service.Mock | ||
handlers Handlers | ||
} | ||
|
||
func (s *handlersTestSuite) SetupTest() { | ||
e := echo.New() | ||
e.Use(middleware.Logger()) | ||
e.Use(middleware.Recover()) | ||
|
||
s.serviceMock = service.NewMock() | ||
|
||
s.handlers = New(s.serviceMock, "templates") | ||
s.handlers.Register(e) | ||
|
||
s.srv = httptest.NewServer(e) | ||
} | ||
|
||
func (s *handlersTestSuite) TearDownTest() { | ||
s.serviceMock.AssertExpectations(s.T()) | ||
|
||
s.srv.Close() | ||
} | ||
|
||
func TestHandlersTestSuite(t *testing.T) { | ||
suite.Run(t, &handlersTestSuite{}) | ||
} | ||
|
||
func (s *handlersTestSuite) compareHTMLResponse(url, responseSamplePath string) { | ||
req, err := http.NewRequest(http.MethodGet, url, nil) | ||
s.Require().NoError(err) | ||
|
||
resp, err := http.DefaultClient.Do(req) | ||
s.Require().NoError(err) | ||
|
||
sampleData, err := os.ReadFile(responseSamplePath) | ||
s.Require().NoError(err) | ||
|
||
data, err := io.ReadAll(resp.Body) | ||
s.Require().NoError(err) | ||
|
||
s.Require().Equal(sampleData, data) | ||
} |
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,16 @@ | ||
package html | ||
|
||
import ( | ||
"html/template" | ||
"io" | ||
|
||
echo "github.com/labstack/echo/v4" | ||
) | ||
|
||
type renderer struct { | ||
templates *template.Template | ||
} | ||
|
||
func (r *renderer) Render(w io.Writer, name string, data any, c echo.Context) error { | ||
return r.templates.ExecuteTemplate(w, name, data) | ||
} |
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,14 @@ | ||
<html> | ||
<head> | ||
<title>Container Index</title> | ||
</head> | ||
<body> | ||
<h1>Container index</h1> | ||
<hr> | ||
{{- range $container := . }} | ||
<a href="/{{ $container }}/">{{ $container }}</a><br> | ||
{{- end }} | ||
<hr> | ||
Powered by <a href="https://github.com/teran/archived" target="_blank">archived</a> | ||
</body> | ||
</html> |
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,17 @@ | ||
{{ $container := .Container }} | ||
{{ $version := .Version }} | ||
<html> | ||
<head> | ||
<title>Object Index ({{ $container }}/{{ $version }})</title> | ||
</head> | ||
<body> | ||
<h1>Object Index ({{ $container }}/{{ $version }})</h1> | ||
<hr> | ||
<a href="..">..</a><br> | ||
{{- range $object := .Objects }} | ||
<a href="/{{ $container }}/{{ $version }}/{{ $object }}">{{ $object }}</a><br> | ||
{{- end }} | ||
<hr> | ||
Powered by <a href="https://github.com/teran/archived" target="_blank">archived</a> | ||
</body> | ||
</html> |
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,16 @@ | ||
{{ $container := .Container }} | ||
<html> | ||
<head> | ||
<title>Version Index ({{ $container }})</title> | ||
</head> | ||
<body> | ||
<h1>Version Index ({{ $container }})</h1> | ||
<hr> | ||
<a href="..">..</a><br> | ||
{{- range $version := .Versions }} | ||
<a href="/{{ $container }}/{{ $version }}/">{{ $version }}</a><br> | ||
{{- end }} | ||
<hr> | ||
Powered by <a href="https://github.com/teran/archived" target="_blank">archived</a> | ||
</body> | ||
</html> |
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,12 @@ | ||
<html> | ||
<head> | ||
<title>Container Index</title> | ||
</head> | ||
<body> | ||
<h1>Container index</h1> | ||
<hr> | ||
<a href="/test-container-1/">test-container-1</a><br> | ||
<hr> | ||
Powered by <a href="https://github.com/teran/archived" target="_blank">archived</a> | ||
</body> | ||
</html> |
Oops, something went wrong.