mirror of
				https://github.com/khairul169/garage-webui.git
				synced 2025-10-30 22:59:31 +07:00 
			
		
		
		
	
		
			
				
	
	
		
			55 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			55 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package router
 | |
| 
 | |
| import (
 | |
| 	"encoding/json"
 | |
| 	"fmt"
 | |
| 	"khairul169/garage-webui/schema"
 | |
| 	"khairul169/garage-webui/utils"
 | |
| 	"net/http"
 | |
| )
 | |
| 
 | |
| type Buckets struct{}
 | |
| 
 | |
| func (b *Buckets) GetAll(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 data schema.Bucket
 | |
| 			if err := json.Unmarshal(body, &data); err != nil {
 | |
| 				ch <- schema.Bucket{ID: bucket.ID, GlobalAliases: bucket.GlobalAliases}
 | |
| 				return
 | |
| 			}
 | |
| 
 | |
| 			data.LocalAliases = bucket.LocalAliases
 | |
| 			ch <- data
 | |
| 		}()
 | |
| 	}
 | |
| 
 | |
| 	res := make([]schema.Bucket, 0, len(buckets))
 | |
| 	for i := 0; i < len(buckets); i++ {
 | |
| 		res = append(res, <-ch)
 | |
| 	}
 | |
| 
 | |
| 	utils.ResponseSuccess(w, res)
 | |
| }
 |