@@ -3,11 +3,13 @@ package di
33import (
44 "context"
55 "fmt"
6+ "net/http"
67 "os"
78 "strconv"
89 "time"
910
1011 lemonsqueezy "github.com/NdoleStudio/lemonsqueezy-go"
12+ "github.com/hashicorp/go-retryablehttp"
1113
1214 "github.com/jinzhu/now"
1315
@@ -96,6 +98,9 @@ func NewContainer(projectID string) (container *Container) {
9698 container .RegisterBillingRoutes ()
9799 container .RegisterBillingListeners ()
98100
101+ container .RegisterWebhookRoutes ()
102+ container .RegisterWebhookListeners ()
103+
99104 container .RegisterLemonsqueezyRoutes ()
100105
101106 // this has to be last since it registers the /* route
@@ -340,6 +345,17 @@ func (container *Container) BillingHandler() (h *handlers.BillingHandler) {
340345 )
341346}
342347
348+ // WebhookHandler creates a new instance of handlers.WebhookHandler
349+ func (container * Container ) WebhookHandler () (h * handlers.WebhookHandler ) {
350+ container .logger .Debug (fmt .Sprintf ("creating %T" , h ))
351+ return handlers .NewWebhookHandler (
352+ container .Logger (),
353+ container .Tracer (),
354+ container .WebhookService (),
355+ container .WebhookHandlerValidator (),
356+ )
357+ }
358+
343359// HeartbeatHandlerValidator creates a new instance of validators.HeartbeatHandlerValidator
344360func (container * Container ) HeartbeatHandlerValidator () (validator * validators.HeartbeatHandlerValidator ) {
345361 container .logger .Debug (fmt .Sprintf ("creating %T" , validator ))
@@ -358,6 +374,15 @@ func (container *Container) BillingHandlerValidator() (validator *validators.Bil
358374 )
359375}
360376
377+ // WebhookHandlerValidator creates a new instance of validators.WebhookHandlerValidator
378+ func (container * Container ) WebhookHandlerValidator () (validator * validators.WebhookHandlerValidator ) {
379+ container .logger .Debug (fmt .Sprintf ("creating %T" , validator ))
380+ return validators .NewWebhookHandlerValidator (
381+ container .Logger (),
382+ container .Tracer (),
383+ )
384+ }
385+
361386// MessageThreadHandler creates a new instance of handlers.MessageThreadHandler
362387func (container * Container ) MessageThreadHandler () (h * handlers.MessageThreadHandler ) {
363388 container .logger .Debug (fmt .Sprintf ("creating %T" , h ))
@@ -445,6 +470,16 @@ func (container *Container) BillingUsageRepository() (repository repositories.Bi
445470 )
446471}
447472
473+ // WebhookRepository creates a new instance of repositories.WebhookRepository
474+ func (container * Container ) WebhookRepository () (repository repositories.WebhookRepository ) {
475+ container .logger .Debug ("creating GORM repositories.WebhookRepository" )
476+ return repositories .NewGormWebhookRepository (
477+ container .Logger (),
478+ container .Tracer (),
479+ container .DB (),
480+ )
481+ }
482+
448483// PhoneNotificationRepository creates a new instance of repositories.PhoneNotificationRepository
449484func (container * Container ) PhoneNotificationRepository () (repository repositories.PhoneNotificationRepository ) {
450485 container .logger .Debug ("creating GORM repositories.PhoneNotificationRepository" )
@@ -517,6 +552,44 @@ func (container *Container) BillingService() (service *services.BillingService)
517552 )
518553}
519554
555+ // WebhookService creates a new instance of services.WebhookService
556+ func (container * Container ) WebhookService () (service * services.WebhookService ) {
557+ container .logger .Debug (fmt .Sprintf ("creating %T" , service ))
558+ return services .NewWebhookService (
559+ container .Logger (),
560+ container .Tracer (),
561+ container .HTTPClient ("webhook" ),
562+ container .WebhookRepository (),
563+ )
564+ }
565+
566+ // HTTPClient creates a new http.Client
567+ func (container * Container ) HTTPClient (name string ) * http.Client {
568+ container .logger .Debug (fmt .Sprintf ("creating %s %T" , name , http .DefaultClient ))
569+ return & http.Client {
570+ Timeout : 60 * time .Second ,
571+ Transport : container .RetryHTTPRoundTripper (),
572+ }
573+ }
574+
575+ //func (container *Container) HTTPRoundTripper(name string) http.RoundTripper {
576+ // container.logger.Debug(fmt.Sprintf("Debug: initializing %s %T", name, http.DefaultTransport))
577+ // return otelroundtripper.New(
578+ // otelroundtripper.WithName(name),
579+ // otelroundtripper.WithParent(container.RetryHTTPRoundTripper()),
580+ // otelroundtripper.WithMeter(global.Meter(os.Getenv("NAMESPACE"))),
581+ // otelroundtripper.WithAttributes(initializers.InitializeOtelResources(container.Version, container.Namespace).Attributes()...),
582+ // )
583+ //}
584+
585+ // RetryHTTPRoundTripper creates a retryable http.RoundTripper
586+ func (container * Container ) RetryHTTPRoundTripper () http.RoundTripper {
587+ container .logger .Debug (fmt .Sprintf ("initializing retry %T" , http .DefaultTransport ))
588+ retryClient := retryablehttp .NewClient ()
589+ retryClient .Logger = container .Logger ()
590+ return retryClient .StandardClient ().Transport
591+ }
592+
520593// PhoneService creates a new instance of services.PhoneService
521594func (container * Container ) PhoneService () (service * services.PhoneService ) {
522595 container .logger .Debug (fmt .Sprintf ("creating %T" , service ))
@@ -665,7 +738,7 @@ func (container *Container) LemonsqueezyService() (service *services.Lemonsqueez
665738func (container * Container ) LemonsqueezyHandler () (handler * handlers.LemonsqueezyHandler ) {
666739 container .logger .Debug (fmt .Sprintf ("creating %T" , handler ))
667740
668- return handlers .NewLemonsqueezyHandlerHandler (
741+ return handlers .NewLemonsqueezyHandler (
669742 container .Logger (),
670743 container .Tracer (),
671744 container .LemonsqueezyService (),
@@ -687,6 +760,7 @@ func (container *Container) LemonsqueezyHandlerValidator() (validator *validator
687760func (container * Container ) LemonsqueezyClient () (client * lemonsqueezy.Client ) {
688761 container .logger .Debug (fmt .Sprintf ("creating %T" , client ))
689762 return lemonsqueezy .New (
763+ lemonsqueezy .WithHTTPClient (container .HTTPClient ("lemonsqueezy" )),
690764 lemonsqueezy .WithAPIKey (os .Getenv ("LEMONSQUEEZY_API_KEY" )),
691765 lemonsqueezy .WithSigningSecret (os .Getenv ("LEMONSQUEEZY_SIGNING_SECRET" )),
692766 )
@@ -769,6 +843,20 @@ func (container *Container) RegisterBillingListeners() {
769843 }
770844}
771845
846+ // RegisterWebhookListeners registers event listeners for listeners.WebhookListener
847+ func (container * Container ) RegisterWebhookListeners () {
848+ container .logger .Debug (fmt .Sprintf ("registering listeners for %T" , listeners.WebhookListener {}))
849+ _ , routes := listeners .NewWebhookListener (
850+ container .Logger (),
851+ container .Tracer (),
852+ container .WebhookService (),
853+ )
854+
855+ for event , handler := range routes {
856+ container .EventDispatcher ().Subscribe (event , handler )
857+ }
858+ }
859+
772860// MessageService creates a new instance of services.MessageService
773861func (container * Container ) MessageService () (service * services.MessageService ) {
774862 container .logger .Debug (fmt .Sprintf ("creating %T" , service ))
@@ -818,6 +906,12 @@ func (container *Container) RegisterBillingRoutes() {
818906 container .BillingHandler ().RegisterRoutes (container .AuthRouter ())
819907}
820908
909+ // RegisterWebhookRoutes registers routes for the /webhooks prefix
910+ func (container * Container ) RegisterWebhookRoutes () {
911+ container .logger .Debug (fmt .Sprintf ("registering %T routes" , & handlers.WebhookHandler {}))
912+ container .WebhookHandler ().RegisterRoutes (container .App (), container .AuthenticatedMiddleware ())
913+ }
914+
821915// RegisterPhoneRoutes registers routes for the /phone prefix
822916func (container * Container ) RegisterPhoneRoutes () {
823917 container .logger .Debug (fmt .Sprintf ("registering %T routes" , & handlers.PhoneHandler {}))
0 commit comments