fasten-onprem/backend/pkg/web/handler/couchdb_proxy.go

41 lines
1.1 KiB
Go

package handler
import (
"fmt"
"github.com/fastenhealth/fastenhealth-onprem/backend/pkg/config"
"github.com/gin-gonic/gin"
"log"
"net/http"
"net/http/httputil"
"net/url"
"strings"
)
func CouchDBProxy(c *gin.Context) {
appConfig := c.MustGet("CONFIG").(config.Interface)
couchdbUrl := fmt.Sprintf("%s://%s:%s", appConfig.GetString("couchdb.scheme"), appConfig.GetString("couchdb.host"), appConfig.GetString("couchdb.port"))
remote, err := url.Parse(couchdbUrl)
if err != nil {
panic(err)
}
proxy := httputil.NewSingleHostReverseProxy(remote)
//Define the director func
//This is a good place to log, for example
proxy.Director = func(req *http.Request) {
req.Header = c.Request.Header
req.Header.Add("X-Forwarded-Host", req.Host)
req.Header.Add("X-Origin-Host", remote.Host)
req.Host = remote.Host
req.URL.Scheme = remote.Scheme
req.URL.Host = remote.Host
log.Printf(c.Param("proxyPath"))
req.URL.Path = strings.TrimPrefix(c.Param("proxyPath"), "/database")
//todo: throw an error if not a user DB.
}
proxy.ServeHTTP(c.Writer, c.Request)
}