-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
185 lines (164 loc) · 3.94 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package main
import (
"context"
"embed"
"io/ioutil"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"sync"
"time"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/go-redis/redis/v8"
"github.com/thep0y/go-logger/log"
"gopkg.in/yaml.v3"
)
type Config struct {
Listening string `json:"listening"`
Redis struct {
Addr string `json:"addr"`
Password string `json:"password"`
Db int `json:"db"`
Prefix string `json:"prefix"`
} `json:"redis"`
}
var (
//go:embed src/config.yaml
DefaultConfig embed.FS
config Config
RedisServer *redis.Client
)
func init() {
log.ErrorPrefix.File = false
// 初始化配置
_, err := os.Stat("config.yaml")
if os.IsNotExist(err) {
cf, err := DefaultConfig.ReadFile("src/config.yaml")
if err != nil {
log.Error(err)
panic(err)
}
err = ioutil.WriteFile("config.yaml", cf, 0644)
if err != nil {
log.Error(err)
panic(err)
} else {
log.Info("已创建配置文件,请修改后重新运行")
os.Exit(0)
}
} else if err != nil {
log.Error(err)
panic(err)
}
js, err := ioutil.ReadFile("config.yaml")
if err != nil {
log.Error(err)
panic(err)
}
err = yaml.Unmarshal(js, &config)
if err != nil {
log.Error(err.Error())
panic(err)
}
if config.Redis.Prefix != "" && !strings.HasSuffix(config.Redis.Prefix, ":") {
config.Redis.Prefix += ":"
}
// 创建Redis客户端
RedisServer = redis.NewClient(&redis.Options{
Addr: config.Redis.Addr,
Password: config.Redis.Password,
DB: config.Redis.Db,
})
// 测试Redis可用性
for i := 3; i > 0; i-- {
_, err = RedisServer.Ping(context.Background()).Result()
if err != nil {
log.Errorf("Error.致命错误!Redis连接失败,五秒后重试,剩余%d次", i)
if i == 0 {
panic(err.Error())
} else {
time.Sleep(time.Second * 5)
continue
}
}
}
}
func main() {
gin.SetMode(gin.ReleaseMode)
server := gin.New()
server.Use(cors.Default(), gin.Recovery())
server.GET("/", func(c *gin.Context) {
jsonpCallback := c.Query("jsonpCallback")
if jsonpCallback == "" || c.Request.Referer() == "" {
c.JSON(http.StatusNotFound, gin.H{
"code": http.StatusNotFound,
"message": "请求错误",
})
return
}
u, err := url.ParseRequestURI(c.Request.Referer())
if err != nil {
log.Error(err.Error())
c.JSON(http.StatusInternalServerError, gin.H{
"code": http.StatusInternalServerError,
"message": "服务器内部错误",
})
return
}
host := u.Hostname()
path := u.Path
var (
site_uv string = "0" // 站点总访问人数
site_pv string = "0" // 站点访问量
page_pv string = "0" // 页面访问量
wg sync.WaitGroup // 协程等待
)
wg.Add(3)
// 计算 site_uv
go func() {
defer wg.Done()
RedisServer.SAdd(context.Background(), config.Redis.Prefix+"site_uv:"+host, c.ClientIP())
suv, err := RedisServer.SCard(context.Background(), config.Redis.Prefix+"site_uv:"+host).Result()
if err != nil {
log.Error(err.Error())
return
}
site_uv = strconv.FormatInt(suv, 10)
}()
// 计算 site_pv
go func() {
defer wg.Done()
spv, err := RedisServer.HIncrBy(context.Background(),
config.Redis.Prefix+"site_pv",
host, 1).Result()
if err != nil {
log.Error(err.Error())
return
}
site_pv = strconv.FormatInt(spv, 10)
}()
// 计算 page_pv
go func() {
defer wg.Done()
ppv, err := RedisServer.HIncrBy(context.Background(),
config.Redis.Prefix+"page_pv:"+host,
path, 1).Result()
if err != nil {
log.Error(err.Error())
return
}
page_pv = strconv.FormatInt(ppv, 10)
}()
wg.Wait()
c.Writer.WriteString(`try{` + jsonpCallback + `({"site_uv":` + site_uv + `,"page_pv":` + page_pv + `,"version":2.4,"site_pv":` + site_pv + `})}catch(e){}`)
})
log.Info("服务启动,监听" + config.Listening)
err := server.Run(config.Listening)
if err != nil {
log.Error(err.Error())
panic(err)
}
}