Commit 2bd84312 authored by Juan Batiz-Benet's avatar Juan Batiz-Benet

added command context initialization

parent 38182447
......@@ -8,9 +8,11 @@ import (
flag "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
commander "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
config "github.com/jbenet/go-ipfs/config"
core "github.com/jbenet/go-ipfs/core"
daemon "github.com/jbenet/go-ipfs/daemon"
u "github.com/jbenet/go-ipfs/util"
)
......@@ -100,6 +102,7 @@ func main() {
return
}
// localNode constructs a node
func localNode(confdir string, online bool) (*core.IpfsNode, error) {
filename, err := config.Filename(confdir)
if err != nil {
......@@ -131,3 +134,50 @@ func getConfigDir(c *commander.Command) (string, error) {
return u.TildeExpansion(confStr)
}
// cmdContext is a wrapper structure that keeps a node, a daemonlistener, and
// a config directory together. These three are needed for most commands.
type cmdContext struct {
node *core.IpfsNode
daemon *daemon.DaemonListener
configDir string
}
// setupCmdContext initializes a cmdContext structure from a given command.
func setupCmdContext(c *commander.Command, online bool) (cc cmdContext, err error) {
cc.configDir, err = getConfigDir(c.Parent.Parent)
if err != nil {
return
}
cc.node, err = localNode(cc.configDir, online)
if err != nil {
return
}
cc.daemon, err = setupDaemon(cc.configDir, cc.node)
if err != nil {
return
}
return
}
// setupDaemon sets up the daemon corresponding to given node.
func setupDaemon(confdir string, node *core.IpfsNode) (*daemon.DaemonListener, error) {
if node.Config.Addresses.API == "" {
return nil, errors.New("no config.Addresses.API endpoint supplied")
}
maddr, err := ma.NewMultiaddr(node.Config.Addresses.API)
if err != nil {
return nil, err
}
dl, err := daemon.NewDaemonListener(node, maddr, confdir)
if err != nil {
return nil, err
}
go dl.Listen()
return dl, nil
}
package main
import (
"errors"
"os"
"os/signal"
"syscall"
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
"github.com/jbenet/go-ipfs/daemon"
)
var cmdIpfsRun = &commander.Command{
......@@ -19,30 +19,18 @@ var cmdIpfsRun = &commander.Command{
}
func runCmd(c *commander.Command, inp []string) error {
conf, err := getConfigDir(c.Parent)
if err != nil {
return err
}
n, err := localNode(conf, true)
cc, err := setupCmdContext(c, true)
if err != nil {
return err
}
// launch the RPC endpoint.
if n.Config.Addresses.API == "" {
return errors.New("no config.Addresses.API endpoint supplied")
}
sigc := make(chan os.Signal, 1)
signal.Notify(sigc, syscall.SIGHUP, syscall.SIGINT,
syscall.SIGTERM, syscall.SIGQUIT)
maddr, err := ma.NewMultiaddr(n.Config.Addresses.API)
if err != nil {
return err
}
// wait until we get a signal to exit.
<-sigc
dl, err := daemon.NewDaemonListener(n, maddr, conf)
if err != nil {
return err
}
dl.Listen()
dl.Close()
cc.daemon.Close()
return nil
}
package main
import (
"errors"
"fmt"
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
"github.com/jbenet/go-ipfs/daemon"
h "github.com/jbenet/go-ipfs/server/http"
)
......@@ -16,58 +14,36 @@ var cmdIpfsServe = &commander.Command{
UsageLine: "serve",
Short: "Serve an interface to ipfs",
Subcommands: []*commander.Command{
cmdIpfsServeHttp,
cmdIpfsServeHTTP,
},
Flag: *flag.NewFlagSet("ipfs-serve", flag.ExitOnError),
}
var cmdIpfsServeHttp = &commander.Command{
var cmdIpfsServeHTTP = &commander.Command{
UsageLine: "http",
Short: "Serve an HTTP API",
Long: `ipfs serve http - Serve an http gateway into ipfs.`,
Run: serveHttpCmd,
Run: serveHTTPCmd,
Flag: *flag.NewFlagSet("ipfs-serve-http", flag.ExitOnError),
}
func init() {
cmdIpfsServeHttp.Flag.String("address", "/ip4/127.0.0.1/tcp/8080", "Listen Address")
cmdIpfsServeHTTP.Flag.String("address", "/ip4/127.0.0.1/tcp/8080", "Listen Address")
}
func serveHttpCmd(c *commander.Command, _ []string) error {
conf, err := getConfigDir(c.Parent.Parent)
func serveHTTPCmd(c *commander.Command, _ []string) error {
cc, err := setupCmdContext(c, true)
if err != nil {
return err
}
n, err := localNode(conf, true)
if err != nil {
return err
}
// launch the API RPC endpoint.
if n.Config.Addresses.API == "" {
return errors.New("no config.RPCAddress endpoint supplied")
}
maddr, err := ma.NewMultiaddr(n.Config.Addresses.API)
if err != nil {
return err
}
dl, err := daemon.NewDaemonListener(n, maddr, conf)
if err != nil {
fmt.Println("Failed to create daemon listener.")
return err
}
go dl.Listen()
defer dl.Close()
defer cc.daemon.Close()
address := c.Flag.Lookup("address").Value.Get().(string)
maddr, err = ma.NewMultiaddr(address)
maddr, err := ma.NewMultiaddr(address)
if err != nil {
return err
}
fmt.Printf("Serving on %s\n", address)
return h.Serve(maddr, n)
return h.Serve(maddr, cc.node)
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment