github.com/piotrnar/gocoin@v0.0.0-20240512203912-faa0448c5e96/client/usif/webui/home.go (about) 1 package webui 2 3 import ( 4 "encoding/json" 5 "net/http" 6 "strings" 7 "sync" 8 "time" 9 10 "github.com/piotrnar/gocoin/client/common" 11 "github.com/piotrnar/gocoin/client/network" 12 "github.com/piotrnar/gocoin/client/network/peersdb" 13 "github.com/piotrnar/gocoin/client/usif" 14 "github.com/piotrnar/gocoin/lib/btc" 15 "github.com/piotrnar/gocoin/lib/others/sys" 16 ) 17 18 var ( 19 mutexHrate sync.Mutex 20 lastHrate float64 21 nextHrate time.Time 22 ) 23 24 func p_home(w http.ResponseWriter, r *http.Request) { 25 if !ipchecker(r) { 26 return 27 } 28 29 // The handler also gets called for /favicon.ico 30 if r.URL.Path != "/" { 31 http.NotFound(w, r) 32 return 33 } 34 35 s := load_template("home.html") 36 37 if !common.CFG.WebUI.ServerMode { 38 common.LockCfg() 39 dat, _ := json.MarshalIndent(&common.CFG, "", " ") 40 common.UnlockCfg() 41 s = strings.Replace(s, "{CONFIG_FILE}", strings.Replace(string(dat), ",\"", ", \"", -1), 1) 42 } 43 44 fees_chart := load_template("fees_chart.html") 45 s = strings.Replace(s, "<!-- include fees_chart.html -->", fees_chart, 1) 46 47 s = strings.Replace(s, "<!--PUB_AUTH_KEY-->", common.PublicKey, 1) 48 49 write_html_head(w, r) 50 w.Write([]byte(s)) 51 write_html_tail(w) 52 } 53 54 func json_status(w http.ResponseWriter, r *http.Request) { 55 if !ipchecker(r) { 56 return 57 } 58 59 var out struct { 60 Height uint32 61 Hash string 62 Timestamp uint32 63 Received int64 64 Time_now int64 65 Diff float64 66 Median uint32 67 Version uint32 68 MinValue uint64 69 WalletON bool 70 LastTrustedBlockHeight uint32 71 LastHeaderHeight uint32 72 BlockChainSynchronized bool 73 } 74 common.Last.Mutex.Lock() 75 out.Height = common.Last.Block.Height 76 out.Hash = common.Last.Block.BlockHash.String() 77 out.Timestamp = common.Last.Block.Timestamp() 78 out.Received = common.Last.Time.Unix() 79 out.Time_now = time.Now().Unix() 80 out.Diff = btc.GetDifficulty(common.Last.Block.Bits()) 81 out.Median = common.Last.Block.GetMedianTimePast() 82 out.Version = common.Last.Block.BlockVersion() 83 common.Last.Mutex.Unlock() 84 out.MinValue = common.AllBalMinVal() 85 out.WalletON = common.GetBool(&common.WalletON) 86 out.LastTrustedBlockHeight = common.GetUint32(&common.LastTrustedBlockHeight) 87 network.MutexRcv.Lock() 88 out.LastHeaderHeight = network.LastCommitedHeader.Height 89 network.MutexRcv.Unlock() 90 out.BlockChainSynchronized = common.GetBool(&common.BlockChainSynchronized) 91 92 bx, er := json.Marshal(out) 93 if er == nil { 94 w.Header()["Content-Type"] = []string{"application/json"} 95 w.Write(bx) 96 } else { 97 println(er.Error()) 98 } 99 } 100 101 func json_system(w http.ResponseWriter, r *http.Request) { 102 if !ipchecker(r) { 103 return 104 } 105 106 var out struct { 107 Blocks_cached int 108 Blocks_on_disk uint32 109 BlocksToGet int 110 Known_peers int 111 Node_uptime uint64 112 Net_block_qsize int 113 Net_tx_qsize int 114 Heap_size uint64 115 Heap_sysmem uint64 116 Qdb_extramem int64 117 Ecdsa_verify_cnt uint64 118 Average_block_size int 119 Average_fee float64 120 LastHeaderHeight uint32 121 NetworkHashRate float64 122 SavingUTXO bool 123 } 124 125 out.Blocks_cached = network.CachedBlocksLen() 126 common.Last.Mutex.Lock() 127 if common.Last.ParseTill != nil { 128 out.Blocks_on_disk = common.Last.ParseTill.Height - common.Last.Block.Height 129 } 130 common.Last.Mutex.Unlock() 131 out.BlocksToGet = network.BlocksToGetCnt() 132 out.Known_peers = peersdb.PeerDB.Count() 133 out.Node_uptime = uint64(time.Since(common.StartTime).Seconds()) 134 out.Net_block_qsize = len(network.NetBlocks) 135 out.Net_tx_qsize = len(network.NetTxs) 136 out.Heap_size, out.Heap_sysmem = sys.MemUsed() 137 by, _ := common.MemUsed() 138 out.Qdb_extramem = int64(by) 139 out.Ecdsa_verify_cnt = btc.EcdsaVerifyCnt() 140 out.Average_block_size = common.AverageBlockSize.Get() 141 out.Average_fee = usif.GetAverageFee() 142 network.MutexRcv.Lock() 143 out.LastHeaderHeight = network.LastCommitedHeader.Height 144 network.MutexRcv.Unlock() 145 146 mutexHrate.Lock() 147 if nextHrate.IsZero() || time.Now().After(nextHrate) { 148 lastHrate = usif.GetNetworkHashRateNum() 149 nextHrate = time.Now().Add(time.Minute) 150 } 151 out.NetworkHashRate = lastHrate 152 mutexHrate.Unlock() 153 154 out.SavingUTXO = common.BlockChain.Unspent.WritingInProgress.Get() 155 156 bx, er := json.Marshal(out) 157 if er == nil { 158 w.Header()["Content-Type"] = []string{"application/json"} 159 w.Write(bx) 160 } else { 161 println(er.Error()) 162 } 163 }