2024-06-18 17:38:53 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2024-07-10 20:58:52 +02:00
|
|
|
"fmt"
|
2024-06-18 17:38:53 +02:00
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
2024-06-19 23:23:55 +02:00
|
|
|
"github.com/getsentry/sentry-go"
|
2024-06-18 17:38:53 +02:00
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
"github.com/labstack/echo/v4/middleware"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
loadConfigs()
|
|
|
|
|
|
|
|
isProd := viper.GetBool("prod")
|
|
|
|
|
2024-06-19 23:23:55 +02:00
|
|
|
err := sentry.Init(sentry.ClientOptions{
|
|
|
|
Dsn: viper.GetString("sentry.dsn"),
|
|
|
|
Debug: isProd,
|
|
|
|
AttachStacktrace: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
sentry.CaptureException(err)
|
|
|
|
log.Fatalf("sentry.Init: %s", err)
|
|
|
|
}
|
|
|
|
// Flush buffered events before the program terminates.
|
|
|
|
// Set the timeout to the maximum duration the program can afford to wait.
|
|
|
|
defer sentry.Flush(2 * time.Second)
|
|
|
|
|
2024-06-18 17:38:53 +02:00
|
|
|
mmdb := viper.GetString("mmdb.path")
|
|
|
|
if mmdb == "" {
|
2024-06-19 23:23:55 +02:00
|
|
|
sentry.CaptureException(errors.New("mmdb.path must be set"))
|
2024-06-18 17:38:53 +02:00
|
|
|
log.Fatal("mmdb.path must be set")
|
|
|
|
}
|
|
|
|
brokers := viper.GetString("kafka.brokers")
|
|
|
|
if brokers == "" {
|
2024-06-19 23:23:55 +02:00
|
|
|
sentry.CaptureException(errors.New("kafka.brokers must be set"))
|
2024-06-18 17:38:53 +02:00
|
|
|
log.Fatal("kafka.brokers must be set")
|
|
|
|
}
|
|
|
|
topic := viper.GetString("kafka.topic")
|
|
|
|
if topic == "" {
|
2024-06-19 23:23:55 +02:00
|
|
|
sentry.CaptureException(errors.New("kafka.topic must be set"))
|
2024-06-18 17:38:53 +02:00
|
|
|
log.Fatal("kafka.topic must be set")
|
|
|
|
}
|
|
|
|
groupID := viper.GetString("kafka.group_id")
|
2024-06-19 23:23:55 +02:00
|
|
|
if groupID == "" {
|
|
|
|
sentry.CaptureException(errors.New("kafka.group_id must be set"))
|
|
|
|
log.Fatal("kafka.group_id must be set")
|
|
|
|
}
|
|
|
|
|
2024-08-07 00:08:34 +02:00
|
|
|
geolocator, err := NewMaxMindGeoLocator(mmdb)
|
2024-06-19 23:23:55 +02:00
|
|
|
if err != nil {
|
|
|
|
sentry.CaptureException(err)
|
|
|
|
log.Fatalf("Failed to open MMDB: %v", err)
|
|
|
|
}
|
2024-06-18 17:38:53 +02:00
|
|
|
|
2024-08-14 23:28:07 +02:00
|
|
|
stats := newStatsKeeper()
|
2024-06-18 17:38:53 +02:00
|
|
|
|
|
|
|
phEventChan := make(chan PostHogEvent)
|
|
|
|
statsChan := make(chan PostHogEvent)
|
|
|
|
subChan := make(chan Subscription)
|
|
|
|
unSubChan := make(chan Subscription)
|
|
|
|
|
2024-08-14 23:28:07 +02:00
|
|
|
go stats.keepStats(statsChan)
|
2024-06-18 17:38:53 +02:00
|
|
|
|
|
|
|
kafkaSecurityProtocol := "SSL"
|
|
|
|
if !isProd {
|
|
|
|
kafkaSecurityProtocol = "PLAINTEXT"
|
|
|
|
}
|
2024-08-07 00:08:34 +02:00
|
|
|
consumer, err := NewPostHogKafkaConsumer(brokers, kafkaSecurityProtocol, groupID, topic, geolocator, phEventChan, statsChan)
|
2024-06-18 17:38:53 +02:00
|
|
|
if err != nil {
|
2024-06-19 23:23:55 +02:00
|
|
|
sentry.CaptureException(err)
|
2024-06-18 17:38:53 +02:00
|
|
|
log.Fatalf("Failed to create Kafka consumer: %v", err)
|
|
|
|
}
|
|
|
|
defer consumer.Close()
|
|
|
|
go consumer.Consume()
|
|
|
|
|
|
|
|
filter := NewFilter(subChan, unSubChan, phEventChan)
|
|
|
|
go filter.Run()
|
|
|
|
|
|
|
|
// Echo instance
|
|
|
|
e := echo.New()
|
|
|
|
|
|
|
|
// Middleware
|
|
|
|
e.Use(middleware.Logger())
|
|
|
|
e.Use(middleware.Recover())
|
|
|
|
e.Use(middleware.RequestID())
|
|
|
|
e.Use(middleware.GzipWithConfig(middleware.GzipConfig{
|
|
|
|
Level: 9, // Set compression level to maximum
|
|
|
|
}))
|
|
|
|
|
|
|
|
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
|
|
|
|
AllowOrigins: []string{"*"},
|
|
|
|
AllowMethods: []string{http.MethodGet, http.MethodHead},
|
|
|
|
}))
|
|
|
|
e.File("/", "./index.html")
|
|
|
|
|
|
|
|
// Routes
|
|
|
|
e.GET("/", index)
|
|
|
|
|
2024-08-14 23:28:07 +02:00
|
|
|
e.GET("/served", servedHandler(stats))
|
2024-06-18 17:38:53 +02:00
|
|
|
|
2024-08-14 23:28:07 +02:00
|
|
|
e.GET("/stats", statsHandler(stats))
|
2024-06-18 17:38:53 +02:00
|
|
|
|
|
|
|
e.GET("/events", func(c echo.Context) error {
|
|
|
|
e.Logger.Printf("SSE client connected, ip: %v", c.RealIP())
|
|
|
|
|
2024-08-14 23:28:07 +02:00
|
|
|
var teamId string
|
2024-06-18 17:38:53 +02:00
|
|
|
eventType := c.QueryParam("eventType")
|
|
|
|
distinctId := c.QueryParam("distinctId")
|
|
|
|
geo := c.QueryParam("geo")
|
|
|
|
|
|
|
|
teamIdInt := 0
|
|
|
|
token := ""
|
|
|
|
geoOnly := false
|
|
|
|
|
|
|
|
if strings.ToLower(geo) == "true" || geo == "1" {
|
|
|
|
geoOnly = true
|
|
|
|
} else {
|
|
|
|
teamId = ""
|
|
|
|
|
|
|
|
authHeader := c.Request().Header.Get("Authorization")
|
|
|
|
if authHeader == "" {
|
|
|
|
return errors.New("authorization header is required")
|
|
|
|
}
|
|
|
|
|
|
|
|
claims, err := decodeAuthToken(authHeader)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
teamId = strconv.Itoa(int(claims["team_id"].(float64)))
|
2024-07-10 20:58:52 +02:00
|
|
|
token = fmt.Sprint(claims["api_token"])
|
2024-06-18 17:38:53 +02:00
|
|
|
|
|
|
|
if teamId == "" {
|
|
|
|
return errors.New("teamId is required unless geo=true")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
eventTypes := []string{}
|
|
|
|
if eventType != "" {
|
|
|
|
eventTypes = strings.Split(eventType, ",")
|
|
|
|
}
|
|
|
|
|
|
|
|
subscription := Subscription{
|
|
|
|
TeamId: teamIdInt,
|
|
|
|
Token: token,
|
|
|
|
ClientId: c.Response().Header().Get(echo.HeaderXRequestID),
|
|
|
|
DistinctId: distinctId,
|
|
|
|
Geo: geoOnly,
|
|
|
|
EventTypes: eventTypes,
|
|
|
|
EventChan: make(chan interface{}, 100),
|
|
|
|
ShouldClose: &atomic.Bool{},
|
|
|
|
}
|
|
|
|
|
|
|
|
subChan <- subscription
|
|
|
|
|
|
|
|
w := c.Response()
|
|
|
|
w.Header().Set("Content-Type", "text/event-stream")
|
|
|
|
w.Header().Set("Cache-Control", "no-cache")
|
|
|
|
w.Header().Set("Connection", "keep-alive")
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-c.Request().Context().Done():
|
|
|
|
e.Logger.Printf("SSE client disconnected, ip: %v", c.RealIP())
|
|
|
|
filter.unSubChan <- subscription
|
|
|
|
subscription.ShouldClose.Store(true)
|
|
|
|
return nil
|
|
|
|
case payload := <-subscription.EventChan:
|
|
|
|
jsonData, err := json.Marshal(payload)
|
|
|
|
if err != nil {
|
2024-06-19 23:23:55 +02:00
|
|
|
sentry.CaptureException(err)
|
2024-06-18 17:38:53 +02:00
|
|
|
log.Println("Error marshalling payload", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
event := Event{
|
|
|
|
Data: jsonData,
|
|
|
|
}
|
|
|
|
if err := event.WriteTo(w); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
w.Flush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
e.GET("/jwt", func(c echo.Context) error {
|
|
|
|
authHeader := c.Request().Header.Get("Authorization")
|
|
|
|
if authHeader == "" {
|
|
|
|
return errors.New("authorization header is required")
|
|
|
|
}
|
|
|
|
|
|
|
|
claims, err := decodeAuthToken(authHeader)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(http.StatusOK, claims)
|
|
|
|
})
|
|
|
|
|
|
|
|
e.GET("/sse", func(c echo.Context) error {
|
|
|
|
e.Logger.Printf("Map client connected, ip: %v", c.RealIP())
|
|
|
|
|
|
|
|
w := c.Response()
|
|
|
|
w.Header().Set("Content-Type", "text/event-stream")
|
|
|
|
w.Header().Set("Cache-Control", "no-cache")
|
|
|
|
w.Header().Set("Connection", "keep-alive")
|
|
|
|
|
|
|
|
ticker := time.NewTicker(1 * time.Second)
|
|
|
|
defer ticker.Stop()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-c.Request().Context().Done():
|
|
|
|
e.Logger.Printf("SSE client disconnected, ip: %v", c.RealIP())
|
|
|
|
return nil
|
|
|
|
case <-ticker.C:
|
|
|
|
event := Event{
|
|
|
|
Data: []byte("ping: " + time.Now().Format(time.RFC3339Nano)),
|
|
|
|
}
|
|
|
|
if err := event.WriteTo(w); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
w.Flush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2024-06-19 14:25:55 +02:00
|
|
|
e.Logger.Fatal(e.Start(":8080"))
|
2024-06-18 17:38:53 +02:00
|
|
|
}
|