forked from OpenBazaar/openbazaar-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gateway.go
88 lines (74 loc) · 2.06 KB
/
gateway.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
package api
import (
"gx/ipfs/QmQopLATEYMNg7dVqZRNDfeE2S1yKy8zrRh5xnYiuqeZBn/goprocess"
manet "gx/ipfs/QmUBa4w6CbHJUMeGJPDiMEDWsM93xToK1fTnFXnrC8Hksw/go-multiaddr-net"
"net"
"net/http"
"time"
"github.com/OpenBazaar/openbazaar-go/core"
"github.com/ipfs/go-ipfs/commands"
"github.com/ipfs/go-ipfs/core/corehttp"
"github.com/op/go-logging"
)
var log = logging.MustGetLogger("api")
func makeHandler(n *core.OpenBazaarNode, ctx commands.Context, l net.Listener, options ...corehttp.ServeOption) (http.Handler, error) {
topMux := http.NewServeMux()
restAPI, err := newRestAPIHandler(n)
if err != nil {
return nil, err
}
wsAPI := newWSAPIHandler(ctx)
n.Broadcast = wsAPI.h.Broadcast
topMux.Handle("/ob/", restAPI)
topMux.Handle("/wallet/", restAPI)
topMux.Handle("/ws", wsAPI)
mux := topMux
for _, option := range options {
var err error
mux, err = option(n.IpfsNode, l, mux)
if err != nil {
return nil, err
}
}
return topMux, nil
}
func Serve(cb chan<- bool, node *core.OpenBazaarNode, ctx commands.Context, lis net.Listener, options ...corehttp.ServeOption) error {
handler, err := makeHandler(node, ctx, lis, options...)
cb <- true
if err != nil {
return err
}
addr, err := manet.FromNetAddr(lis.Addr())
if err != nil {
return err
}
// if the server exits beforehand
var serverError error
serverExited := make(chan struct{})
node.IpfsNode.Process().Go(func(p goprocess.Process) {
serverError = http.Serve(lis, handler)
close(serverExited)
})
// wait for server to exit.
select {
case <-serverExited:
// if node being closed before server exits, close server
case <-node.IpfsNode.Process().Closing():
log.Infof("server at %s terminating...", addr)
lis.Close()
outer:
for {
// wait until server exits
select {
case <-serverExited:
// if the server exited as we are closing, we really dont care about errors
serverError = nil
break outer
case <-time.After(5 * time.Second):
log.Infof("waiting for server at %s to terminate...", addr)
}
}
}
log.Infof("server at %s terminated", addr)
return serverError
}