@@ -30,6 +30,7 @@ import (
3030 "github.com/database-playground/backend-v2/internal/workers"
3131 "github.com/gin-contrib/cors"
3232 "github.com/gin-gonic/gin"
33+ "github.com/posthog/posthog-go"
3334 "github.com/redis/rueidis"
3435 "github.com/vektah/gqlparser/v2/ast"
3536 "go.uber.org/fx"
@@ -50,6 +51,31 @@ func ApqCache(redisClient rueidis.Client) graphql.Cache[string] {
5051 return apq .NewCache (redisClient , 24 * time .Hour )
5152}
5253
54+ func PostHogClient (lifecycle fx.Lifecycle , cfg config.Config ) (posthog.Client , error ) {
55+ if cfg .PostHog .APIKey == nil || cfg .PostHog .Host == nil {
56+ slog .Warn ("PostHog client is not initialized, because you did not configure a PostHog API key and a host." )
57+ return nil , nil
58+ }
59+
60+ client , err := posthog .NewWithConfig (
61+ * cfg .PostHog .APIKey ,
62+ posthog.Config {
63+ Endpoint : * cfg .PostHog .Host ,
64+ },
65+ )
66+ if err != nil {
67+ return nil , err
68+ }
69+
70+ lifecycle .Append (fx .StopHook (func () {
71+ if err := client .Close (); err != nil {
72+ slog .Info ("failed to close PostHog client" , "error" , err )
73+ }
74+ }))
75+
76+ return client , nil
77+ }
78+
5379// GqlgenHandler creates a gqlgen handler.
5480func GqlgenHandler (
5581 entClient * ent.Client ,
@@ -85,8 +111,8 @@ func UserAccountContext(entClient *ent.Client, storage auth.Storage, eventServic
85111}
86112
87113// EventService creates an events.EventService.
88- func EventService (entClient * ent.Client ) * events.EventService {
89- return events .NewEventService (entClient )
114+ func EventService (entClient * ent.Client , posthogClient posthog. Client ) * events.EventService {
115+ return events .NewEventService (entClient , posthogClient )
90116}
91117
92118// SubmissionService creates a submission.SubmissionService.
0 commit comments