feat: rewrite backend to go

This commit is contained in:
2024-08-18 05:54:08 +07:00
parent e1d2274bfb
commit b554cb4dbf
30 changed files with 448 additions and 592 deletions
+51
View File
@@ -0,0 +1,51 @@
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)
}
+11
View File
@@ -0,0 +1,11 @@
package router
import (
"khairul169/garage-webui/utils"
"net/http"
)
func GetConfig(w http.ResponseWriter, r *http.Request) {
config := utils.Garage.Config
utils.ResponseSuccess(w, config)
}
+28
View File
@@ -0,0 +1,28 @@
package router
import (
"fmt"
"khairul169/garage-webui/utils"
"net/http"
"net/http/httputil"
"net/url"
"strings"
)
func ProxyHandler(w http.ResponseWriter, r *http.Request) {
target, err := url.Parse(utils.Garage.GetAdminEndpoint())
if err != nil {
utils.ResponseError(w, err)
return
}
proxy := &httputil.ReverseProxy{
Rewrite: func(r *httputil.ProxyRequest) {
r.SetURL(target)
r.Out.URL.Path = strings.TrimPrefix(r.In.URL.Path, "/api")
r.Out.Header.Set("Authorization", fmt.Sprintf("Bearer %s", utils.Garage.GetAdminKey()))
},
}
proxy.ServeHTTP(w, r)
}