-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
229 lines (213 loc) · 7.44 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package main
import (
"context"
"fmt"
grpcAuth "github.com/awakari/pub/api/grpc/auth"
"github.com/awakari/pub/api/grpc/events"
grpcLimits "github.com/awakari/pub/api/grpc/limits"
grpcPermits "github.com/awakari/pub/api/grpc/permits"
"github.com/awakari/pub/api/grpc/publisher"
grpcSrcAp "github.com/awakari/pub/api/grpc/source/activitypub"
grpcSrcFeeds "github.com/awakari/pub/api/grpc/source/feeds"
grpcSrcSites "github.com/awakari/pub/api/grpc/source/sites"
grpcSrcTg "github.com/awakari/pub/api/grpc/source/telegram"
"github.com/awakari/pub/api/grpc/tgbot"
auth2 "github.com/awakari/pub/api/http/auth"
v2 "github.com/awakari/pub/api/http/pub"
httpSrc "github.com/awakari/pub/api/http/pub/src"
"github.com/awakari/pub/config"
"github.com/awakari/pub/model"
"github.com/awakari/pub/storage"
"github.com/gin-gonic/gin"
grpcpool "github.com/processout/grpc-go-pool"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"log/slog"
//_ "net/http/pprof"
"os"
)
func main() {
//
slog.Info("starting...")
cfg, err := config.NewConfigFromEnv()
if err != nil {
slog.Error(fmt.Sprintf("failed to load the config: %s", err.Error()))
}
opts := slog.HandlerOptions{
Level: slog.Level(cfg.Log.Level),
}
log := slog.New(slog.NewTextHandler(os.Stdout, &opts))
connPoolEvts, err := grpcpool.New(
func() (*grpc.ClientConn, error) {
return grpc.NewClient(cfg.Api.Events.Uri, grpc.WithTransportCredentials(insecure.NewCredentials()))
},
int(cfg.Api.Events.Connection.Count.Init),
int(cfg.Api.Events.Connection.Count.Max),
cfg.Api.Events.Connection.IdleTimeout,
)
if err != nil {
panic(err)
}
defer connPoolEvts.Close()
clientEvts := events.NewClientPool(connPoolEvts)
svcEvts := events.NewService(clientEvts)
svcEvts = events.NewLoggingMiddleware(svcEvts, log)
err = svcEvts.SetStream(context.TODO(), cfg.Api.Events.Topic, cfg.Api.Events.Limit)
if err != nil {
panic(err)
}
// init the source-feeds client
connSrcFeeds, err := grpc.NewClient(cfg.Api.Source.Feeds.Uri, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err == nil {
log.Info("connected the source-feeds service")
defer connSrcFeeds.Close()
} else {
log.Error(fmt.Sprintf("failed to connect the source-feeds service: %s", err))
}
clientSrcFeeds := grpcSrcFeeds.NewServiceClient(connSrcFeeds)
svcSrcFeeds := grpcSrcFeeds.NewService(clientSrcFeeds)
svcSrcFeeds = grpcSrcFeeds.NewServiceLogging(svcSrcFeeds, log)
// init the source-telegram client
connSrcTg, err := grpc.NewClient(cfg.Api.Source.Telegram.Uri, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err == nil {
log.Info("connected the source-telegram service")
defer connSrcTg.Close()
} else {
log.Error(fmt.Sprintf("failed to connect the source-telegram service: %s", err))
}
clientSrcTg := grpcSrcTg.NewServiceClient(connSrcTg)
svcSrcTg := grpcSrcTg.NewService(clientSrcTg, cfg.Api.Source.Telegram.FmtUriReplica)
svcSrcTg = grpcSrcTg.NewServiceLogging(svcSrcTg, log)
// init the source-sites client
connSrcSites, err := grpc.NewClient(cfg.Api.Source.Sites.Uri, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err == nil {
log.Info("connected the source-sites service")
defer connSrcSites.Close()
} else {
log.Error(fmt.Sprintf("failed to connect the source-sites service: %s", err))
}
clientSrcSites := grpcSrcSites.NewServiceClient(connSrcSites)
svcSrcSites := grpcSrcSites.NewService(clientSrcSites)
svcSrcSites = grpcSrcSites.NewServiceLogging(svcSrcSites, log)
// init the int-activitypub client
connSrcAp, err := grpc.NewClient(cfg.Api.Source.ActivityPub.Uri, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err == nil {
log.Info("connected the int-activitypub service")
defer connSrcAp.Close()
} else {
log.Error(fmt.Sprintf("failed to connect the int-activitypub service: %s", err))
}
clientSrcAp := grpcSrcAp.NewServiceClient(connSrcAp)
svcSrcAp := grpcSrcAp.NewService(clientSrcAp)
svcSrcAp = grpcSrcAp.NewLogging(svcSrcAp, log)
connTgBot, err := grpc.NewClient(cfg.Api.TgBot.Uri, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err == nil {
log.Info("connected the the bot-telegram service")
} else {
log.Warn(fmt.Sprintf("failed to connect the bot-telegram: %s", err))
}
var clientTgBot tgbot.ServiceClient
if connTgBot != nil {
clientTgBot = tgbot.NewServiceClient(connTgBot)
}
var svcTgBot tgbot.Service
if clientTgBot != nil {
svcTgBot = tgbot.NewService(clientTgBot)
svcTgBot = tgbot.NewServiceLogging(svcTgBot, log)
}
connPoolLimits, err := grpcpool.New(
func() (*grpc.ClientConn, error) {
return grpc.NewClient(cfg.Api.Usage.Uri, grpc.WithTransportCredentials(insecure.NewCredentials()))
},
int(cfg.Api.Usage.Connection.Count.Init),
int(cfg.Api.Usage.Connection.Count.Max),
cfg.Api.Usage.Connection.IdleTimeout,
)
if err != nil {
panic(err)
}
defer connPoolLimits.Close()
clientLimits := grpcLimits.NewClientPool(connPoolLimits)
svcLimits := grpcLimits.NewService(clientLimits)
svcLimits = grpcLimits.NewServiceLogging(svcLimits, log)
connPoolPermits, err := grpcpool.New(
func() (*grpc.ClientConn, error) {
return grpc.NewClient(cfg.Api.Usage.Uri, grpc.WithTransportCredentials(insecure.NewCredentials()))
},
int(cfg.Api.Usage.Connection.Count.Init),
int(cfg.Api.Usage.Connection.Count.Max),
cfg.Api.Usage.Connection.IdleTimeout,
)
if err != nil {
panic(err)
}
defer connPoolLimits.Close()
clientPermits := grpcPermits.NewClientPool(connPoolPermits)
svcPermits := grpcPermits.NewService(clientPermits)
svcPermits = grpcPermits.NewServiceLogging(svcPermits, log)
// init blacklist
var stor storage.Blacklist
stor, err = storage.NewBlacklist(context.TODO(), cfg.Db)
if err != nil {
panic(fmt.Sprintf("failed to initialize the blacklist storage: %s", err))
}
var cursor string
var page []model.BlacklistEntry
blacklist := model.NewPrefixes[model.BlacklistValue]()
for {
page, err = stor.GetPage(context.TODO(), 100, cursor)
if err != nil {
panic(err)
}
if len(page) == 0 {
break
}
cursor = page[len(page)-1].Prefix
for _, e := range page {
_ = blacklist.Put(context.TODO(), e.Prefix, e.Value)
}
}
stor.Close()
log.Info("loaded the blacklist")
handlerPub := v2.NewHandler(
publisher.NewService(clientEvts, svcPermits, cfg.Api.Events),
cfg.Api.Writer.Internal,
blacklist,
log,
)
handlerSrc := httpSrc.NewHandler(svcSrcFeeds, svcSrcSites, svcSrcTg, svcSrcAp, svcTgBot, svcLimits, svcPermits)
connAuth, err := grpc.NewClient(cfg.Api.Auth.Uri, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
panic(err)
}
clientAuth := grpcAuth.NewServiceClient(connAuth)
svcAuth := grpcAuth.NewService(clientAuth)
svcAuth = grpcAuth.NewLogging(svcAuth, log)
handlerAuth := auth2.Handler{
Svc: svcAuth,
}
authSrcTg := auth2.NewTelegramValidator(svcSrcTg)
// expose the profiling
//go func() {
// _ = http.ListenAndServe("localhost:6060", nil)
//}()
r := gin.Default()
r.
Group("/v1/src/:type").
POST("", handlerAuth.Authorize, handlerSrc.Create).
GET("", handlerAuth.Authorize, handlerSrc.Read).
DELETE("", handlerAuth.Authorize, handlerSrc.Delete).
GET("/list", handlerAuth.Authorize, handlerSrc.List)
r.
Group("/v1/tg", handlerAuth.Authorize).
POST("", authSrcTg.ClientLogin)
r.
Group("/v1", handlerAuth.Authorize).
POST("", handlerPub.Write).
POST("/batch", handlerPub.WriteBatch).
POST("/internal", handlerPub.WriteInternal)
err = r.Run(fmt.Sprintf(":%d", cfg.Api.Http.Port))
if err != nil {
panic(err)
}
}