-
Notifications
You must be signed in to change notification settings - Fork 376
/
http_api.go
56 lines (49 loc) · 1.59 KB
/
http_api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package ipfsutil
import (
"net/http"
ipfswebui "berty.tech/ipfs-webui-packed"
"github.com/ipfs/go-ipfs/commands"
"github.com/ipfs/go-ipfs/core"
"github.com/ipfs/go-ipfs/core/corehttp"
"go.uber.org/zap"
)
// ServeHTTPApi collects options, creates listener, prints status message and starts serving requests
func ServeHTTPApi(logger *zap.Logger, node *core.IpfsNode, rootDirectory string) {
// mandatory for the IPFS API server
cctx := commands.Context{
// needed for config access
ConfigRoot: rootDirectory,
// http handler requires it
ReqLog: &commands.ReqLog{},
// the node is already construct so we pass it
ConstructNode: func() (*core.IpfsNode, error) {
return node, nil
},
}
var APIAddr string
cfg, err := node.Repo.Config()
if err != nil || len(cfg.Addresses.API) == 0 {
APIAddr = "ip4/127.0.0.1/tcp/5001"
} else {
APIAddr = cfg.Addresses.API[0]
}
var opts = []corehttp.ServeOption{
corehttp.CommandsOption(cctx),
// allow redirections from the http://{apiAddr}/webui to the actual webui address
corehttp.WebUIOption,
corehttp.GatewayOption(false, corehttp.WebUIPaths...),
}
// start the server in a new goroutine since it is not async
go func(node *core.IpfsNode, apiAddr string, opts ...corehttp.ServeOption) {
err := corehttp.ListenAndServe(node, apiAddr, opts...)
if err != nil {
logger.Error("corehttp.ListenAndServe failed", zap.Error(err))
}
}(node, APIAddr, opts...)
}
func ServeHTTPWebui(logger *zap.Logger) {
dir := http.FileServer(ipfswebui.Dir())
go func(dir http.Handler) {
logger.Error(http.ListenAndServe(":3000", dir).Error())
}(dir)
}