forked from elastos/Elastos.ELA
/
server.go
66 lines (56 loc) · 1.53 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package httpnodeinfo
import (
"Elastos.ELA/common/config"
"Elastos.ELA/core/ledger"
"fmt"
"html/template"
"net/http"
"strconv"
"Elastos.ELA/net/servers"
)
type Info struct {
NodeVersion string
BlockHeight uint32
NeighborCnt int
Neighbors []NgbNodeInfo
HttpRestPort int
HttpWsPort int
HttpJsonPort int
HttpLocalPort int
NodePort int
NodeId string
}
type NgbNodeInfo struct {
NgbId string
NbrAddr string
}
var templates = template.Must(template.New("info").Parse(page))
func viewHandler(w http.ResponseWriter, r *http.Request) {
var ngbrNodersInfo []NgbNodeInfo
var node = servers.NodeForServers
ngbrNoders := node.GetNeighborNoder()
for i := 0; i < len(ngbrNoders); i++ {
ngbrNodersInfo = append(ngbrNodersInfo, NgbNodeInfo{
NgbId: fmt.Sprintf("0x%x", ngbrNoders[i].GetID()),
NbrAddr: ngbrNoders[i].GetAddr(),
})
}
pageInfo := &Info{
BlockHeight: ledger.DefaultLedger.Blockchain.BlockHeight,
NeighborCnt: len(ngbrNoders),
Neighbors: ngbrNodersInfo,
HttpRestPort: config.Parameters.HttpRestPort,
HttpWsPort: config.Parameters.HttpWsPort,
HttpJsonPort: config.Parameters.HttpJsonPort,
NodePort: config.Parameters.NodePort,
NodeId: fmt.Sprintf("0x%x", node.GetID()),
}
err := templates.ExecuteTemplate(w, "info", pageInfo)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func StartServer() {
http.HandleFunc("/info", viewHandler)
http.ListenAndServe(":"+strconv.Itoa(int(config.Parameters.HttpInfoPort)), nil)
}