From 898ad74079a6802182ead03625e6568de0bebad1 Mon Sep 17 00:00:00 2001 From: KoCoder Date: Thu, 19 Feb 2026 09:16:45 +0100 Subject: [PATCH] Simplify main.go --- cmd/api/main.go | 88 +-------------------- cmd/cronjobs/imap/main.go | 19 +++++ core/main.go | 100 ++++++++++++++++++++++++ integration/mailing/imap_integration.go | 12 ++- 4 files changed, 132 insertions(+), 87 deletions(-) create mode 100644 cmd/cronjobs/imap/main.go create mode 100644 core/main.go diff --git a/cmd/api/main.go b/cmd/api/main.go index 51ac027..5872a7f 100644 --- a/cmd/api/main.go +++ b/cmd/api/main.go @@ -1,94 +1,12 @@ package main import ( - "fmt" "net/http" - kfx "git.kocoder.xyz/kocoded/vt/fx" - "git.kocoder.xyz/kocoded/vt/fx/interfaces" - "git.kocoder.xyz/kocoded/vt/fx/interfaces/stores" - "git.kocoder.xyz/kocoded/vt/integration" - "git.kocoder.xyz/kocoded/vt/integration/mailing" - "git.kocoder.xyz/kocoded/vt/interceptors" - "git.kocoder.xyz/kocoded/vt/repositories" - mandantv1 "git.kocoder.xyz/kocoded/vt/routers/mandant/v1" - messagebusv1 "git.kocoder.xyz/kocoded/vt/routers/messagebus/v1" - projectv1 "git.kocoder.xyz/kocoded/vt/routers/project/v1" - todov1 "git.kocoder.xyz/kocoded/vt/routers/todo/v1" - "git.kocoder.xyz/kocoded/vt/utils" - "github.com/joho/godotenv" - "go.uber.org/fx" + "git.kocoder.xyz/kocoded/vt/core" + "git.kocoder.xyz/kocoded/vt/fx" ) -func AsIntegration(f any) any { - return fx.Annotate( - f, - fx.As(new(interfaces.BasicIntegration)), - fx.ResultTags(`group:"integration"`), - ) -} - -func AsRoute(f any) any { - return fx.Annotate( - f, - fx.As(new(kfx.Handler)), - fx.ResultTags(`group:"connectRoute"`), - ) -} - func main() { - err := godotenv.Load() - - if err != nil { - fmt.Println("Error loading env vars... %v", err) - } - - fx.New( - fx.Provide( - kfx.NewHTTPServer, - utils.NewReflector, - utils.NewHealthchecker, - - utils.NewDB, - utils.NewCache, - - utils.NewOIDCProvider, - utils.NewOIDCVerifier, - - fx.Annotate(repositories.NewSessionRepository, fx.As(new(stores.SessionStore))), - fx.Annotate(repositories.NewInMemeoryMessageRepository, fx.As(new(stores.MessageStore))), - interceptors.NewOIDCInterceptor, - - AsIntegration(mailing.NewImapIntegration), - fx.Annotate( - integration.NewIntegrationHandler, - fx.ParamTags(`group:"integration"`), - ), - - kfx.NewOtelLoggerProvider, - kfx.NewOtelMeterProvider, - kfx.NewOtelTracerProvider, - kfx.NewLogger, - - AsRoute(utils.NewHealthCheckV1), - - AsRoute(utils.NewReflectorV1), - AsRoute(utils.NewReflectorV1Alpha1), - - AsRoute(kfx.NewEchoHandler), - - AsRoute(messagebusv1.NewMessagebusRoute), - - AsRoute(projectv1.NewProjectRoute), - AsRoute(todov1.NewTodoRoute), - AsRoute(mandantv1.NewMandantRoute), - - fx.Annotate( - kfx.NewServeMux, - fx.ParamTags(`group:"connectRoute"`), - ), - ), - - fx.Invoke(func(*integration.IntegrationHandler) {}, kfx.SetupOTelSDK, func(*http.Server) {}), - ).Run() + core.Main(fx.SetupOTelSDK, func(*http.Server) {}) } diff --git a/cmd/cronjobs/imap/main.go b/cmd/cronjobs/imap/main.go new file mode 100644 index 0000000..ede4896 --- /dev/null +++ b/cmd/cronjobs/imap/main.go @@ -0,0 +1,19 @@ +package main + +import ( + "context" + "time" + + "git.kocoder.xyz/kocoded/vt/core" + "git.kocoder.xyz/kocoded/vt/integration" +) + +func main() { + ctx, cancel := context.WithTimeout(context.Background(), time.Second*15) + defer cancel() + + app := core.Main(func(*integration.IntegrationHandler) {}) + app.Stop(ctx) + + return +} diff --git a/core/main.go b/core/main.go new file mode 100644 index 0000000..1f2dd0c --- /dev/null +++ b/core/main.go @@ -0,0 +1,100 @@ +package core + +import ( + "context" + "fmt" + + "github.com/joho/godotenv" + "go.uber.org/fx" + + kfx "git.kocoder.xyz/kocoded/vt/fx" + "git.kocoder.xyz/kocoded/vt/fx/interfaces" + "git.kocoder.xyz/kocoded/vt/fx/interfaces/stores" + "git.kocoder.xyz/kocoded/vt/integration" + "git.kocoder.xyz/kocoded/vt/integration/mailing" + "git.kocoder.xyz/kocoded/vt/interceptors" + "git.kocoder.xyz/kocoded/vt/repositories" + mandantv1 "git.kocoder.xyz/kocoded/vt/routers/mandant/v1" + messagebusv1 "git.kocoder.xyz/kocoded/vt/routers/messagebus/v1" + projectv1 "git.kocoder.xyz/kocoded/vt/routers/project/v1" + todov1 "git.kocoder.xyz/kocoded/vt/routers/todo/v1" + "git.kocoder.xyz/kocoded/vt/utils" +) + +func AsIntegration(f any) any { + return fx.Annotate( + f, + fx.As(new(interfaces.BasicIntegration)), + fx.ResultTags(`group:"integration"`), + ) +} + +func AsRoute(f any) any { + return fx.Annotate( + f, + fx.As(new(kfx.Handler)), + fx.ResultTags(`group:"connectRoute"`), + ) +} + +func Main(funcs ...interface{}) *fx.App { + context := context.Background() + err := godotenv.Load() + + if err != nil { + fmt.Println("Error loading env vars... %v", err) + } + + app := fx.New( + fx.Provide( + kfx.NewHTTPServer, + utils.NewReflector, + utils.NewHealthchecker, + + utils.NewDB, + utils.NewCache, + + utils.NewOIDCProvider, + utils.NewOIDCVerifier, + + fx.Annotate(repositories.NewSessionRepository, fx.As(new(stores.SessionStore))), + fx.Annotate(repositories.NewInMemeoryMessageRepository, fx.As(new(stores.MessageStore))), + interceptors.NewOIDCInterceptor, + + AsIntegration(mailing.NewImapIntegration), + fx.Annotate( + integration.NewIntegrationHandler, + fx.ParamTags(`group:"integration"`), + ), + + kfx.NewOtelLoggerProvider, + kfx.NewOtelMeterProvider, + kfx.NewOtelTracerProvider, + kfx.NewLogger, + + AsRoute(utils.NewHealthCheckV1), + + AsRoute(utils.NewReflectorV1), + AsRoute(utils.NewReflectorV1Alpha1), + + AsRoute(kfx.NewEchoHandler), + + AsRoute(messagebusv1.NewMessagebusRoute), + + AsRoute(projectv1.NewProjectRoute), + AsRoute(todov1.NewTodoRoute), + AsRoute(mandantv1.NewMandantRoute), + + fx.Annotate( + kfx.NewServeMux, + fx.ParamTags(`group:"connectRoute"`), + ), + ), + + fx.Invoke(funcs...), + ) + + app.Start(context) + + return app +} diff --git a/integration/mailing/imap_integration.go b/integration/mailing/imap_integration.go index 8a85538..42e8ae7 100644 --- a/integration/mailing/imap_integration.go +++ b/integration/mailing/imap_integration.go @@ -3,6 +3,8 @@ package mailing import ( "context" "fmt" + "os" + "strconv" "time" "git.kocoder.xyz/kocoded/vt/fx/interfaces" @@ -39,7 +41,12 @@ func NewImapIntegration() interfaces.BasicIntegration { imap.DialTimeout = 10 * time.Second imap.CommandTimeout = 30 * time.Second - m, err := imap.New("me@kocoder.xyz", "&,25,upMeddeEnTYPTifaccEptIonaAlErGiE", "mx.kocoder.xyz", 993) + port, err := strconv.Atoi(os.Getenv("IMAP_INTEGRATION_PORT")) + if err != nil { + panic(err) + } + + m, err := imap.New(os.Getenv("IMAP_INTEGRATION_USER"), os.Getenv("IMAP_INTEGRATION_PASS"), os.Getenv("IMAP_INTEGRATION_HOST"), port) if err != nil { panic(err) @@ -54,7 +61,7 @@ func NewImapIntegration() interfaces.BasicIntegration { panic("Search") } - emails, err := m.GetEmails(uids[:5]...) + emails, err := m.GetEmails(uids...) if err != nil { panic("Getting EMails") } @@ -62,6 +69,7 @@ func NewImapIntegration() interfaces.BasicIntegration { for uid, email := range emails { fmt.Printf("\n--- Email UID %d ---\n", uid) fmt.Printf("From: %s\n", email.From) + fmt.Printf("To: %s\n", email.To) fmt.Printf("Subject: %s\n", email.Subject) fmt.Printf("Date: %s\n", email.Sent.Format("Jan 2, 2006 3:04 PM")) fmt.Printf("Size: %.1f KB\n", float64(email.Size)/1024)