Add initial tx explorer template + favicon

This commit is contained in:
Martin Boehm 2018-06-27 00:44:23 +02:00
parent 94873f4d86
commit b52b861db2
5 changed files with 60 additions and 11 deletions

View File

@ -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)
}
}

BIN
static/favicon.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 856 B

View File

@ -0,0 +1,17 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="description" content="TREZOR {{.CoinName}} Explorer">
<title>TREZOR {{.CoinName}} Explorer</title>
</head>
<body>
{{template "tx" .Tx}}
</body>
</html>

3
static/templates/tx.html Normal file
View File

@ -0,0 +1,3 @@
{{define "tx"}}
{{.Txid}}
{{end}}

View File

@ -4,8 +4,7 @@
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm"
crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<style>
.row {
margin-top: 1%;
@ -415,4 +414,4 @@
document.getElementById('serverAddress').value = window.location.protocol.replace("http", "ws") + "//" + window.location.host;
</script>
</html>
</html>