// Copyright (c) 2025 Nikolai Papin // // This file is part of Easywish // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See // the GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . package minioclient import ( "easywish/config" "fmt" "net/http" "net/http/httputil" "net/url" "strings" "time" "github.com/gin-gonic/gin" ) func setupGinEndpoint(router *gin.Engine) { cfg := config.GetConfig() minioHost := fmt.Sprintf("%s:%d", cfg.MinioHost, cfg.MinioPort) proxy := &httputil.ReverseProxy{ Director: func(req *http.Request) { path := strings.TrimPrefix(req.URL.Path, "/s3") targetURL := &url.URL{ Scheme: "http", Host: minioHost, Path: path, RawQuery: req.URL.RawQuery, } req.URL = targetURL req.Host = minioHost req.Header.Set("Host", minioHost) req.Header.Del("X-Forwarded-For") req.Header.Del("X-Forwarded-Host") req.Header.Del("X-Forwarded-Proto") }, Transport: &http.Transport{ ResponseHeaderTimeout: time.Duration(cfg.MinioTimeout), }, ErrorHandler: func(w http.ResponseWriter, r *http.Request, err error) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusInternalServerError) w.Write([]byte(`{"error": "Failed to forward request"}`)) fmt.Printf("Proxy error: %v\n", err) }, } s3group := router.Group("/s3") s3group.Any("/*path", func(c *gin.Context) { proxy.ServeHTTP(c.Writer, c.Request) }) }