s3manager-web/handlers.go
2016-12-18 22:54:21 +01:00

64 lines
1.2 KiB
Go

package main
import (
"fmt"
"html/template"
"net/http"
"path"
"strings"
minio "github.com/minio/minio-go"
)
// indexHandler handles the main page
func indexHandler(w http.ResponseWriter, r *http.Request) {
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)
}
}