forked from xbbdjj/grinnodes
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
115 lines (95 loc) · 2.26 KB
/
main.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
"fmt"
"net"
"net/http"
"github.com/xbbdjj/grinnodes/chart"
"github.com/xbbdjj/grinnodes/config"
"github.com/xbbdjj/grinnodes/ip"
"github.com/xbbdjj/grinnodes/p2p"
"github.com/xbbdjj/grinnodes/storage"
"github.com/gin-gonic/gin"
)
func main() {
storage.ClearOldPeer()
go p2p.Start()
go ip.Start()
go chart.Start()
r := gin.Default()
r.LoadHTMLGlob("templates/*")
r.GET("/googlemap", func(c *gin.Context) {
gps, _ := storage.AllGPS()
type country struct {
Name string
Total int
Percent string
}
total, _ := storage.NodeTotal()
publictotal, _ := storage.NodePublicCount()
countrys := []country{}
arr, _ := storage.AllCountry()
for _, v := range arr {
c := country{
Name: v.Name,
Total: v.Total,
Percent: fmt.Sprintf("%.2f", float32(v.Total*100)/float32(total)),
}
countrys = append(countrys, c)
}
c.HTML(http.StatusOK, "indexgoogle.tmpl", gin.H{
"country": countrys,
"latlng": gps,
"total": total,
"public": publictotal,
})
})
r.GET("/", func(c *gin.Context) {
type country struct {
Name string
Total int
Percent string
}
total, _ := storage.NodeTotal()
publictotal, _ := storage.NodePublicCount()
countrys := []country{}
arr, _ := storage.AllCountry()
for _, v := range arr {
c := country{
Name: v.Name,
Total: v.Total,
Percent: fmt.Sprintf("%.2f", float32(v.Total*100)/float32(total)),
}
countrys = append(countrys, c)
}
c.HTML(http.StatusOK, "index.tmpl", gin.H{
"country": countrys,
"total": total,
"public": publictotal,
"mapboxkey": config.NewConfig().MapBoxKey,
})
})
r.GET("/geojson", func(c *gin.Context) {
geo, _ := storage.GetGeoJSON()
c.JSON(http.StatusOK, geo)
})
r.GET("/publicnodes", func(c *gin.Context) {
ip := c.DefaultQuery("ip", "")
if len(ip) > 0 && net.ParseIP(ip) == nil {
c.HTML(http.StatusOK, "error.tmpl", gin.H{
"error": "please type right ip address!",
})
return
}
res, _ := storage.PublicNodeList(ip)
c.HTML(http.StatusOK, "publicnodes.tmpl", gin.H{
"peers": res,
})
})
r.GET("/history", func(c *gin.Context) {
arr, _ := storage.GetChart()
c.HTML(http.StatusOK, "highchart.tmpl", gin.H{
"total": arr,
})
})
r.Run(":80")
}