Skip to content

Commit

Permalink
Embed IPFS node
Browse files Browse the repository at this point in the history
  • Loading branch information
maurycy authored Aug 27, 2020
1 parent 45c546a commit c1849b7
Show file tree
Hide file tree
Showing 12 changed files with 496 additions and 12 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ build:
go build \
-o bin/space \
-ldflags \
"-X 'main.ipfsaddr=${IPFS_ADDR}'\
"-X 'main.ipfsnodeaddr=${IPFS_NODE_ADDR}' \
-X 'main.ipfsnodepath=${IPFS_NODE_PATH}' \
-X 'main.mongousr=${MONGO_USR}' \
-X 'main.mongopw=${MONGO_PW}' \
-X 'main.spaceapi=${SERVICES_API_URL}' \
Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ Space Daemon requires a few modules to run successfully. If you downloaded the b

### IPFS Node

All encrypted files are stored in an IPFS node. For convenience, Space Daemon connects to a hosted IPFS node by default. You can connect to one of your choosing by providing the `-ipfsaddr` flag (e.g. `-ipfsaddr=/ip4/127.0.0.1/tcp/5001`)
All encrypted files are stored in an IPFS node. For convenience, Space Daemon runs an embedded node within the daemon that can be configured as well as the option to specify an external node to connect to.

If you have your own node outside of the daemon, then set the flag `-ipfsnode` to `false`. This will not spin up an embedded node. You can then connect to your external node by providing the `-ipfsaddr` flag (e.g. `-ipfsaddr=/ip4/127.0.0.1/tcp/5001`).

In the case you are running the embedded IPFS node, you can further configure the listen address and data directory by setting these flags respectively: `-ipfsnodeaddr` and `-ipfsnodepath`.

### Textile Hub

Expand Down Expand Up @@ -78,7 +82,7 @@ Currently, local Textile Threads require a running MongoDB database. Space Daemo

## Running from source

After cloning this repo, you can run it from source by running `go run ./cmd/space-daemon -dev=true`. Consider that you will need the following environment variables exported in your system:
After cloning this repo, you can run it from source by running `go run ./cmd/space-daemon -dev`. Consider that you will need the following environment variables exported in your system:

