s3manager-web/internal/app/s3manager/buckets_view.go

33 lines
800 B
Go
Raw Normal View History

2017-05-08 23:07:07 +02:00
package s3manager
2017-04-03 14:08:01 +02:00
import (
2019-09-05 00:44:02 +02:00
"fmt"
2017-04-03 14:08:01 +02:00
"html/template"
"net/http"
2018-03-14 21:53:35 +01:00
"path/filepath"
2017-04-03 14:08:01 +02:00
)
2018-05-31 16:10:41 +02:00
// HandleBucketsView renders all buckets on an HTML page.
func HandleBucketsView(s3 S3, tmplDir string) http.HandlerFunc {
2019-01-26 13:53:36 +01:00
return func(w http.ResponseWriter, _ *http.Request) {
2018-03-14 21:53:35 +01:00
buckets, err := s3.ListBuckets()
2017-04-03 14:08:01 +02:00
if err != nil {
2019-09-05 00:44:02 +02:00
handleHTTPError(w, fmt.Errorf("error listing buckets: %w", err))
2017-04-03 14:08:01 +02:00
return
}
2018-03-14 21:53:35 +01:00
l := filepath.Join(tmplDir, "layout.html.tmpl")
p := filepath.Join(tmplDir, "buckets.html.tmpl")
t, err := template.ParseFiles(l, p)
2017-04-03 14:08:01 +02:00
if err != nil {
2019-09-05 00:44:02 +02:00
handleHTTPError(w, fmt.Errorf("error parsing template files: %w", err))
2017-04-03 14:08:01 +02:00
return
}
err = t.ExecuteTemplate(w, "layout", buckets)
if err != nil {
2019-09-05 00:44:02 +02:00
handleHTTPError(w, fmt.Errorf("error executing template: %w", err))
2017-04-03 14:08:01 +02:00
return
}
2018-05-31 16:10:41 +02:00
}
2017-04-03 14:08:01 +02:00
}