/
home.go
205 lines (182 loc) · 6.3 KB
/
home.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Copyright Url-Shortenter Author(https://houseme.github.io/url-shortenter/). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/houseme/url-shortenter.
// Package home
package home
import (
"context"
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/database/gredis"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/gipv4"
"github.com/gogf/gf/v2/net/gtrace"
"github.com/gogf/gf/v2/os/gtime"
"github.com/gogf/gf/v2/util/grand"
"golang.org/x/sync/singleflight"
"google.golang.org/protobuf/types/known/timestamppb"
"google.golang.org/protobuf/types/known/wrapperspb"
"github.com/houseme/url-shortenter/app/front/internal/consts"
"github.com/houseme/url-shortenter/app/front/internal/model"
"github.com/houseme/url-shortenter/app/front/internal/service"
"github.com/houseme/url-shortenter/internal/database/dao"
"github.com/houseme/url-shortenter/internal/database/model/entity"
"github.com/houseme/url-shortenter/internal/protocol"
"github.com/houseme/url-shortenter/utility/cache"
"github.com/houseme/url-shortenter/utility/helper"
)
type sHome struct {
}
var sfg singleflight.Group
func init() {
service.RegisterHome(&sHome{})
}
// ShortDetail short url detail
func (s *sHome) ShortDetail(ctx context.Context, in *model.HomeInput) (out string, err error) {
ctx, span := gtrace.NewSpan(ctx, "tracing-lite-service-home-ShortDetail")
defer span.End()
var (
log = g.Log(helper.Helper().Logger(ctx))
conn gredis.Conn
isSendLog bool
)
log.Debug(ctx, "home-short-detail in:", in)
in.VisitState = consts.VisitState
defer func() {
if isSendLog {
s.NewAccessLog(ctx, in)
}
}()
if conn, err = g.Redis(cache.RedisCache().ShortRequestConn(ctx)).Conn(ctx); err != nil {
err = gerror.Wrap(err, "failed to get redis connection")
return "", err
}
defer func() {
if errs := conn.Close(ctx); errs != nil {
log.Error(ctx, "failed to close redis connection err:", errs)
}
}()
var val *gvar.Var
if val, err = conn.Do(ctx, "GET", in.Short); err != nil {
log.Error(ctx, "home-short-detail get redis value failed err:", err)
}
if !val.IsNil() && !val.IsEmpty() && val.String() != "" {
out = val.String()
log.Debug(ctx, "home-short-detail from redis out:", out)
isSendLog = true
in.VisitState = consts.VisitStateNormal
return
}
// 开始查询数据库 防止缓存击穿
v, err, _ := sfg.Do(in.Short, func() (interface{}, error) {
// query DB
var ent = (*entity.ShortUrls)(nil)
if err = dao.ShortUrls.Ctx(ctx).Scan(&ent, "short_url = ?", in.Short); err != nil {
return nil, err
}
if ent == nil {
log.Debug(ctx, "home-short-detail short url not found")
return nil, nil
}
log.Debug(ctx, "home-short-detail select from db:", ent)
if ent.IsValid != consts.ShortValid {
log.Debug(ctx, "home-short-detail short url ent.IsValid != consts.ShortValid")
in.VisitState = consts.VisitStateInvalid
isSendLog = true
return nil, nil
}
// set cache
if val, err = conn.Do(ctx, "SETEX", in.Short, 86400*2+grand.Intn(2022), ent.DestUrl); err != nil {
log.Error(ctx, "home-short-detail storage.Redis Set failed err:", err)
}
log.Info(ctx, "home-short-detail set redis cache end shortUrl:", in.Short, "destUrl:", ent.DestUrl)
return ent.DestUrl, nil
})
if err != nil {
log.Error(ctx, "home-short-detail query db failed err:", err)
err = gerror.Wrap(err, "failed to query db")
return "", err
}
if v == nil {
return "", nil
}
isSendLog = true
in.VisitState = consts.VisitStateNormal
log.Debug(ctx, "home-short-detail from db out:", v)
out = v.(string)
return
}
// NewAccessLog 创建访问日志
func (s *sHome) NewAccessLog(ctx context.Context, in *model.HomeInput) {
ctx, span := gtrace.NewSpan(ctx, "tracing-service-home-NewAccessLog")
defer span.End()
var (
t = gtime.Now()
serverIP, err = gipv4.GetIntranetIp()
l = entity.AccessLogs{
ShortUrl: in.Short,
AccessTime: t,
AccessDate: t,
YearTime: uint(t.Year()),
MonthTime: uint(t.Month()),
DayTime: uint(t.Day()),
Ip: in.ClientIP,
UserAgent: in.UserAgent,
ShortAll: in.ShortAll,
TraceId: span.SpanContext().TraceID().String(),
VisitState: in.VisitState,
ServerIp: serverIP,
}
log = g.Log(helper.Helper().Logger(ctx))
)
if err != nil {
log.Error(ctx, "home-new-access-log get intranet ip failed err:", err)
l.ServerIp = helper.Helper().GetOutBoundIP(ctx)
}
var val *gvar.Var
if val, err = g.Redis(cache.RedisCache().ShortCacheConn(ctx)).Do(ctx, "LPUSH",
cache.RedisCache().ShortAccessLogQueue(ctx), l); err != nil {
log.Error(ctx, "NewAccessLog err:", err)
}
log.Debug(ctx, "NewAccessLog set redis :", val, " access log:", l)
}
// ShortAll 短链列表
func (s *sHome) ShortAll(ctx context.Context, in *model.HomeInput) (out []entity.ShortUrls, err error) {
ctx, span := gtrace.NewSpan(ctx, "tracing-service-home-ShortAll")
defer span.End()
var (
log = g.Log(helper.Helper().Logger(ctx))
t = gtime.Now()
serverIP string
isSendLog bool
)
log.Debug(ctx, "home-short-all in:", in)
in.VisitState = consts.VisitState
if serverIP, err = gipv4.GetIntranetIp(); err != nil {
log.Error(ctx, "home-short-all get server ip failed err:", err)
return
}
defer func() {
if isSendLog {
s.NewAccessLog(ctx, in)
}
}()
acl := &protocol.AccessLogRequest{
ShortUrl: &wrapperspb.StringValue{Value: in.Short},
ShortAll: &wrapperspb.StringValue{Value: in.ShortAll},
VisitState: &wrapperspb.UInt64Value{Value: uint64(in.VisitState)},
Ip: &wrapperspb.StringValue{Value: in.ClientIP},
ServerIP: &wrapperspb.StringValue{Value: serverIP},
TraceID: &wrapperspb.StringValue{Value: span.SpanContext().TraceID().String()},
UserAgent: &wrapperspb.StringValue{Value: in.UserAgent},
AccessTime: ×tamppb.Timestamp{Seconds: t.Unix(), Nanos: int32(t.Nanosecond())},
AccessDate: ×tamppb.Timestamp{Seconds: t.Unix(), Nanos: int32(t.Nanosecond())},
YearTime: &wrapperspb.UInt64Value{Value: uint64(t.Year())},
MonthTime: &wrapperspb.UInt64Value{Value: uint64(t.Month())},
DayTime: &wrapperspb.UInt64Value{Value: uint64(t.Day())},
}
log.Debug(ctx, "home-short-all acl:", acl)
return
}