-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
173 lines (157 loc) · 5.63 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
package main
import (
"fmt"
apiGrpc "github.com/awakari/metrics/api/grpc"
apiGrpcInterests "github.com/awakari/metrics/api/grpc/interests"
apiGrpcLimits "github.com/awakari/metrics/api/grpc/limits"
apiGrpcSrcAp "github.com/awakari/metrics/api/grpc/source/activitypub"
apiGrpcSrcFeeds "github.com/awakari/metrics/api/grpc/source/feeds"
apiGrpcSrcSites "github.com/awakari/metrics/api/grpc/source/sites"
apiGrpcSrcTg "github.com/awakari/metrics/api/grpc/source/telegram"
apiHttp "github.com/awakari/metrics/api/http"
"github.com/awakari/metrics/config"
"github.com/awakari/metrics/service"
"github.com/gin-gonic/gin"
grpcpool "github.com/processout/grpc-go-pool"
apiProm "github.com/prometheus/client_golang/api"
apiPromV1 "github.com/prometheus/client_golang/api/prometheus/v1"
"github.com/prometheus/client_golang/prometheus/promhttp"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"log/slog"
"net/http"
"os"
)
func main() {
slog.Info("starting...")
cfg, err := config.NewConfigFromEnv()
if err != nil {
panic(err)
}
opts := slog.HandlerOptions{
Level: slog.Level(cfg.Log.Level),
}
log := slog.New(slog.NewTextHandler(os.Stdout, &opts))
clientProm, err := apiProm.NewClient(apiProm.Config{
Address: cfg.Api.Prometheus.Uri,
})
var ap apiPromV1.API
switch err {
case nil:
ap = apiPromV1.NewAPI(clientProm)
default:
panic(err)
}
svc := service.NewService(ap)
svc = service.NewLogging(svc, log)
connPoolInterests, err := grpcpool.New(
func() (*grpc.ClientConn, error) {
return grpc.NewClient(cfg.Api.Interests.Uri, grpc.WithTransportCredentials(insecure.NewCredentials()))
},
int(cfg.Api.Interests.Connection.Count.Init),
int(cfg.Api.Interests.Connection.Count.Max),
cfg.Api.Interests.Connection.IdleTimeout,
)
if err != nil {
panic(err)
}
defer connPoolInterests.Close()
clientInterests := apiGrpcInterests.NewClientPool(connPoolInterests)
clientInterests = apiGrpcInterests.NewClientLogging(clientInterests, log)
// 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 := apiGrpcSrcFeeds.NewServiceClient(connSrcFeeds)
svcSrcFeeds := apiGrpcSrcFeeds.NewService(clientSrcFeeds)
svcSrcFeeds = apiGrpcSrcFeeds.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 := apiGrpcSrcTg.NewServiceClient(connSrcTg)
svcSrcTg := apiGrpcSrcTg.NewService(clientSrcTg)
svcSrcTg = apiGrpcSrcTg.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 := apiGrpcSrcSites.NewServiceClient(connSrcSites)
svcSrcSites := apiGrpcSrcSites.NewService(clientSrcSites)
svcSrcSites = apiGrpcSrcSites.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 := apiGrpcSrcAp.NewServiceClient(connSrcAp)
svcSrcAp := apiGrpcSrcAp.NewService(clientSrcAp)
svcSrcAp = apiGrpcSrcAp.NewLogging(svcSrcAp, 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 := apiGrpcLimits.NewClientPool(connPoolLimits)
svcLimits := apiGrpcLimits.NewService(clientLimits)
svcLimits = apiGrpcLimits.NewServiceLogging(svcLimits, log)
handlerCookies := apiHttp.NewCookieHandler(cfg.Api.Http.Cookie)
r := gin.Default()
handlerStatus := apiHttp.NewHandler(svc, clientInterests, cfg.Limits.Default.Groups)
r.
Group("/v1/public", handlerCookies.Handle).
GET("/pub-rate", handlerStatus.GetPublishRate).
GET("/read", handlerStatus.GetReadStatus).
GET("/followers", handlerStatus.GetFollowersCount).
GET("/top-interests", handlerStatus.GetTopInterests).
GET("/duration", handlerStatus.GetCoreDuration)
r.
Group("/v1/attr", handlerCookies.Handle).
GET("/types", handlerStatus.GetEventAttributeTypes).
GET("/values/:name", handlerStatus.GetEventAttributeValuesByName)
go func() {
err = r.Run(fmt.Sprintf(":%d", cfg.Api.Http.Port))
if err != nil {
panic(err)
}
}()
go func() {
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(fmt.Sprintf(":%d", cfg.Api.Metrics.Port), nil)
}()
log.Info(fmt.Sprintf("starting to listen the API @ port #%d...", cfg.Api.Port))
err = apiGrpc.Serve(
cfg,
svcLimits,
svc,
svcSrcFeeds,
svcSrcSites,
svcSrcTg,
svcSrcAp,
)
if err != nil {
panic(err)
}
}