gengitkan/internal/route/proxy.go

45 lines
1.1 KiB
Go

package route
import (
"fmt"
"net/http"
"net/http/httputil"
"net/url"
"github.com/davecgh/go-spew/spew"
"forge.cadoles.com/wpetit/gitea-kan/internal/config"
"forge.cadoles.com/wpetit/gitea-kan/internal/middleware"
"github.com/pkg/errors"
"gitlab.com/wpetit/goweb/middleware/container"
"gitlab.com/wpetit/goweb/service/session"
)
func proxyAPIRequest(w http.ResponseWriter, r *http.Request) {
ctn := container.Must(r.Context())
conf := config.Must(ctn)
apiBaseURL, err := url.Parse(conf.Gitea.APIBaseURL)
if err != nil {
panic(errors.Wrap(err, "could not parse api base url"))
}
sess, err := session.Must(ctn).Get(w, r)
if err != nil {
panic(errors.Wrap(err, "could not retrieve session"))
}
accessToken := sess.Get(middleware.SessionOAuth2AccessToken)
proxy := httputil.NewSingleHostReverseProxy(apiBaseURL)
proxy.Director = func(r *http.Request) {
r.Host = apiBaseURL.Host
r.URL.Scheme = apiBaseURL.Scheme
r.URL.Host = apiBaseURL.Host
r.Header.Add("Authorization", fmt.Sprintf("token %s", accessToken))
spew.Dump(r)
}
proxy.ServeHTTP(w, r)
}