Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
G
go-ipfs
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
jihao
go-ipfs
Commits
07b3415c
提交
07b3415c
authored
5月 24, 2015
作者:
Henry
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
http endpoints: dont print before listen
also splits api, gw and fuse bring up into helper functions
上级
aad700bd
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
187 行增加
和
71 行删除
+187
-71
daemon.go
cmd/ipfs/daemon.go
+173
-64
corehttp.go
core/corehttp/corehttp.go
+14
-7
没有找到文件。
cmd/ipfs/daemon.go
浏览文件 @
07b3415c
...
...
@@ -3,13 +3,16 @@ package main
import
(
_
"expvar"
"fmt"
_
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/codahale/metrics/runtime"
"net/http"
_
"net/http/pprof"
"os"
"strings"
"sync"
_
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/codahale/metrics/runtime"
ma
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr-net"
cmds
"github.com/ipfs/go-ipfs/commands"
"github.com/ipfs/go-ipfs/core"
commands
"github.com/ipfs/go-ipfs/core/commands"
...
...
@@ -192,98 +195,76 @@ func daemonFunc(req cmds.Request, res cmds.Response) {
return
node
,
nil
}
//
verify api address is valid multiaddr
apiMaddr
,
err
:=
ma
.
NewMultiaddr
(
cfg
.
Addresses
.
API
)
//
construct api endpoint - every time
err
,
apiErrc
:=
mountHTTPapi
(
req
)
if
err
!=
nil
{
res
.
SetError
(
err
,
cmds
.
ErrNormal
)
return
}
var
gatewayMaddr
ma
.
Multiaddr
// construct http gateway - if it is set in the config
var
gwErrc
<-
chan
error
if
len
(
cfg
.
Addresses
.
Gateway
)
>
0
{
// ignore error for gateway address
// if there is an error (invalid address), then don't run the gateway
gatewayMaddr
,
_
=
ma
.
NewMultiaddr
(
cfg
.
Addresses
.
Gateway
)
if
gatewayMaddr
==
nil
{
log
.
Errorf
(
"Invalid gateway address: %s"
,
cfg
.
Addresses
.
Gateway
)
var
err
error
err
,
gwErrc
=
mountHTTPgw
(
req
)
if
err
!=
nil
{
res
.
SetError
(
err
,
cmds
.
ErrNormal
)
return
}
}
//
mount
if the user provided the --mount flag
//
construct fuse mountpoints -
if the user provided the --mount flag
mount
,
_
,
err
:=
req
.
Option
(
mountKwd
)
.
Bool
()
if
err
!=
nil
{
res
.
SetError
(
err
,
cmds
.
ErrNormal
)
return
}
if
mount
{
fsdir
,
found
,
err
:=
req
.
Option
(
ipfsMountKwd
)
.
String
()
if
err
!=
nil
{
res
.
SetError
(
err
,
cmds
.
ErrNormal
)
return
}
if
!
found
{
fsdir
=
cfg
.
Mounts
.
IPFS
}
nsdir
,
found
,
err
:=
req
.
Option
(
ipnsMountKwd
)
.
String
()
if
err
!=
nil
{
if
err
:=
mountFuse
(
req
);
err
!=
nil
{
res
.
SetError
(
err
,
cmds
.
ErrNormal
)
return
}
if
!
found
{
nsdir
=
cfg
.
Mounts
.
IPNS
}
}
err
=
commands
.
Mount
(
node
,
fsdir
,
nsdir
)
// collect long-running errors and block for shutdown
// TODO(cryptix): our fuse currently doesnt follow this pattern for graceful shutdown
for
err
:=
range
merge
(
apiErrc
,
gwErrc
)
{
if
err
!=
nil
{
res
.
SetError
(
err
,
cmds
.
ErrNormal
)
return
}
fmt
.
Printf
(
"IPFS mounted at: %s
\n
"
,
fsdir
)
fmt
.
Printf
(
"IPNS mounted at: %s
\n
"
,
nsdir
)
}
}
var
rootRedirect
corehttp
.
ServeOption
if
len
(
cfg
.
Gateway
.
RootRedirect
)
>
0
{
rootRedirect
=
corehttp
.
RedirectOption
(
""
,
cfg
.
Gateway
.
RootRedirect
)
// mountHTTPapi collects options, creates listener, prints status message and starts serving requests
func
mountHTTPapi
(
req
cmds
.
Request
)
(
error
,
<-
chan
error
)
{
cfg
,
err
:=
req
.
Context
()
.
GetConfig
()
if
err
!=
nil
{
return
fmt
.
Errorf
(
"mountHTTPapi: GetConfig() failed: %s"
,
err
),
nil
}
writable
,
writableOptionFound
,
err
:=
req
.
Option
(
writableKwd
)
.
Bool
(
)
apiMaddr
,
err
:=
ma
.
NewMultiaddr
(
cfg
.
Addresses
.
API
)
if
err
!=
nil
{
res
.
SetError
(
err
,
cmds
.
ErrNormal
)
return
return
fmt
.
Errorf
(
"mountHTTPapi: invalid API address: %q (err: %s)"
,
cfg
.
Addresses
.
API
,
err
),
nil
}
if
!
writableOptionFound
{
writable
=
cfg
.
Gateway
.
Writable
apiLis
,
err
:=
manet
.
Listen
(
apiMaddr
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"mountHTTPapi: manet.Listen(%s) failed: %s"
,
apiMaddr
,
err
),
nil
}
// we might have listened to /tcp/0 - lets see what we are listing on
apiMaddr
=
apiLis
.
Multiaddr
()
fmt
.
Printf
(
"API server listening on %s
\n
"
,
apiMaddr
)
if
gatewayMaddr
!=
nil
{
go
func
()
{
var
opts
=
[]
corehttp
.
ServeOption
{
corehttp
.
VersionOption
(),
corehttp
.
IPNSHostnameOption
(),
corehttp
.
GatewayOption
(
writable
),
}
if
rootRedirect
!=
nil
{
opts
=
append
(
opts
,
rootRedirect
)
}
if
writable
{
fmt
.
Printf
(
"Gateway (writable) server listening on %s
\n
"
,
gatewayMaddr
)
}
else
{
fmt
.
Printf
(
"Gateway (readonly) server listening on %s
\n
"
,
gatewayMaddr
)
}
err
:=
corehttp
.
ListenAndServe
(
node
,
gatewayMaddr
.
String
(),
opts
...
)
if
err
!=
nil
{
log
.
Error
(
err
)
}
}()
unrestricted
,
_
,
err
:=
req
.
Option
(
unrestrictedApiAccess
)
.
Bool
()
if
err
!=
nil
{
return
fmt
.
Errorf
(
"mountHTTPapi: Option(%s) failed: %s"
,
unrestrictedApiAccess
,
err
),
nil
}
gateway
:=
corehttp
.
NewGateway
(
corehttp
.
GatewayConfig
{
apiGw
:=
corehttp
.
NewGateway
(
corehttp
.
GatewayConfig
{
Writable
:
true
,
BlockList
:
&
corehttp
.
BlockList
{
Decider
:
func
(
s
string
)
bool
{
unrestricted
,
_
,
_
:=
req
.
Option
(
unrestrictedApiAccess
)
.
Bool
()
if
unrestricted
{
return
true
}
...
...
@@ -300,18 +281,146 @@ func daemonFunc(req cmds.Request, res cmds.Response) {
var
opts
=
[]
corehttp
.
ServeOption
{
corehttp
.
CommandsOption
(
*
req
.
Context
()),
corehttp
.
WebUIOption
,
gateway
.
ServeOption
(),
apiGw
.
ServeOption
(),
corehttp
.
VersionOption
(),
defaultMux
(
"/debug/vars"
),
defaultMux
(
"/debug/pprof/"
),
}
if
rootRedirect
!=
nil
{
opts
=
append
(
opts
,
rootRedirect
)
if
len
(
cfg
.
Gateway
.
RootRedirect
)
>
0
{
opts
=
append
(
opts
,
corehttp
.
RedirectOption
(
""
,
cfg
.
Gateway
.
RootRedirect
)
)
}
fmt
.
Printf
(
"API server listening on %s
\n
"
,
apiMaddr
)
if
err
:=
corehttp
.
ListenAndServe
(
node
,
apiMaddr
.
String
(),
opts
...
);
err
!=
nil
{
res
.
SetError
(
err
,
cmds
.
ErrNormal
)
return
node
,
err
:=
req
.
Context
()
.
ConstructNode
()
if
err
!=
nil
{
return
fmt
.
Errorf
(
"mountHTTPgw: ConstructNode() failed: %s"
,
err
),
nil
}
errc
:=
make
(
chan
error
)
go
func
()
{
errc
<-
corehttp
.
Serve
(
node
,
apiLis
.
NetListener
(),
opts
...
)
}()
return
nil
,
errc
}
// mountHTTPgw collects options, creates listener, prints status message and starts serving requests
func
mountHTTPgw
(
req
cmds
.
Request
)
(
error
,
<-
chan
error
)
{
cfg
,
err
:=
req
.
Context
()
.
GetConfig
()
if
err
!=
nil
{
return
fmt
.
Errorf
(
"mountHTTPgw: GetConfig() failed: %s"
,
err
),
nil
}
gatewayMaddr
,
err
:=
ma
.
NewMultiaddr
(
cfg
.
Addresses
.
Gateway
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"mountHTTPgw: invalid gateway address: %q (err: %s)"
,
cfg
.
Addresses
.
Gateway
,
err
),
nil
}
writable
,
writableOptionFound
,
err
:=
req
.
Option
(
writableKwd
)
.
Bool
()
if
err
!=
nil
{
return
fmt
.
Errorf
(
"mountHTTPgw: req.Option(%s) failed: %s"
,
writableKwd
,
err
),
nil
}
if
!
writableOptionFound
{
writable
=
cfg
.
Gateway
.
Writable
}
gwLis
,
err
:=
manet
.
Listen
(
gatewayMaddr
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"mountHTTPgw: manet.Listen(%s) failed: %s"
,
gatewayMaddr
,
err
),
nil
}
// we might have listened to /tcp/0 - lets see what we are listing on
gatewayMaddr
=
gwLis
.
Multiaddr
()
if
writable
{
fmt
.
Printf
(
"Gateway (writable) server listening on %s
\n
"
,
gatewayMaddr
)
}
else
{
fmt
.
Printf
(
"Gateway (readonly) server listening on %s
\n
"
,
gatewayMaddr
)
}
var
opts
=
[]
corehttp
.
ServeOption
{
corehttp
.
VersionOption
(),
corehttp
.
IPNSHostnameOption
(),
corehttp
.
GatewayOption
(
writable
),
}
if
len
(
cfg
.
Gateway
.
RootRedirect
)
>
0
{
opts
=
append
(
opts
,
corehttp
.
RedirectOption
(
""
,
cfg
.
Gateway
.
RootRedirect
))
}
node
,
err
:=
req
.
Context
()
.
ConstructNode
()
if
err
!=
nil
{
return
fmt
.
Errorf
(
"mountHTTPgw: ConstructNode() failed: %s"
,
err
),
nil
}
errc
:=
make
(
chan
error
)
go
func
()
{
errc
<-
corehttp
.
Serve
(
node
,
gwLis
.
NetListener
(),
opts
...
)
}()
return
nil
,
errc
}
//collects options and opens the fuse mountpoint
func
mountFuse
(
req
cmds
.
Request
)
error
{
cfg
,
err
:=
req
.
Context
()
.
GetConfig
()
if
err
!=
nil
{
return
fmt
.
Errorf
(
"mountFuse: GetConfig() failed: %s"
,
err
)
}
fsdir
,
found
,
err
:=
req
.
Option
(
ipfsMountKwd
)
.
String
()
if
err
!=
nil
{
return
fmt
.
Errorf
(
"mountFuse: req.Option(%s) failed: %s"
,
ipfsMountKwd
,
err
)
}
if
!
found
{
fsdir
=
cfg
.
Mounts
.
IPFS
}
nsdir
,
found
,
err
:=
req
.
Option
(
ipnsMountKwd
)
.
String
()
if
err
!=
nil
{
return
fmt
.
Errorf
(
"mountFuse: req.Option(%s) failed: %s"
,
ipnsMountKwd
,
err
)
}
if
!
found
{
nsdir
=
cfg
.
Mounts
.
IPNS
}
node
,
err
:=
req
.
Context
()
.
ConstructNode
()
if
err
!=
nil
{
return
fmt
.
Errorf
(
"mountFuse: ConstructNode() failed: %s"
,
err
)
}
err
=
commands
.
Mount
(
node
,
fsdir
,
nsdir
)
if
err
!=
nil
{
return
err
}
fmt
.
Printf
(
"IPFS mounted at: %s
\n
"
,
fsdir
)
fmt
.
Printf
(
"IPNS mounted at: %s
\n
"
,
nsdir
)
return
nil
}
// merge does fan-in of multiple read-only error channels
// taken from http://blog.golang.org/pipelines
func
merge
(
cs
...<-
chan
error
)
<-
chan
error
{
var
wg
sync
.
WaitGroup
out
:=
make
(
chan
error
)
// Start an output goroutine for each input channel in cs. output
// copies values from c to out until c is closed, then calls wg.Done.
output
:=
func
(
c
<-
chan
error
)
{
for
n
:=
range
c
{
out
<-
n
}
wg
.
Done
()
}
wg
.
Add
(
len
(
cs
))
for
_
,
c
:=
range
cs
{
if
c
!=
nil
{
go
output
(
c
)
}
}
// Start a goroutine to close out once all the output goroutines are
// done. This must start after the wg.Add call.
go
func
()
{
wg
.
Wait
()
close
(
out
)
}()
return
out
}
core/corehttp/corehttp.go
浏览文件 @
07b3415c
...
...
@@ -5,6 +5,7 @@ high-level HTTP interfaces to IPFS.
package
corehttp
import
(
"fmt"
"net"
"net/http"
"time"
...
...
@@ -49,20 +50,26 @@ func ListenAndServe(n *core.IpfsNode, listeningMultiAddr string, options ...Serv
if
err
!=
nil
{
return
err
}
handler
,
err
:=
makeHandler
(
n
,
options
...
)
list
,
err
:=
manet
.
Listen
(
addr
)
if
err
!=
nil
{
return
err
}
return
listenAndServe
(
n
,
addr
,
handler
)
// we might have listened to /tcp/0 - lets see what we are listing on
addr
=
list
.
Multiaddr
()
fmt
.
Printf
(
"API server listening on %s
\n
"
,
addr
)
return
Serve
(
n
,
list
.
NetListener
(),
options
...
)
}
func
listenAndServe
(
node
*
core
.
IpfsNode
,
addr
ma
.
Multiaddr
,
handler
http
.
Handler
)
error
{
netarg
,
host
,
err
:=
manet
.
DialArgs
(
addr
)
func
Serve
(
node
*
core
.
IpfsNode
,
lis
net
.
Listener
,
options
...
ServeOption
)
error
{
handler
,
err
:=
makeHandler
(
node
,
options
...
)
if
err
!=
nil
{
return
err
}
list
,
err
:=
net
.
Listen
(
netarg
,
host
)
addr
,
err
:=
manet
.
FromNetAddr
(
lis
.
Addr
()
)
if
err
!=
nil
{
return
err
}
...
...
@@ -75,7 +82,7 @@ func listenAndServe(node *core.IpfsNode, addr ma.Multiaddr, handler http.Handler
defer
node
.
Children
()
.
Done
()
go
func
()
{
serverError
=
http
.
Serve
(
lis
t
,
handler
)
serverError
=
http
.
Serve
(
lis
,
handler
)
close
(
serverExited
)
}()
...
...
@@ -87,7 +94,7 @@ func listenAndServe(node *core.IpfsNode, addr ma.Multiaddr, handler http.Handler
case
<-
node
.
Closing
()
:
log
.
Infof
(
"server at %s terminating..."
,
addr
)
lis
t
.
Close
()
lis
.
Close
()
outer
:
for
{
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论