mirror of
https://github.com/PostHog/posthog.git
synced 2024-11-21 21:49:51 +01:00
59eaa99c14
* 🔥 initial commit * update readme * Update README.md * Update README.md * deploy scripts * very basic consumer setup * add some configs and docker-compose * formatting for testing * add tailscale * flip from dev to prod flag * set default to be not prod * default for group_id * tailscale up * update gitignore * basic geolocation * remove unused localServer * document mmdb * just make configs an example * drop raw print * add a start script (downloads the mmdb) * add readme and update configs.example * ts working * if in start * update start script * fix start * fix start * fix more * add sql endpoints for tokenId and Person lookups * work towards filter * sub channel * fix subChan * hardcode team2 token * add cors * only allow get and head * add atomicbool * add channel to kafka * add logs * verbose logs * make array * drop sub ptrs * more logs * helps to loop * drop some logigng * move sub branch * logging * drop log * hog * Deal with numeric distinct ids later * logs * api_key * send 1/1000 * remove log * remove more logs * change response payload * set timestamp if needed * fill in person_id if team_id is set * require teamid, convert to token * clean up subs on disconnect * log * check for token in another place * clean up subs on disconnect * drop modulo and log * fix no assign * don't reuse db conn for now * drop a log * add back commented out log * Don't block on send to client channel * add geo bool * only geo events * use wrapper ip * don't require team in geo mode * add an endpoint and stats keeper for teams * remove stats keeper * start stats keeper * wire it up * change the shape of the response * omit empty error * omit empty on the stats as well * enable logging on back pressure * add jwt endpoint for testing * support multiple event types * Get Auth Setup * jwt team is float so turn that into int * logs * add auth for stats endpoint * remove tailscale and use autoTLS on public endpoints * default to :443 for auto tls * remove un-needed endpoints and handlers * Use compression because... a lot of data (#9) * add dockerfile and CI/CD (#10) * add dockerfile and CI/CD * Use ubuntu not alpine couldn't build in alpine :'( * Add MMDB download to Dockerfile (#11) * Use clearer name for MMDB * Don't connect to Kafka over SSL in dev * Fix JWT token in example config * Add postgres.url to example config * Add expected scope * Fix const syntax * Put scope validation where claims are known * Fix audience validation * moves * ignore livestream for ci * main -> master * move GA to root * docker lint fix * fix typo * fixes for docker builds * test docker build * livestream build docker * dang * Update .github/workflows/livestream-docker-image.yml Co-authored-by: Neil Kakkar <neilkakkar@gmail.com> * Update .github/workflows/livestream-docker-image.yml Co-authored-by: Neil Kakkar <neilkakkar@gmail.com> * don't build posthog container when PR is pushed for rust or livestream * Update .github/workflows/livestream-docker-image.yml Co-authored-by: Neil Kakkar <neilkakkar@gmail.com> * add a lot of paths-ignore * Update .github/workflows/livestream-docker-image.yml Co-authored-by: Neil Kakkar <neilkakkar@gmail.com> * Dorny filters are handling most of what I was trying to do * remove tailscale to speed up builds * maybe? * push container to github.com/posthog/postog * don't build container on PR * remove more filters because dorny --------- Co-authored-by: Brett Hoerner <brett@bretthoerner.com> Co-authored-by: Zach Waterfield <zlwaterfield@gmail.com> Co-authored-by: Frank Hamand <frankhamand@gmail.com> Co-authored-by: Michael Matloka <michal@matloka.com> Co-authored-by: Neil Kakkar <neilkakkar@gmail.com>
273 lines
6.0 KiB
Go
273 lines
6.0 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"log"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
"github.com/hashicorp/golang-lru/v2/expirable"
|
|
"github.com/labstack/echo/v4"
|
|
"github.com/labstack/echo/v4/middleware"
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
func main() {
|
|
loadConfigs()
|
|
|
|
isProd := viper.GetBool("prod")
|
|
|
|
mmdb := viper.GetString("mmdb.path")
|
|
if mmdb == "" {
|
|
log.Fatal("mmdb.path must be set")
|
|
}
|
|
|
|
geolocator, err := NewGeoLocator(mmdb)
|
|
if err != nil {
|
|
log.Fatalf("Failed to open MMDB: %v", err)
|
|
}
|
|
|
|
brokers := viper.GetString("kafka.brokers")
|
|
if brokers == "" {
|
|
log.Fatal("kafka.brokers must be set")
|
|
}
|
|
|
|
topic := viper.GetString("kafka.topic")
|
|
if topic == "" {
|
|
log.Fatal("kafka.topic must be set")
|
|
}
|
|
|
|
groupID := viper.GetString("kafka.group_id")
|
|
|
|
teamStats := &TeamStats{
|
|
Store: make(map[string]*expirable.LRU[string, string]),
|
|
}
|
|
|
|
phEventChan := make(chan PostHogEvent)
|
|
statsChan := make(chan PostHogEvent)
|
|
subChan := make(chan Subscription)
|
|
unSubChan := make(chan Subscription)
|
|
|
|
go teamStats.keepStats(statsChan)
|
|
|
|
kafkaSecurityProtocol := "SSL"
|
|
if !isProd {
|
|
kafkaSecurityProtocol = "PLAINTEXT"
|
|
}
|
|
consumer, err := NewKafkaConsumer(brokers, kafkaSecurityProtocol, groupID, topic, geolocator, phEventChan, statsChan)
|
|
if err != nil {
|
|
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)
|
|
|
|
e.GET("/stats", func(c echo.Context) error {
|
|
|
|
type stats struct {
|
|
UsersOnProduct int `json:"users_on_product,omitempty"`
|
|
Error string `json:"error,omitempty"`
|
|
}
|
|
|
|
authHeader := c.Request().Header.Get("Authorization")
|
|
if authHeader == "" {
|
|
return errors.New("authorization header is required")
|
|
}
|
|
|
|
claims, err := decodeAuthToken(authHeader)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
teamIdInt := int(claims["team_id"].(float64))
|
|
|
|
token, err := tokenFromTeamId(teamIdInt)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
var hash *expirable.LRU[string, string]
|
|
var ok bool
|
|
if hash, ok = teamStats.Store[token]; !ok {
|
|
resp := stats{
|
|
Error: "no stats",
|
|
}
|
|
return c.JSON(http.StatusOK, resp)
|
|
}
|
|
|
|
siteStats := stats{
|
|
UsersOnProduct: hash.Len(),
|
|
}
|
|
return c.JSON(http.StatusOK, siteStats)
|
|
})
|
|
|
|
e.GET("/events", func(c echo.Context) error {
|
|
e.Logger.Printf("SSE client connected, ip: %v", c.RealIP())
|
|
|
|
teamId := c.QueryParam("teamId")
|
|
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 = ""
|
|
|
|
log.Println("~~~~ Looking for auth header")
|
|
authHeader := c.Request().Header.Get("Authorization")
|
|
if authHeader == "" {
|
|
return errors.New("authorization header is required")
|
|
}
|
|
|
|
log.Println("~~~~ decoding auth header")
|
|
claims, err := decodeAuthToken(authHeader)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
teamId = strconv.Itoa(int(claims["team_id"].(float64)))
|
|
|
|
log.Printf("~~~~ team found %s", teamId)
|
|
if teamId == "" {
|
|
return errors.New("teamId is required unless geo=true")
|
|
}
|
|
}
|
|
|
|
if teamId != "" {
|
|
teamIdInt64, err := strconv.ParseInt(teamId, 10, 0)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
teamIdInt := int(teamIdInt64)
|
|
token, err = tokenFromTeamId(teamIdInt)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
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 {
|
|
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()
|
|
}
|
|
}
|
|
})
|
|
|
|
if !isProd {
|
|
e.Logger.Fatal(e.Start(":8080"))
|
|
} else {
|
|
e.Logger.Fatal(e.StartAutoTLS(":443"))
|
|
}
|
|
}
|