Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-dms3
Commits
2bd84312
Commit
2bd84312
authored
Oct 02, 2014
by
Juan Batiz-Benet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added command context initialization
parent
38182447
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
69 additions
and
55 deletions
+69
-55
cmd/ipfs/ipfs.go
cmd/ipfs/ipfs.go
+50
-0
cmd/ipfs/run.go
cmd/ipfs/run.go
+10
-22
cmd/ipfs/serve.go
cmd/ipfs/serve.go
+9
-33
No files found.
cmd/ipfs/ipfs.go
View file @
2bd84312
...
...
@@ -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
}
cmd/ipfs/run.go
View file @
2bd84312
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
}
cmd/ipfs/serve.go
View file @
2bd84312
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
{
cmdIpfsServeH
ttp
,
cmdIpfsServeH
TTP
,
},
Flag
:
*
flag
.
NewFlagSet
(
"ipfs-serve"
,
flag
.
ExitOnError
),
}
var
cmdIpfsServeH
ttp
=
&
commander
.
Command
{
var
cmdIpfsServeH
TTP
=
&
commander
.
Command
{
UsageLine
:
"http"
,
Short
:
"Serve an HTTP API"
,
Long
:
`ipfs serve http - Serve an http gateway into ipfs.`
,
Run
:
serveH
ttp
Cmd
,
Run
:
serveH
TTP
Cmd
,
Flag
:
*
flag
.
NewFlagSet
(
"ipfs-serve-http"
,
flag
.
ExitOnError
),
}
func
init
()
{
cmdIpfsServeH
ttp
.
Flag
.
String
(
"address"
,
"/ip4/127.0.0.1/tcp/8080"
,
"Listen Address"
)
cmdIpfsServeH
TTP
.
Flag
.
String
(
"address"
,
"/ip4/127.0.0.1/tcp/8080"
,
"Listen Address"
)
}
func
serveH
ttp
Cmd
(
c
*
commander
.
Command
,
_
[]
string
)
error
{
c
onf
,
err
:=
g
et
ConfigDir
(
c
.
Parent
.
Parent
)
func
serveH
TTP
Cmd
(
c
*
commander
.
Command
,
_
[]
string
)
error
{
c
c
,
err
:=
s
et
upCmdContext
(
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
)
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment