Redirect blockchain explorer requests to external site

indexv1
Martin Boehm 2018-02-26 16:21:58 +01:00
parent ad68018b1a
commit 16fc1b4797
2 changed files with 33 additions and 17 deletions

View File

@ -61,6 +61,8 @@ var (
certFiles = flag.String("certfile", "", "to enable SSL specify path to certificate files without extension, expecting <certfile>.crt and <certfile>.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)
}

View File

@ -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"`