-
Notifications
You must be signed in to change notification settings - Fork 2
/
frisbii.go
100 lines (87 loc) · 2.7 KB
/
frisbii.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package frisbii
import (
"context"
"errors"
"net"
"net/http"
"github.com/ipfs/go-cid"
"github.com/ipfs/go-log/v2"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/ipld/go-ipld-prime/linking"
"github.com/ipni/go-libipni/metadata"
)
const ContextID = "frisbii"
var logger = log.Logger("frisbii")
var advMetadata = metadata.Default.New(metadata.IpfsGatewayHttp{})
// FrisbiiServer is the main server for the frisbii application, it starts an
// HTTP server to serve data according to the Trustless Gateway spec and it
// also provides a mechanism to announce the server to the indexer service.
type FrisbiiServer struct {
ctx context.Context
lsys linking.LinkSystem
httpOptions []HttpOption
listener net.Listener
mux *http.ServeMux
indexerProvider IndexerProvider
}
type IndexerProvider interface {
GetPublisherHttpFunc() (http.HandlerFunc, error)
NotifyPut(ctx context.Context, provider *peer.AddrInfo, contextID []byte, md metadata.Metadata) (cid.Cid, error)
}
func NewFrisbiiServer(
ctx context.Context,
lsys linking.LinkSystem,
address string,
httpOptions ...HttpOption,
) (*FrisbiiServer, error) {
listener, err := net.Listen("tcp", address)
if err != nil {
return nil, err
}
return &FrisbiiServer{
ctx: ctx,
lsys: lsys,
httpOptions: httpOptions,
listener: listener,
}, nil
}
func (fs *FrisbiiServer) Addr() net.Addr {
return fs.listener.Addr()
}
func (fs *FrisbiiServer) Serve() error {
fs.mux = http.NewServeMux()
fs.mux.Handle("/ipfs/", NewHttpIpfs(fs.ctx, fs.lsys, fs.httpOptions...))
fs.mux.Handle("/", http.NotFoundHandler())
server := &http.Server{
Addr: fs.Addr().String(),
BaseContext: func(listener net.Listener) context.Context { return fs.ctx },
Handler: NewLogMiddleware(fs.mux, fs.httpOptions...),
}
logger.Debugf("Serve() server on %s", fs.Addr().String())
return server.Serve(fs.listener)
}
func (fs *FrisbiiServer) SetIndexerProvider(handlerPath string, indexerProvider IndexerProvider) error {
fs.indexerProvider = indexerProvider
handlerFunc, err := indexerProvider.GetPublisherHttpFunc()
if err != nil {
return err
}
if handlerPath == "" || handlerPath[len(handlerPath)-1] != '/' {
handlerPath += "/"
}
fs.mux.HandleFunc(handlerPath, handlerFunc)
logger.Debugf("SetIndexerProvider() handler on %s", handlerPath)
return nil
}
func (fs *FrisbiiServer) Announce() error {
if fs.indexerProvider == nil {
return errors.New("indexer provider not setup")
}
if c, err := fs.indexerProvider.NotifyPut(fs.ctx, nil, []byte(ContextID), advMetadata); err != nil {
logger.Errorf("Announce() error: %s", err)
return err
} else {
logger.Debugw("Announce() complete", "advCid", c.String())
}
return nil
}