1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-07-01 10:49:24 +08:00

cmd/ipfs: Obtain lock when starting daemon

This commit is contained in:
Matt Bell
2014-10-27 16:20:48 -07:00
committed by Juan Batiz-Benet
parent d87aad1e3a
commit e011d950bb

View File

@ -1,12 +1,19 @@
package main package main
import ( import (
"fmt"
"net/http" "net/http"
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/camlistore/lock"
cmds "github.com/jbenet/go-ipfs/commands" cmds "github.com/jbenet/go-ipfs/commands"
cmdsHttp "github.com/jbenet/go-ipfs/commands/http" cmdsHttp "github.com/jbenet/go-ipfs/commands/http"
"github.com/jbenet/go-ipfs/config"
) )
// DaemonLockFile is the filename of the daemon lock, relative to config dir
const DaemonLockFile = "daemon.lock"
var Daemon = &cmds.Command{ var Daemon = &cmds.Command{
Options: []cmds.Option{}, Options: []cmds.Option{},
Help: "TODO", Help: "TODO",
@ -15,13 +22,33 @@ var Daemon = &cmds.Command{
} }
func daemonFunc(req cmds.Request, res cmds.Response) { func daemonFunc(req cmds.Request, res cmds.Response) {
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()
handler := cmdsHttp.Handler{} handler := cmdsHttp.Handler{}
http.Handle(cmdsHttp.ApiPath+"/", handler) http.Handle(cmdsHttp.ApiPath+"/", handler)
// TODO: load listen address/port from config/options // TODO: load listen address/port from config/options
err := http.ListenAndServe(":8080", nil) err = http.ListenAndServe(":8080", nil)
if err != nil { if err != nil {
res.SetError(err, cmds.ErrNormal) res.SetError(err, cmds.ErrNormal)
return return
} }
// TODO: log to indicate that we are now listening // TODO: log to indicate that we are now listening
} }