daemon.go 1.25 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 24
	// TODO: spin up a core.IpfsNode

25
	ctx := req.Context()
26

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

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

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

46
	handler := cmdsHttp.Handler{*ctx}
47
	http.Handle(cmdsHttp.ApiPath+"/", handler)
48 49 50

	fmt.Printf("API server listening on '%s'\n", host)

51
	err = http.ListenAndServe(host, nil)
52 53 54 55 56
	if err != nil {
		res.SetError(err, cmds.ErrNormal)
		return
	}
}