Skip to content

Commit

Permalink
Expose CouchDB (resolves #71) (#72)
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Stone <sstone1@uk.ibm.com>
  • Loading branch information
Simon Stone authored Jan 22, 2021
1 parent 2b6f41a commit babbb05
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 11 deletions.
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,9 @@
# vendor/

# Directories created by us at runtime
/data
/data

/builders/java/.classpath
/builders/java/.project
/builders/java/.settings
/builders/java/target
5 changes: 3 additions & 2 deletions internal/app/microfabd/microfabd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
23 changes: 18 additions & 5 deletions internal/pkg/couchdb/couchdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions internal/pkg/couchdb/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "/_") {
Expand Down
11 changes: 10 additions & 1 deletion internal/pkg/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit babbb05

Please sign in to comment.