forked from jqs7/Jqs7Bot
/
webserver.go
82 lines (75 loc) · 1.71 KB
/
webserver.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
package main
import (
"net/http"
"net/url"
"time"
"github.com/DeanThompson/ginpprof"
"github.com/gin-gonic/gin"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
func GinServer() {
r := gin.Default()
if runMode == "debug" {
gin.SetMode(gin.DebugMode)
} else {
gin.SetMode(gin.ReleaseMode)
}
r.LoadHTMLGlob("html/*")
r.Static("/assets", "./assets")
r.GET("/", func(c *gin.Context) {
var total, users []interface{}
limit := time.Now().AddDate(0, 0, -100)
M("dailyTotal", func(c *mgo.Collection) {
c.Find(bson.M{
"date": bson.M{"$gt": limit},
}).Sort("date").All(&total)
})
M("dailyUsersCount", func(c *mgo.Collection) {
c.Find(bson.M{
"date": bson.M{"$gt": limit},
}).Sort("date").All(&users)
})
c.HTML(http.StatusOK, "index.html",
gin.H{"total": total, "users": users})
})
r.GET("/rank/:date", func(c *gin.Context) {
s := c.Params.ByName("date")
loc, _ := time.LoadLocation("Asia/Shanghai")
date, err := time.ParseInLocation("2006-01-02", s, loc)
if err != nil {
return
}
var result interface{}
M("dailyRank", func(c *mgo.Collection) {
c.Find(
bson.M{"date": date},
).One(&result)
})
c.JSON(http.StatusOK, result)
})
r.GET("/user/:name", func(c *gin.Context) {
limit := time.Now().AddDate(0, 0, -100)
s, err := url.QueryUnescape(c.Params.ByName("name"))
if err != nil {
return
}
var result []interface{}
userid := rc.HGet("tgUsersName", s).Val()
M("dailyUser", func(c *mgo.Collection) {
c.Find(
bson.M{
"user": userid,
"date": bson.M{"$gt": limit},
},
).Sort("date").All(&result)
})
c.HTML(http.StatusOK, "user.html",
gin.H{
"result": result,
"userName": s,
})
})
ginpprof.Wrapper(r)
r.Run(":6060")
}