mirror of
https://github.com/khairul169/garage-webui.git
synced 2025-04-27 22:39:31 +07:00
52 lines
1.1 KiB
Go
52 lines
1.1 KiB
Go
package router
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"khairul169/garage-webui/schema"
|
|
"khairul169/garage-webui/utils"
|
|
"net/http"
|
|
)
|
|
|
|
func GetAllBuckets(w http.ResponseWriter, r *http.Request) {
|
|
body, err := utils.Garage.Fetch("/v1/bucket?list", &utils.FetchOptions{})
|
|
if err != nil {
|
|
utils.ResponseError(w, err)
|
|
return
|
|
}
|
|
|
|
var buckets []schema.GetBucketsRes
|
|
if err := json.Unmarshal(body, &buckets); err != nil {
|
|
utils.ResponseError(w, err)
|
|
return
|
|
}
|
|
|
|
ch := make(chan schema.Bucket, len(buckets))
|
|
|
|
for _, bucket := range buckets {
|
|
go func() {
|
|
body, err := utils.Garage.Fetch(fmt.Sprintf("/v1/bucket?id=%s", bucket.ID), &utils.FetchOptions{})
|
|
|
|
if err != nil {
|
|
ch <- schema.Bucket{ID: bucket.ID, GlobalAliases: bucket.GlobalAliases}
|
|
return
|
|
}
|
|
|
|
var bucket schema.Bucket
|
|
if err := json.Unmarshal(body, &bucket); err != nil {
|
|
ch <- schema.Bucket{ID: bucket.ID, GlobalAliases: bucket.GlobalAliases}
|
|
return
|
|
}
|
|
|
|
ch <- bucket
|
|
}()
|
|
}
|
|
|
|
res := make([]schema.Bucket, 0, len(buckets))
|
|
for i := 0; i < len(buckets); i++ {
|
|
res = append(res, <-ch)
|
|
}
|
|
|
|
utils.ResponseSuccess(w, res)
|
|
}
|