diff --git a/blockbook.go b/blockbook.go index 7dd412af..fcf300f9 100644 --- a/blockbook.go +++ b/blockbook.go @@ -61,6 +61,8 @@ var ( certFiles = flag.String("certfile", "", "to enable SSL specify path to certificate files without extension, expecting .crt and .key, (default no SSL)") zeroMQBinding = flag.String("zeromq", "", "binding to zeromq, if missing no zeromq connection") + + insightWeb = flag.String("insight", "", "address of the insight Bitcoin blockchain explorer") ) var ( @@ -157,7 +159,7 @@ func main() { var socketIoServer *server.SocketIoServer if *socketIoBinding != "" { - socketIoServer, err = server.NewSocketIoServer(*socketIoBinding, *certFiles, index, mempool, chain) + socketIoServer, err = server.NewSocketIoServer(*socketIoBinding, *certFiles, index, mempool, chain, *insightWeb) if err != nil { glog.Fatal("socketio: ", err) } diff --git a/server/socketio.go b/server/socketio.go index b14ee7ad..3dfd4ff6 100644 --- a/server/socketio.go +++ b/server/socketio.go @@ -17,17 +17,18 @@ import ( // SocketIoServer is handle to SocketIoServer type SocketIoServer struct { - binding string - certFiles string - server *gosocketio.Server - https *http.Server - db *db.RocksDB - mempool *bchain.Mempool - chain *bchain.BitcoinRPC + binding string + certFiles string + server *gosocketio.Server + https *http.Server + db *db.RocksDB + mempool *bchain.Mempool + chain *bchain.BitcoinRPC + insightWeb string } // NewSocketIoServer creates new SocketIo interface to blockbook and returns its handle -func NewSocketIoServer(binding string, certFiles string, db *db.RocksDB, mempool *bchain.Mempool, chain *bchain.BitcoinRPC) (*SocketIoServer, error) { +func NewSocketIoServer(binding string, certFiles string, db *db.RocksDB, mempool *bchain.Mempool, chain *bchain.BitcoinRPC, insightWeb string) (*SocketIoServer, error) { server := gosocketio.NewServer(transport.GetDefaultWebsocketTransport()) server.On(gosocketio.OnConnection, func(c *gosocketio.Channel) { @@ -49,22 +50,29 @@ func NewSocketIoServer(binding string, certFiles string, db *db.RocksDB, mempool addr, path := splitBinding(binding) serveMux := http.NewServeMux() - serveMux.Handle(path, server) https := &http.Server{ Addr: addr, Handler: serveMux, } s := &SocketIoServer{ - binding: binding, - certFiles: certFiles, - https: https, - server: server, - db: db, - mempool: mempool, - chain: chain, + binding: binding, + certFiles: certFiles, + https: https, + server: server, + db: db, + mempool: mempool, + chain: chain, + insightWeb: insightWeb, } + // support for tests of socket.io interface + serveMux.Handle(path+"test.html", http.FileServer(http.Dir("./server/static/"))) + // redirect to Bitcore for details of transaction + serveMux.HandleFunc(path+"tx/", s.txRedirect) + // handle socket.io + serveMux.Handle(path, server) + server.On("message", s.onMessage) server.On("subscribe", s.onSubscribe) @@ -101,6 +109,12 @@ func (s *SocketIoServer) Shutdown(ctx context.Context) error { return s.https.Shutdown(ctx) } +func (s *SocketIoServer) txRedirect(w http.ResponseWriter, r *http.Request) { + if s.insightWeb != "" { + http.Redirect(w, r, s.insightWeb+r.URL.Path, 302) + } +} + type reqRange struct { Start int `json:"start"` End int `json:"end"`