```
IPFS_ADDR=[Your IPFS node address]
Expand Down
12 changes: 12 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/FleekHQ/space-daemon/core/env"
"github.com/FleekHQ/space-daemon/core/space"

node "github.com/FleekHQ/space-daemon/core/ipfs/node"
"github.com/FleekHQ/space-daemon/core/keychain"
"github.com/FleekHQ/space-daemon/core/sync"
"github.com/FleekHQ/space-daemon/log"
Expand Down Expand Up @@ -91,6 +92,17 @@ func (a *App) Start(ctx context.Context) error {
}
a.Run("FolderWatcher", watcher)

// setup local ipfs node if Ipfsnode is set
if a.cfg.GetBool(config.Ipfsnode, true) {
// setup local ipfs node
node := node.NewIpsNode(a.cfg)
a.RunAsync("IpfsNode", node, func() error {
return node.Start(ctx)
})
} else {
log.Info("Skipping embedded IPFS node")
}

// setup local buckets
buckd := textile.NewBuckd(a.cfg)
a.RunAsync("BucketDaemon", buckd, func() error {
Expand Down
10 changes: 8 additions & 2 deletions cmd/space-daemon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ var (
memprofile = flag.String("memprofile", "", "write memory profile to `file`")
debugMode = flag.Bool("debug", true, "run daemon with debug mode for profiling")
devMode = flag.Bool("dev", false, "run daemon in dev mode to use .env file")
ipfsaddr string
ipfsaddr = flag.String("ipfsaddr", "/ip4/127.0.0.1/tcp/5001", "IPFS multiaddress to connect to (defaults to local node)")
ipfsnode = flag.Bool("ipfsnode", true, "run IPFS embedded into the daemon (defaults to true)")
ipfsnodeaddr string
ipfsnodepath string
mongousr string
mongopw string
mongohost string
Expand All @@ -49,7 +52,10 @@ func main() {
log.Debug("Running mode", fmt.Sprintf("DevMode:%v", *devMode))

cf := &config.Flags{
Ipfsaddr: ipfsaddr,
Ipfsaddr: *ipfsaddr,
Ipfsnode: *ipfsnode == true,
Ipfsnodeaddr: ipfsnodeaddr,
Ipfsnodepath: ipfsnodepath,
Mongousr: mongousr,
Mongopw: mongopw,
Mongohost: mongohost,
Expand Down
7 changes: 7 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ const (
SpaceVaultSaltSecret = "space/vaultSaltSecret"
SpaceServicesHubAuthURL = "space/servicesHubAuthUrl"
Ipfsaddr = "space/ipfsAddr"
Ipfsnode = "space/ipfsNode"
Ipfsnodeaddr = "space/ipfsNodeAddr"
Ipfsnodepath = "space/ipfsNodePath"
Mongousr = "space/mongoUsr"
Mongopw = "space/mongoPw"
Mongohost = "space/mongoHost"
Expand All @@ -37,6 +40,9 @@ var (

type Flags struct {
Ipfsaddr string
Ipfsnode bool
Ipfsnodeaddr string
Ipfsnodepath string
Mongousr string
Mongopw string
Mongohost string
Expand All @@ -57,4 +63,5 @@ type Flags struct {
type Config interface {
GetString(key string, defaultValue interface{}) string
GetInt(key string, defaultValue interface{}) int
GetBool(key string, defaultValue interface{}) bool
}
15 changes: 15 additions & 0 deletions config/json_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,21 @@ func (c jsonConfig) GetInt(key string, defaultValue interface{}) int {
return v
}

// Gets the configuration value given a path in the json config file
// defaults to empty value if non is found and just logs errors
func (c jsonConfig) GetBool(key string, defaultValue interface{}) bool {
if c.cfg == nil {
return false
}
v, err := c.cfg.GetBool(key, defaultValue)
if err != nil {
log.Error(fmt.Sprintf("error getting key %s from config", key), err)
return false
}

return v
}

func CreateConfigJson() error {
fmt.Println("Generating default config file")
spaceJson := defaultSpaceJson{
Expand Down
32 changes: 28 additions & 4 deletions config/map_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import (
)

type mapConfig struct {
configStr map[string]string
configInt map[string]int
configStr map[string]string
configInt map[string]int
configBool map[string]bool
}

func NewMap(envVal env.SpaceEnv, flags *Flags) Config {
configStr := make(map[string]string)
configInt := make(map[string]int)
configBool := make(map[string]bool)

// default values
configStr[SpaceStorePath] = "~/.fleek-space"
Expand All @@ -24,6 +26,8 @@ func NewMap(envVal env.SpaceEnv, flags *Flags) Config {
configInt[SpaceRestProxyServerPort] = 9997
if flags.DevMode {
configStr[Ipfsaddr] = os.Getenv(env.IpfsAddr)
configStr[Ipfsnodeaddr] = os.Getenv(env.IpfsNodeAddr)
configStr[Ipfsnodepath] = os.Getenv(env.IpfsNodePath)
configStr[Mongousr] = os.Getenv(env.MongoUsr)
configStr[Mongopw] = os.Getenv(env.MongoPw)
configStr[Mongohost] = os.Getenv(env.MongoHost)
Expand All @@ -37,8 +41,14 @@ func NewMap(envVal env.SpaceEnv, flags *Flags) Config {
configStr[TextileThreadsTarget] = os.Getenv(env.TextileThreadsTarget)
configStr[TextileUserKey] = os.Getenv(env.TextileUserKey)
configStr[TextileUserSecret] = os.Getenv(env.TextileUserSecret)

if os.Getenv(env.IpfsNode) != "false" {
configBool[Ipfsnode] = true
}
} else {
configStr[Ipfsaddr] = flags.Ipfsaddr
configStr[Ipfsnodeaddr] = flags.Ipfsnodeaddr
configStr[Ipfsnodeaddr] = flags.Ipfsnodepath
configStr[Mongousr] = flags.Mongousr
configStr[Mongopw] = flags.Mongopw
configStr[Mongohost] = flags.Mongohost
Expand All @@ -52,11 +62,13 @@ func NewMap(envVal env.SpaceEnv, flags *Flags) Config {
configStr[TextileThreadsTarget] = flags.TextileThreadsTarget
configStr[TextileUserKey] = flags.TextileUserKey
configStr[TextileUserSecret] = flags.TextileUserSecret
configBool[Ipfsnode] = flags.Ipfsnode
}

c := mapConfig{
configStr: configStr,
configInt: configInt,
configStr: configStr,
configInt: configInt,
configBool: configBool,
}

return c
Expand Down Expand Up @@ -85,3 +97,15 @@ func (m mapConfig) GetInt(key string, defaultValue interface{}) int {

return 0
}

func (m mapConfig) GetBool(key string, defaultValue interface{}) bool {
if val, exists := m.configBool[key]; exists {
return val
}

if boolVal, ok := defaultValue.(bool); ok {
return boolVal
}

return false
}
3 changes: 3 additions & 0 deletions core/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ const (
SpaceWorkingDir = "SPACE_APP_DIR"
LogLevel = "LOG_LEVEL"
IpfsAddr = "IPFS_ADDR"
IpfsNode = "IPFS_NODE"
IpfsNodeAddr = "IPFS_NODE_ADDR"
IpfsNodePath = "IPFS_NODE_PATH"
MongoUsr = "MONGO_USR"
MongoPw = "MONGO_PW"
MongoHost = "MONGO_HOST"
Expand Down
Loading

0 comments on commit c1849b7

Please sign in to comment.