Skip to content

Commit

Permalink
feat: add serve gateway
Browse files Browse the repository at this point in the history
Signed-off-by: gfanton <8671905+gfanton@users.noreply.github.com>
  • Loading branch information
gfanton authored and n0izn0iz committed Jan 29, 2022
1 parent 664195f commit a0cfba9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
24 changes: 24 additions & 0 deletions go/bind/core/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,30 @@ func (n *Node) ServeConfigAPI() error {
return nil
}

func (n *Node) ServeGateway(smaddr string, writable bool) (string, error) {
maddr, err := ma.NewMultiaddr(smaddr)
if err != nil {
return "", err
}

ml, err := manet.Listen(maddr)
if err != nil {
return "", err
}

n.muListeners.Lock()
n.listeners = append(n.listeners, ml)
n.muListeners.Unlock()

go func(l net.Listener) {
if err := n.ipfsMobile.ServeGateway(l, writable); err != nil {
log.Printf("serve error: %s", err.Error())
}
}(manet.NetListener(ml))

return "", nil
}

func (n *Node) ServeMultiaddr(smaddr string) (string, error) {
maddr, err := ma.NewMultiaddr(smaddr)
if err != nil {
Expand Down
12 changes: 12 additions & 0 deletions go/pkg/ipfsmobile/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,18 @@ func (im *IpfsMobile) ServeCoreHTTP(l net.Listener, opts ...ipfs_corehttp.ServeO
return ipfs_corehttp.Serve(im.IpfsNode, l, opts...)
}

func (im *IpfsMobile) ServeGateway(l net.Listener, writable bool, opts ...ipfs_corehttp.ServeOption) error {
opts = append(opts,
ipfs_corehttp.HostnameOption(),
ipfs_corehttp.GatewayOption(writable, "/ipfs", "/ipns"),
ipfs_corehttp.VersionOption(),
ipfs_corehttp.CheckVersionOption(),
ipfs_corehttp.CommandsROOption(im.commandCtx),
)

return ipfs_corehttp.Serve(im.IpfsNode, l, opts...)
}

func NewNode(ctx context.Context, cfg *IpfsConfig) (*IpfsMobile, error) {
if err := cfg.fillDefault(); err != nil {
return nil, fmt.Errorf("invalid configuration: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion packages/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ GO_MOD_FILES = $(GO_DIR)/go.mod $(GO_DIR)/go.sum

CORE_PACKAGE = github.com/ipfs-shipyard/gomobile-ipfs/go/bind/core
EXT_PACKAGE ?=
GOMOBILE = go run golang.org/x/mobile/cmd/gomobile
GOMOBILE ?= go run golang.org/x/mobile/cmd/gomobile
GOMOBILE_OPT ?=
GOMOBILE_TARGET ?=

Expand Down

0 comments on commit a0cfba9

Please sign in to comment.