ipfsdaemonlauncher

package module
v0.0.0-...-208f87b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 29, 2024 License: ISC Imports: 15 Imported by: 0

README

IPFS Daemon Launcher

GoDoc Go Report Card License Go version

IPFS Daemon Launcher is a Go package that manages IPFS by running the ipfs binary as a separate process alongside your Go application. This package allows your Go application to start an IPFS node, connect it to the IPFS network, and interact with the node via the HTTP Kubo RPC API. Your Go application can also gracefully shut down the IPFS node when needed.

Key Features

  1. Automatic IPFS Binary Download
    Upon the first usage in your Go application, the package automatically downloads the appropriate IPFS binary from the official IPFS distribution site, based on your machine's chipset (e.g., amd64, riscv, arm, etc.). The download process is blocking, but once the binary is downloaded, subsequent invocations are quick.

    Example:

    package main
    
    import (
        "log"
    
        "github.com/bartmika/ipfs-daemon-launcher"
    )
    
    func main() {
        // This step will download the IPFS binary if not already present.
        // Note: This is a blocking operation during the initial download.
        wrapper, initErr := ipfsdaemonlauncher.NewDaemonLauncher()
        if initErr != nil {
            log.Fatalf("Failed to create IPFS wrapper: %v", initErr)
        }
        if wrapper == nil {
            log.Fatal("Failed: wrapper cannot be nil")
        }
    
        // Continue with the rest of your application...
    }
    
  2. Starting the IPFS Daemon in Background
    Your Go application can start the IPFS daemon concurrently, allowing it to run in the background. This operation is non-blocking, so your application continues running while IPFS is managed by the package.

    Example:

    // Continued from the previous example...
    
    // Start the IPFS daemon in the background (non-blocking).
    if startErr := wrapper.StartDaemonInBackground(); startErr != nil {
        log.Fatal(startErr)
    }
    
    // Ensure IPFS daemon is gracefully shut down when your application exits.
    defer func() {
        if endErr := wrapper.ShutdownDaemon(); endErr != nil {
            log.Fatal(endErr)
        }
    }()
    
    // Continue with the rest of your application...
    
  3. Interacting with the IPFS Node
    Once the IPFS daemon is running, your Go application can interact with the node using the HTTP Kubo RPC API.

    Example:

    package main
    
    import (
        "context"
        "fmt"
        "log"
        "net/http"
        "os"
        "strings"
        "time"
    
        ipfsFiles "github.com/ipfs/go-ipfs-files"
        "github.com/ipfs/kubo/client/rpc"
    
        ipfsdaemonlauncher "github.com/bartmika/ipfs-daemon-launcher"
    )
    
    func main() {
        wrapper, initErr := ipfsdaemonlauncher.NewDaemonLauncher()
        if initErr != nil {
            log.Fatalf("failed creating ipfs-daemon-launcher: %v", initErr)
        }
        if wrapper == nil {
            log.Fatal("cannot return nil wrapper")
        }
    
        if startErr := wrapper.StartDaemonInBackground(); startErr != nil {
            log.Fatal(startErr)
        }
        defer func() {
            if endErr := wrapper.ShutdownDaemon(); endErr != nil {
                log.Fatal(endErr)
            }
        }()
    
        httpClient := &http.Client{}
        httpApi, err := rpc.NewURLApiWithClient("http://127.0.0.1:5001", httpClient)
        if err != nil {
            log.Printf("failed loading ipfs daemon: %v\n", err)
            return
        }
    
        content := strings.NewReader("Hellow world via `github.com/bartmika/ipfs-daemon-launcher/examples/rpc/main.go`!")
        p, err := httpApi.Unixfs().Add(context.Background(), ipfsFiles.NewReaderFile(content))
        if err != nil {
            log.Printf("failed adding data to ipfs: %v\n", err)
            return
        }
    
        fmt.Printf("Data successfully stored in IPFS with file CID: %v\n", p)
    }
    

Note: You can see the full code of above in rpc example code folder.

Installation

STEP 1 - Install our package

go get github.com/bartmika/ipfs-daemon-launcher

STEP 2 - Update your .gitgnore to not include binary

Inside your projects git repository, update your .gitgnore to include the following so your repo doesn't save the ipfs binary!

