package route import ( "fmt" "net/http" "net/http/httputil" "net/url" "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(rr *http.Request) { rr.Host = apiBaseURL.Host rr.URL.Scheme = apiBaseURL.Scheme rr.URL.Host = apiBaseURL.Host rr.Method = r.Method rr.Header.Add("Accept", "application/json") rr.Header.Add("Authorization", fmt.Sprintf("token %s", accessToken)) } proxy.ServeHTTP(w, r) }