From 20f3ba6879341bc542b7c33454db7ab547aec20e Mon Sep 17 00:00:00 2001 From: Nikita Zinkevich Date: Mon, 30 Sep 2024 12:16:04 +0300 Subject: [PATCH] [#137] Add index page support Signed-off-by: Nikita Zinkevich --- cmd/http-gw/app.go | 15 +-------------- internal/handler/browse.go | 10 +++++++--- internal/handler/handler.go | 2 +- 3 files changed, 9 insertions(+), 18 deletions(-) diff --git a/cmd/http-gw/app.go b/cmd/http-gw/app.go index 3aea305..0358de6 100644 --- a/cmd/http-gw/app.go +++ b/cmd/http-gw/app.go @@ -187,12 +187,6 @@ func (s *appSettings) IndexPageEnabled() bool { return s.returnIndexPage } -func (s *appSettings) IndexPageTemplatePath() string { - s.mu.RLock() - defer s.mu.RUnlock() - return s.indexPageTemplatePath -} - func (s *appSettings) IndexPageTemplate() string { s.mu.RLock() defer s.mu.RUnlock() @@ -211,12 +205,6 @@ func (s *appSettings) setReturnIndexPage(val bool) { s.mu.Unlock() } -func (s *appSettings) setIndexTemplatePath(val string) { - s.mu.Lock() - s.indexPageTemplatePath = val - s.mu.Unlock() -} - func (s *appSettings) setIndexTemplate(val string) { s.mu.Lock() s.indexPageTemplate = val @@ -227,7 +215,7 @@ func (a *app) loadIndexPageTemplate() { if !a.settings.IndexPageEnabled() { return } - reader, err := os.Open(a.settings.IndexPageTemplatePath()) + reader, err := os.Open(a.cfg.GetString(cfgIndexPageTemplatePath)) if err != nil { a.settings.setIndexTemplate("") a.log.Warn(logs.FailedToReadIndexPageTemplate, zap.Error(err)) @@ -560,7 +548,6 @@ func (a *app) updateSettings() { a.settings.setDefaultTimestamp(a.cfg.GetBool(cfgUploaderHeaderEnableDefaultTimestamp)) a.settings.setZipCompression(a.cfg.GetBool(cfgZipCompression)) a.settings.setReturnIndexPage(a.cfg.GetBool(cfgIndexPageEnabled)) - a.settings.setIndexTemplatePath(a.cfg.GetString(cfgIndexPageTemplatePath)) a.settings.setClientCut(a.cfg.GetBool(cfgClientCut)) a.settings.setBufferMaxSizeForPut(a.cfg.GetUint64(cfgBufferMaxSizeForPut)) a.settings.setNamespaceHeader(a.cfg.GetString(cfgResolveNamespaceHeader)) diff --git a/internal/handler/browse.go b/internal/handler/browse.go index 28e4f29..67611a2 100644 --- a/internal/handler/browse.go +++ b/internal/handler/browse.go @@ -18,8 +18,11 @@ import ( ) const ( - dateFormat = "02-01-2006 15:04" - attrOID, attrCreated, attrFileName, attrSize = "OID", "Created", "FileName", "Size" + dateFormat = "02-01-2006 15:04" + attrOID = "OID" + attrCreated = "Created" + attrFileName = "FileName" + attrSize = "Size" ) type ( @@ -110,7 +113,7 @@ func urlencode(prefix, filename, size, created string) string { } func (h *Handler) browseObjects(c *fasthttp.RequestCtx, bucketInfo *data.BucketInfo, prefix string) { - var log = h.log.With(zap.String("bucket", bucketInfo.Name)) + log := h.log.With(zap.String("bucket", bucketInfo.Name)) ctx := utils.GetContextFromRequest(c) nodes, err := h.listObjects(ctx, bucketInfo, prefix) if err != nil { @@ -119,6 +122,7 @@ func (h *Handler) browseObjects(c *fasthttp.RequestCtx, bucketInfo *data.BucketI } respObjects := make([]ResponseObject, len(nodes)) + for i, node := range nodes { respObjects[i] = NewResponseObject(node) } diff --git a/internal/handler/handler.go b/internal/handler/handler.go index d04f753..0c2d486 100644 --- a/internal/handler/handler.go +++ b/internal/handler/handler.go @@ -31,7 +31,6 @@ type Config interface { ZipCompression() bool ClientCut() bool IndexPageEnabled() bool - IndexPageTemplatePath() string IndexPageTemplate() string BufferMaxSizeForPut() uint64 NamespaceHeader() string @@ -235,6 +234,7 @@ func (h *Handler) byObjectName(c *fasthttp.RequestCtx, f func(context.Context, r if h.config.IndexPageEnabled() && !download && string(c.Method()) != fasthttp.MethodHead { if isDir(unescapedKey) || isContainerRoot(unescapedKey) { + c.SetStatusCode(fasthttp.StatusNotFound) h.browseObjects(c, bktInfo, unescapedKey) return }