working couchdb reverse proxy behind webapp binary.

This commit is contained in:
Jason Kulatunga 2022-10-09 09:34:57 -07:00
parent 0a36a179d5
commit a7daae9047
3 changed files with 42 additions and 1 deletions

View File

@ -0,0 +1,38 @@
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("web.couchdb.scheme"), appConfig.GetString("web.couchdb.host"), appConfig.GetString("web.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")
}
proxy.ServeHTTP(c.Writer, c.Request)
}

View File

@ -42,6 +42,8 @@ func (ae *AppEngine) Setup(logger *logrus.Entry) *gin.Engine {
api.POST("/auth/signup", handler.AuthSignup)
api.GET("/metadata/source", handler.GetMetadataSource)
r.Any("/database/*proxyPath", handler.CouchDBProxy)
}
}

View File

@ -27,10 +27,11 @@ export class FastenDbService extends PouchdbRepository {
//TODO: move most of this functionality back into the lib as a separate file.
replicationHandler: any
remotePouchEndpoint = "http://localhost:5984"
remotePouchEndpoint: string // "http://localhost:5984"
constructor(private _httpClient: HttpClient) {
const userIdentifier = localStorage.getItem("current_user")
super(userIdentifier, "my-secret-encryption-key");
this.remotePouchEndpoint = `${window.location.protocol}//${window.location.host}${this.getBasePath()}/database`
if(userIdentifier){
this.enableSync(userIdentifier)
}