11package api
22
33import (
4- "context"
54 "net"
6- "net/http"
75
86 "github.com/ipfs/go-datastore"
7+ ma "github.com/multiformats/go-multiaddr"
98 pb "github.com/textileio/filecoin/api/pb"
109 "github.com/textileio/filecoin/deals"
1110 "github.com/textileio/filecoin/lotus"
11+ "github.com/textileio/filecoin/util"
1212 "google.golang.org/grpc"
1313)
1414
1515// Server represents the configured lotus client and filecoin grpc server
1616type Server struct {
17- rpc * grpc.Server
18- proxy * http.Server
19- service * service
20-
21- ctx context.Context
22- cancel context.CancelFunc
17+ rpc * grpc.Server
18+ service * service
19+ closeLotus func ()
2320}
2421
2522// Config specifies server settings.
2623type Config struct {
27- LotusAddress string
24+ LotusAddress ma. Multiaddr
2825 LotusAuthToken string
29- GrpcHostAddress string
26+ GrpcHostAddress ma. Multiaddr
3027}
3128
3229// NewServer starts and returns a new server with the given configuration.
33- func NewServer (ctx context.Context , conf Config ) (* Server , error ) {
34- c , cls , err := lotus .New (conf .LotusAddress , conf .LotusAuthToken )
30+ func NewServer (conf Config ) (* Server , error ) {
31+ lotusAddr , err := util .TCPAddrFromMultiAddr (conf .LotusAddress )
32+ if err != nil {
33+ return nil , err
34+ }
35+ c , cls , err := lotus .New (lotusAddr , conf .LotusAuthToken )
3536 if err != nil {
3637 panic (err )
3738 }
38- defer cls ()
3939
40+ // ToDo: use some other persistent data store
4041 dm := deals .New (c , datastore .NewMapDatastore ())
4142
42- ctx , cancel := context .WithCancel (ctx )
4343 s := & Server {
44- rpc : grpc .NewServer (),
45- service : & service {dealModule : dm },
46- ctx : ctx ,
47- cancel : cancel ,
44+ rpc : grpc .NewServer (),
45+ service : & service {dealModule : dm },
46+ closeLotus : cls ,
4847 }
4948
50- listener , err := net .Listen ("tcp" , conf .GrpcHostAddress )
49+ grpcAddr , err := util .TCPAddrFromMultiAddr (conf .GrpcHostAddress )
50+ if err != nil {
51+ return nil , err
52+ }
53+ listener , err := net .Listen ("tcp" , grpcAddr )
5154 if err != nil {
5255 return nil , err
5356 }
@@ -58,3 +61,10 @@ func NewServer(ctx context.Context, conf Config) (*Server, error) {
5861
5962 return s , nil
6063}
64+
65+ // Close shuts down the server
66+ func (s * Server ) Close () {
67+ s .service .dealModule .Close ()
68+ s .closeLotus ()
69+ s .rpc .Stop ()
70+ }
0 commit comments