From babbb054c1170e3d778d94870a05b720b7d3d5ec Mon Sep 17 00:00:00 2001 From: Simon Stone Date: Fri, 22 Jan 2021 10:55:33 +0000 Subject: [PATCH] Expose CouchDB (resolves #71) (#72) Signed-off-by: Simon Stone --- .gitignore | 7 ++++++- internal/app/microfabd/microfabd.go | 5 +++-- internal/pkg/couchdb/couchdb.go | 23 ++++++++++++++++++----- internal/pkg/couchdb/proxy.go | 4 ++-- internal/pkg/proxy/proxy.go | 11 ++++++++++- 5 files changed, 39 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index afa88f0..49cf93d 100644 --- a/.gitignore +++ b/.gitignore @@ -19,4 +19,9 @@ # vendor/ # Directories created by us at runtime -/data \ No newline at end of file +/data + +/builders/java/.classpath +/builders/java/.project +/builders/java/.settings +/builders/java/target diff --git a/internal/app/microfabd/microfabd.go b/internal/app/microfabd/microfabd.go index e8f7ca0..f0bb1f1 100644 --- a/internal/app/microfabd/microfabd.go +++ b/internal/app/microfabd/microfabd.go @@ -205,7 +205,7 @@ func (m *Microfab) Start() error { go console.Start() // Create and start the proxy. - proxy, err := proxy.New(console, m.orderer, m.peers, m.cas, m.config.Port) + proxy, err := proxy.New(console, m.orderer, m.peers, m.cas, m.couchDB, m.config.Port) if err != nil { return err } @@ -456,7 +456,8 @@ func (m *Microfab) createAndStartOrderer(organization *organization.Organization func (m *Microfab) waitForCouchDB() error { logger.Printf("Waiting for CouchDB to start ...") - couchDB, err := couchdb.New("http://localhost:5984") + couchURL := fmt.Sprintf("http://couchdb.%s:%d", m.config.Domain, m.config.Port) + couchDB, err := couchdb.New("http://localhost:5984", couchURL) if err != nil { return err } diff --git a/internal/pkg/couchdb/couchdb.go b/internal/pkg/couchdb/couchdb.go index 576ddf4..b4ecb81 100644 --- a/internal/pkg/couchdb/couchdb.go +++ b/internal/pkg/couchdb/couchdb.go @@ -15,16 +15,21 @@ import ( // CouchDB represents a CouchDB instance. type CouchDB struct { - url *nurl.URL + internalURL *nurl.URL + externalURL *nurl.URL } // New creates a new CouchDB instance. -func New(url string) (*CouchDB, error) { - parsedURL, err := nurl.Parse(url) +func New(internalURL, externalURL string) (*CouchDB, error) { + parsedInternalURL, err := nurl.Parse(internalURL) if err != nil { return nil, err } - return &CouchDB{url: parsedURL}, nil + parsedExternalURL, err := nurl.Parse(externalURL) + if err != nil { + return nil, err + } + return &CouchDB{internalURL: parsedInternalURL, externalURL: parsedExternalURL}, nil } // WaitFor waits for the CouchDB instance to start. @@ -43,8 +48,16 @@ func (c *CouchDB) WaitFor(timeout time.Duration) error { } } +// URL returns the URL of the CouchDB. +func (c *CouchDB) URL(internal bool) *url.URL { + if internal { + return c.internalURL + } + return c.externalURL +} + func (c *CouchDB) hasStarted() bool { - upURL := c.url.ResolveReference(&url.URL{Path: "/_up"}).String() + upURL := c.internalURL.ResolveReference(&url.URL{Path: "/_up"}).String() resp, err := http.Get(upURL) if err != nil { return false diff --git a/internal/pkg/couchdb/proxy.go b/internal/pkg/couchdb/proxy.go index 125abf3..cc8bed8 100644 --- a/internal/pkg/couchdb/proxy.go +++ b/internal/pkg/couchdb/proxy.go @@ -26,8 +26,8 @@ type Proxy struct { func (c *CouchDB) NewProxy(prefix string, port int) (*Proxy, error) { result := &Proxy{transport: http.DefaultTransport, prefix: prefix} director := func(req *http.Request) { - req.URL.Scheme = c.url.Scheme - req.URL.Host = c.url.Host + req.URL.Scheme = c.internalURL.Scheme + req.URL.Host = c.internalURL.Host if req.URL.Path == "/_all_dbs" { // Do nothing. } else if req.URL.Path == "/" || strings.HasPrefix(req.URL.Path, "/_") { diff --git a/internal/pkg/proxy/proxy.go b/internal/pkg/proxy/proxy.go index afd9a69..813aa45 100644 --- a/internal/pkg/proxy/proxy.go +++ b/internal/pkg/proxy/proxy.go @@ -14,6 +14,7 @@ import ( "github.com/IBM-Blockchain/microfab/internal/pkg/ca" "github.com/IBM-Blockchain/microfab/internal/pkg/console" + "github.com/IBM-Blockchain/microfab/internal/pkg/couchdb" "github.com/IBM-Blockchain/microfab/internal/pkg/orderer" "github.com/IBM-Blockchain/microfab/internal/pkg/peer" "golang.org/x/net/http2" @@ -45,7 +46,7 @@ func (tw *h2cTransportWrapper) RoundTrip(req *http.Request) (*http.Response, err var portRegex = regexp.MustCompile(":\\d+$") // New creates a new instance of a proxy. -func New(console *console.Console, orderer *orderer.Orderer, peers []*peer.Peer, cas []*ca.CA, port int) (*Proxy, error) { +func New(console *console.Console, orderer *orderer.Orderer, peers []*peer.Peer, cas []*ca.CA, couchDB *couchdb.CouchDB, port int) (*Proxy, error) { routes := []*route{ { SourceHost: console.URL().Host, @@ -98,6 +99,14 @@ func New(console *console.Console, orderer *orderer.Orderer, peers []*peer.Peer, } routes = append(routes, orgRoutes...) } + if couchDB != nil { + couchRoute := &route{ + SourceHost: couchDB.URL(false).Host, + TargetHost: couchDB.URL(true).Host, + UseHTTP2: false, + } + routes = append(routes, couchRoute) + } rm := routeMap{} for _, route := range routes { rm[route.SourceHost] = route