42 lines
910 B
Go
Raw Normal View History

2024-08-18 05:54:08 +07:00
package router
import (
2025-09-27 19:26:10 -03:00
"bytes"
2024-08-18 05:54:08 +07:00
"fmt"
2025-09-27 19:26:10 -03:00
"io"
2024-08-18 05:54:08 +07:00
"khairul169/garage-webui/utils"
2025-09-27 19:26:10 -03:00
"log"
2024-08-18 05:54:08 +07:00
"net/http"
"net/http/httputil"
"net/url"
"strings"
)
func ProxyHandler(w http.ResponseWriter, r *http.Request) {
2025-09-27 19:26:10 -03:00
// Log CreateBucket requests
if strings.Contains(r.URL.Path, "CreateBucket") && r.Method == "POST" {
body, err := io.ReadAll(r.Body)
if err == nil {
log.Printf("CreateBucket request body: %s", string(body))
// Restore body for proxy
r.Body = io.NopCloser(bytes.NewReader(body))
}
}
2024-08-18 05:54:08 +07:00
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)
}