From b52b861db2f89947d1bb2312509b6806b50580e0 Mon Sep 17 00:00:00 2001 From: Martin Boehm Date: Wed, 27 Jun 2018 00:44:23 +0200 Subject: [PATCH] Add initial tx explorer template + favicon --- server/public.go | 46 ++++++++++++++++++++++++++++++------- static/favicon.ico | Bin 0 -> 856 bytes static/templates/base.html | 17 ++++++++++++++ static/templates/tx.html | 3 +++ static/test.html | 5 ++-- 5 files changed, 60 insertions(+), 11 deletions(-) create mode 100644 static/favicon.ico create mode 100644 static/templates/base.html create mode 100644 static/templates/tx.html diff --git a/server/public.go b/server/public.go index 368f99e7..af2c16d1 100644 --- a/server/public.go +++ b/server/public.go @@ -8,6 +8,7 @@ import ( "context" "encoding/json" "fmt" + "html/template" "net/http" "strconv" "strings" @@ -32,6 +33,7 @@ type PublicServer struct { explorerURL string metrics *common.Metrics is *common.InternalState + txTpl *template.Template } // NewPublicServerS creates new public server http interface to blockbook and returns its handle @@ -69,6 +71,8 @@ func NewPublicServer(binding string, certFiles string, db *db.RocksDB, chain bch is: is, } + // favicon + serveMux.Handle(path+"favicon.ico", http.FileServer(http.Dir("./static/"))) // support for tests of socket.io interface serveMux.Handle(path+"test.html", http.FileServer(http.Dir("./static/"))) // redirect to Bitcore for details of transaction @@ -76,12 +80,16 @@ func NewPublicServer(binding string, certFiles string, db *db.RocksDB, chain bch serveMux.HandleFunc(path+"address/", s.addressRedirect) // explorer serveMux.HandleFunc(path+"explorer/tx/", s.explorerTx) - // API call used to detect state of Blockbook + // API calls serveMux.HandleFunc(path+"api/block-index/", s.apiBlockIndex) + serveMux.HandleFunc(path+"api/tx/", s.apiTx) // handle socket.io serveMux.Handle(path+"socket.io/", socketio.GetHandler()) // default handler serveMux.HandleFunc(path, s.index) + + s.txTpl = template.Must(template.New("tx").ParseFiles("./static/templates/tx.html", "./static/templates/base.html")) + return s, nil } @@ -151,20 +159,22 @@ func (s *PublicServer) addressRedirect(w http.ResponseWriter, r *http.Request) { func (s *PublicServer) explorerTx(w http.ResponseWriter, r *http.Request) { var tx *api.Tx - var err error if i := strings.LastIndexByte(r.URL.Path, '/'); i > 0 { txid := r.URL.Path[i+1:] bestheight, _, err := s.db.GetBestBlock() if err == nil { tx, err = s.api.GetTransaction(txid, bestheight, true) - } - } - if err == nil { - buf, err := json.MarshalIndent(tx, "", " ") - if err != nil { + } else { glog.Error(err) } - w.Write(buf) + } + w.Header().Set("Content-Type", "text/html; charset=utf-8") + data := struct { + CoinName string + Tx *api.Tx + }{s.is.Coin, tx} + if err := s.txTpl.ExecuteTemplate(w, "base.html", data); err != nil { + glog.Error(err) } } @@ -199,6 +209,7 @@ func (s *PublicServer) index(w http.ResponseWriter, r *http.Request) { LastMempoolTime: mt, About: blockbookAbout, } + w.Header().Set("Content-Type", "application/json; charset=utf-8") buf, err := json.MarshalIndent(a, "", " ") if err != nil { glog.Error(err) @@ -231,6 +242,25 @@ func (s *PublicServer) apiBlockIndex(w http.ResponseWriter, r *http.Request) { BlockHash: hash, About: blockbookAbout, } + w.Header().Set("Content-Type", "application/json; charset=utf-8") json.NewEncoder(w).Encode(r) } } + +func (s *PublicServer) apiTx(w http.ResponseWriter, r *http.Request) { + var tx *api.Tx + var err error + if i := strings.LastIndexByte(r.URL.Path, '/'); i > 0 { + txid := r.URL.Path[i+1:] + bestheight, _, err := s.db.GetBestBlock() + if err == nil { + tx, err = s.api.GetTransaction(txid, bestheight, true) + } else { + glog.Error(err) + } + } + if err == nil { + w.Header().Set("Content-Type", "application/json; charset=utf-8") + json.NewEncoder(w).Encode(tx) + } +} diff --git a/static/favicon.ico b/static/favicon.ico new file mode 100644 index 0000000000000000000000000000000000000000..a1b20e0a2698c47a454a5143ad4adb622c828eba GIT binary patch literal 856 zcmV-e1E>6nP)Px&5=lfsR9Fe^S5HV3Q5gTenUzh~%GDw$#2IGVH8xY&OCB`OQ5bgc(jkbv1QHf? z^;YI72s#)g^ykp22%2n(n;5Sr~h7_U+EtA0%)t@B9D# z-uJ$J?^}d*etUJjn=y707#v~Bk`s@MoBRp2TxrQ|ICf&p|@F!97SRGGs~@rLO4eEyjythR;%5Np_Smx6Rl z0xcq-TSdjE>uPF@zarV-%w{&+{~bBkIuE$?IiwFaQAI zm{fJikp*&PR2fmn=%xtxA~L<9Y5i+*2HV$YA{HFy80qJEnMc$4cX+hBQj@fMv1%ic z*B>k^%j;cQiqL4$ga-3F{T8pN16eye=JySA1b&~t%5OLTX%Ccd5uc(0)8+Ci5(Y=9 zCz`8RlvJSrD1f)<@)3UHL4i$8H_0Xtz=j{eCVHJQ&|p)kR3lwN zyMit&S0Da0M4X~oa5P$1cRQ2G{E!7V45Jx~#UXl!Pt$W66KwwYHDoNw9Cl;5uwWay zemiv#kcmnqkK^fiF6u&buF|i`^ zsR+O`Zfk3cFBJ-BsI-H$)4(Cq^`pcu8K@(cZ9SAt&|d74EzeY=J3BjTzs=2^qAB?t zHJK)}rNmE2e%Z?9p3C$ySzrbHPCY$6k=dD + + + + + + + + + TREZOR {{.CoinName}} Explorer + + + + {{template "tx" .Tx}} + + + \ No newline at end of file diff --git a/static/templates/tx.html b/static/templates/tx.html new file mode 100644 index 00000000..ea3233e6 --- /dev/null +++ b/static/templates/tx.html @@ -0,0 +1,3 @@ +{{define "tx"}} +{{.Txid}} +{{end}} \ No newline at end of file diff --git a/static/test.html b/static/test.html index 2d0d1733..e5e0789f 100644 --- a/static/test.html +++ b/static/test.html @@ -4,8 +4,7 @@ - +