s3manager-web/handlers.go

70 lines
1.4 KiB
Go
Raw Normal View History

2016-12-18 22:54:21 +01:00
package main
import (
"fmt"
"html/template"
"net/http"
"path"
"strings"
minio "github.com/minio/minio-go"
)
2016-12-20 19:35:27 +01:00
// indexHandler forwards to "/buckets"
2016-12-18 22:54:21 +01:00
func indexHandler(w http.ResponseWriter, r *http.Request) {
2016-12-20 19:35:27 +01:00
http.Redirect(w, r, "/buckets", http.StatusPermanentRedirect)
}
// bucketsHandler handles the main page
func bucketsHandler(w http.ResponseWriter, r *http.Request) {
2016-12-18 22:54:21 +01:00
lp := path.Join("templates", "layout.html")
ip := path.Join("templates", "index.html")
t, err := template.ParseFiles(lp, ip)
if err != nil {
panic(err)
}
buckets, err := minioClient.ListBuckets()
if err != nil {
panic(err)
}
err = t.ExecuteTemplate(w, "layout", buckets)
if err != nil {
panic(err)
}
}
// bucketHandler handles the main page
func bucketHandler(w http.ResponseWriter, r *http.Request) {
bucket := strings.Split(r.URL.Path, "/")[2]
var objects []minio.ObjectInfo
lp := path.Join("templates", "layout.html")
bp := path.Join("templates", "bucket.html")
t, err := template.ParseFiles(lp, bp)
if err != nil {
panic(err)
}
// Create a done channel to control 'ListObjectsV2' go routine.
doneCh := make(chan struct{})
objectCh := minioClient.ListObjectsV2(bucket, "", false, doneCh)
for object := range objectCh {
if object.Err != nil {
fmt.Println(object.Err)
return
}
objects = append(objects, object)
}
err = t.ExecuteTemplate(w, "layout", objects)
if err != nil {
panic(err)
}
}