daemon.go 1.21 KB
Newer Older
1 2 3
package main

import (
4
	"fmt"
5 6
	"net/http"

7 8
	ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
	manet "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr/net"
9

10 11
	cmds "github.com/jbenet/go-ipfs/commands"
	cmdsHttp "github.com/jbenet/go-ipfs/commands/http"
12
	"github.com/jbenet/go-ipfs/daemon"
13 14 15 16 17 18 19 20 21 22
)

var Daemon = &cmds.Command{
	Options:     []cmds.Option{},
	Help:        "TODO",
	Subcommands: map[string]*cmds.Command{},
	Run:         daemonFunc,
}

func daemonFunc(req cmds.Request, res cmds.Response) {
23
	ctx := req.Context()
24

25
	lk, err := daemon.Lock(ctx.ConfigRoot)
26 27 28 29 30 31
	if err != nil {
		res.SetError(fmt.Errorf("Couldn't obtain lock. Is another daemon already running?"), cmds.ErrNormal)
		return
	}
	defer lk.Close()

32
	addr, err := ma.NewMultiaddr(ctx.Config.Addresses.API)
33 34 35 36 37 38 39 40 41 42 43
	if err != nil {
		res.SetError(err, cmds.ErrNormal)
		return
	}

	_, host, err := manet.DialArgs(addr)
	if err != nil {
		res.SetError(err, cmds.ErrNormal)
		return
	}

44 45
	handler := cmdsHttp.Handler{}
	http.Handle(cmdsHttp.ApiPath+"/", handler)
46
	err = http.ListenAndServe(host, nil)
47 48 49 50 51
	if err != nil {
		res.SetError(err, cmds.ErrNormal)
		return
	}
	// TODO: log to indicate that we are now listening
52

53
}