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

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

7 8
	"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/camlistore/lock"

9 10
	cmds "github.com/jbenet/go-ipfs/commands"
	cmdsHttp "github.com/jbenet/go-ipfs/commands/http"
11
	"github.com/jbenet/go-ipfs/config"
12 13
)

14 15 16
// DaemonLockFile is the filename of the daemon lock, relative to config dir
const DaemonLockFile = "daemon.lock"

17 18 19 20 21 22 23 24
var Daemon = &cmds.Command{
	Options:     []cmds.Option{},
	Help:        "TODO",
	Subcommands: map[string]*cmds.Command{},
	Run:         daemonFunc,
}

func daemonFunc(req cmds.Request, res cmds.Response) {
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
	configPath, err := getConfigPath(req)
	if err != nil {
		res.SetError(err, cmds.ErrNormal)
		return
	}

	lockPath, err := config.Path(configPath, DaemonLockFile)
	if err != nil {
		res.SetError(err, cmds.ErrNormal)
		return
	}

	lk, err := lock.Lock(lockPath)
	if err != nil {
		res.SetError(fmt.Errorf("Couldn't obtain lock. Is another daemon already running?"), cmds.ErrNormal)
		return
	}
	defer lk.Close()

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

54
}