hypercore-protocol / hyperdrive-daemon

Hyperdrive, batteries included.
MIT License
156 stars 24 forks source link

Cleaner interface around daemon setup & metadata file #2

Open pfrazee opened 5 years ago

pfrazee commented 5 years ago

Currently the code for setting up the daemon looks like this:

const startDaemon = require('hyperdrive-daemon')
const { createMetadata } = require('hyperdrive-daemon/lib/metadata')
const { loadMetadata, HyperdriveClient } = require('hyperdrive-daemon-client')

const DAEMON_STORAGE_PATH = require('path').join(require('os').homedir(), '.hyperdrive')
const DAEMON_PORT = 4101

async function setup () {
  // fetch daemon metadata from disk
  var metadata
  try {
    metadata = await loadMetadata()
  } catch (e) {
    await createMetadata(`localhost:${DAEMON_PORT}`)
    metadata = await loadMetadata()    
  }

  // instantiate the daemon
  await startDaemon({
    storage: DAEMON_STORAGE_PATH,
    port: DAEMON_PORT,
    metadata
  })

  var client = new HyperdriveClient(metadata.endpoint, metadata.token)
  await client.ready()
}

Two issues with this:

Ideally it'd be something more like:

const startDaemon = require('hyperdrive-daemon')
const { setupConfig, HyperdriveClient } = require('hyperdrive-daemon-client')

async function setup () {
  // fetch daemon metadata from disk
  var config = await setupConfig()

  // instantiate the daemon
  await startDaemon({
    storage: config.storage,
    port: config.port,
    metadata: config.metadata
  })

  var client = new HyperdriveClient(metadata.endpoint, metadata.token)
  await client.ready()
}