bin
./bin
./bin/*

STEP 3 - Open port 4001 on your machine

On your server, open port 4001 as this is called the called the Swarm port that ipfs uses to connect to the IPFS network.

Documentation

All documentation can be found here.

Usage

See examples folder.

Contributing

Found a bug? Want a feature to improve your developer experience when finding the difference? Please create an issue.

License

Made with ❤️ by Bartlomiej Mika.
The project is licensed under the ISC License.

Documentation

Overview

IPFS-DaemonLauncher is a package that manages running an IPFS node in the background and offers a user-friendly interface, enabling you to build IPFS-embedded Golang applications more efficiently.

Index

Constants

View Source
const (
	IPFSBinaryFilePath  = "./bin/kubo/ipfs"
	IPFSDataDirPath     = "./bin/kubo/data"
	IPFSDenylistDirPath = IPFSDataDirPath + "/denylists/"
)
View Source
const (
	AllPinType       = "all"
	RecursivePinType = "recursive"
	IndirectPinType  = "indirect"
	DirectPinType    = "direct"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type IpfsDaemonLauncher

type IpfsDaemonLauncher struct {
	// contains filtered or unexported fields
}

IpfsDaemonLauncher represents the a wrapper over a `ipfs` executable binary in the operating system that we use to control the operation of.

func NewDaemonLauncher

func NewDaemonLauncher(options ...Option) (*IpfsDaemonLauncher, error)

func (*IpfsDaemonLauncher) AddFile

func (wrap *IpfsDaemonLauncher) AddFile(ctx context.Context, filepath string) (string, error)

func (*IpfsDaemonLauncher) AddFileContent

func (wrap *IpfsDaemonLauncher) AddFileContent(ctx context.Context, filename string, fileContent []byte) (string, error)

func (*IpfsDaemonLauncher) Cat

func (wrap *IpfsDaemonLauncher) Cat(ctx context.Context, cid string) ([]byte, error)

func (*IpfsDaemonLauncher) ForceShutdownDaemon

func (wrap *IpfsDaemonLauncher) ForceShutdownDaemon() error

ForceShutdownDaemon function will send KILL signal to the operating system for the `ipfs` running daemon in background to force that binary to shutdown.

func (*IpfsDaemonLauncher) GarbageCollection

func (wrap *IpfsDaemonLauncher) GarbageCollection(ctx context.Context) error

func (*IpfsDaemonLauncher) GetFile

func (wrap *IpfsDaemonLauncher) GetFile(ctx context.Context, cid string) error

func (*IpfsDaemonLauncher) ListPins

func (wrap *IpfsDaemonLauncher) ListPins(ctx context.Context) ([]string, error)

func (*IpfsDaemonLauncher) ListPinsByType

func (wrap *IpfsDaemonLauncher) ListPinsByType(ctx context.Context, typeID string) ([]string, error)

func (*IpfsDaemonLauncher) Pin

func (wrap *IpfsDaemonLauncher) Pin(ctx context.Context, cid string) error

func (*IpfsDaemonLauncher) ShutdownDaemon

func (wrap *IpfsDaemonLauncher) ShutdownDaemon() error

func (*IpfsDaemonLauncher) StartDaemonInBackground

func (wrap *IpfsDaemonLauncher) StartDaemonInBackground() error

func (*IpfsDaemonLauncher) Unpin

func (wrap *IpfsDaemonLauncher) Unpin(ctx context.Context, cid string) error

type Option

type Option func(*IpfsDaemonLauncher)

Option is a functional option type that allows us to configure the IpfsDaemonLauncher.

func WithContinousOperation

func WithContinousOperation() Option

WithContinousOperation is a functional option to configure our wrapper to not terminate the operation of the `ipfs` binary when running in the background; in addition when we run the `Start()` function, no errors will occure pertaining to previously active running `ipfs` binary instance. This is a useful option if you developing an app in which you restart often and you don't want to restart the `ipfs` binary often then use this option.

func WithDenylist

func WithDenylist(denylistFilename string, denylistURL string) Option

WithDenylist is a functional option which downloads a `denylist` [0] from the URL you provided and applies it to the `ipfs` binary running instance. [0] https://github.com/ipfs/kubo/blob/master/docs/content-blocking.md

func WithForcedShutdownDaemonOnStartup

func WithForcedShutdownDaemonOnStartup() Option

WithForcedShutdownDaemonOnStartup is a functional option to add if you want this package to look for any previously running `ipfs` binary in the system background and shut it down before our package loads up a new `ipfs` binary instance.

func WithOverrideBinaryOsAndArch

func WithOverrideBinaryOsAndArch(overrideOS, overrideArch string) Option

WithOverrideBinaryOsAndArch is a functional option to configure our wrapper to use a specific binary. The available `os` options are: darwin, linux, freebsd, openbsd and windows. The available `arch` choices are: arm, arm64, 386, and amd64.

func WithOverrideDaemonInitialWarmupDuration

func WithOverrideDaemonInitialWarmupDuration(seconds int) Option

WithOverrideDaemonInitialWarmupDuration is a functional option to configure our wrapper to set a custom warmup delay for our app to give a custom delay to allow the `ipfs` to loadup before giving your app execution control.

func WithRunGarbageCollectionOnStarup

func WithRunGarbageCollectionOnStarup() Option

Directories

Path Synopsis
internal
oskit
Package oskit provides number of useful convinence functions related to OS operations like `mkdir`, `mv`, etc.
Package oskit provides number of useful convinence functions related to OS operations like `mkdir`, `mv`, etc.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL