-
Notifications
You must be signed in to change notification settings - Fork 61
/
event.go
99 lines (81 loc) · 2.38 KB
/
event.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
// SPDX-License-Identifier: AGPL-3.0-only
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, version 3.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>
package canal
import (
"context"
"os"
"strconv"
"go.uber.org/fx"
"go.uber.org/zap"
"github.com/bangumi/server/internal/cache"
"github.com/bangumi/server/internal/config"
"github.com/bangumi/server/internal/dal"
"github.com/bangumi/server/internal/dal/query"
"github.com/bangumi/server/internal/driver"
"github.com/bangumi/server/internal/metrics"
"github.com/bangumi/server/internal/model"
"github.com/bangumi/server/internal/pkg/errgo"
"github.com/bangumi/server/internal/pkg/logger"
"github.com/bangumi/server/internal/pkg/logger/log"
"github.com/bangumi/server/internal/web/session"
)
type eventHandler struct {
log *zap.Logger
q *query.Query
session session.Manager
dryRun bool
}
func newEventHandler(
q *query.Query,
log *zap.Logger,
session session.Manager,
) *eventHandler {
dryRun, _ := strconv.ParseBool(os.Getenv("DRY_RUN"))
return &eventHandler{
dryRun: dryRun,
session: session,
q: q,
log: log.Named("eventHandler"),
}
}
func getEventHandler() (*eventHandler, error) {
var h *eventHandler
err := fx.New(
logger.FxLogger(),
config.Module,
dal.Module,
// driver and connector
fx.Provide(
driver.NewMysqlConnectionPool, metrics.NewScope,
driver.NewRedisClient, logger.Copy, cache.NewRedisCache,
session.NewMysqlRepo, session.New,
newEventHandler,
),
fx.Populate(&h),
).Err()
if err != nil {
return nil, errgo.Wrap(err, "fx")
}
return h, nil
}
func (e *eventHandler) OnUserPasswordChange(id model.UserID) {
e.log.Info("user change password", log.UserID(id))
if e.dryRun {
e.log.Info("dry-run enabled, skip handler")
return
}
if err := e.session.RevokeUser(context.Background(), id); err != nil {
e.log.Error("failed to revoke user", log.UserID(id), zap.Error(err))
}
}