diff --git a/cmd/identra-gateway/main.go b/cmd/identra-gateway/main.go index 4f7c207..5a0f351 100644 --- a/cmd/identra-gateway/main.go +++ b/cmd/identra-gateway/main.go @@ -11,7 +11,7 @@ import ( "strings" "github.com/grpc-ecosystem/grpc-gateway/v2/runtime" - "github.com/poly-workshop/identra/internal/pkg/app" + "github.com/poly-workshop/identra/internal/infrastructure/bootstrap" identra_v1_pb "github.com/poly-workshop/identra/gen/go/identra/v1" "github.com/poly-workshop/identra/internal/infrastructure/configs" "github.com/rs/cors" @@ -20,7 +20,7 @@ import ( ) func init() { - app.Init("gateway") + bootstrap.Init("gateway") } // Gateway wraps the grpc-gateway mux and provides HTTP endpoints for gRPC services diff --git a/cmd/identra-grpc/main.go b/cmd/identra-grpc/main.go index 2b41ff4..bc8fb60 100644 --- a/cmd/identra-grpc/main.go +++ b/cmd/identra-grpc/main.go @@ -8,10 +8,9 @@ import ( "net" "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging" - "github.com/poly-workshop/identra/internal/pkg/app" - "github.com/poly-workshop/identra/internal/pkg/grpcutils" - "github.com/poly-workshop/identra/internal/pkg/redisclient" - "github.com/poly-workshop/identra/internal/pkg/smtpmailer" + "github.com/poly-workshop/identra/internal/infrastructure/bootstrap" + "github.com/poly-workshop/identra/internal/infrastructure/cache/redis" + "github.com/poly-workshop/identra/internal/infrastructure/notification/smtp" identra_v1_pb "github.com/poly-workshop/identra/gen/go/identra/v1" "github.com/poly-workshop/identra/internal/application/identra" "github.com/poly-workshop/identra/internal/infrastructure/configs" @@ -20,7 +19,7 @@ import ( ) func init() { - app.Init("grpc") + bootstrap.Init("grpc") } // InterceptorLogger adapts slog logger to interceptor logger. @@ -52,7 +51,7 @@ func main() { // Setup gRPC server with auth interceptor grpcServer := grpc.NewServer( grpc.ChainUnaryInterceptor( - grpcutils.BuildRequestIDInterceptor(), + bootstrap.BuildRequestIDInterceptor(), logging.UnaryServerInterceptor(InterceptorLogger(slog.Default())), ), ) @@ -73,8 +72,8 @@ func main() { func toIdentraConfig( authCfg configs.AuthConfig, - redisCfg redisclient.Config, - mailerCfg smtpmailer.Config, + redisCfg redis.Config, + mailerCfg smtp.Config, presistenceCfg configs.PersistenceConfig, ) identra.Config { return identra.Config{ diff --git a/internal/application/identra/config.go b/internal/application/identra/config.go index 59c43fe..e69fb51 100644 --- a/internal/application/identra/config.go +++ b/internal/application/identra/config.go @@ -3,10 +3,10 @@ package identra import ( "time" - "github.com/poly-workshop/identra/internal/pkg/gormclient" - "github.com/poly-workshop/identra/internal/pkg/mongoclient" - "github.com/poly-workshop/identra/internal/pkg/redisclient" - "github.com/poly-workshop/identra/internal/pkg/smtpmailer" + "github.com/poly-workshop/identra/internal/infrastructure/cache/redis" + "github.com/poly-workshop/identra/internal/infrastructure/notification/smtp" + "github.com/poly-workshop/identra/internal/infrastructure/persistence/gorm" + "github.com/poly-workshop/identra/internal/infrastructure/persistence/mongo" ) // Config holds all settings required to run the identra service. @@ -19,11 +19,11 @@ type Config struct { AccessTokenExpirationDuration time.Duration RefreshTokenExpirationDuration time.Duration TokenIssuer string - SmtpMailer smtpmailer.Config + SmtpMailer smtp.Config DatabaseType string - GORMClient *gormclient.Config - MongoClient *mongoclient.Config - RedisClient *redisclient.Config + GORMClient *gorm.Config + MongoClient *mongo.Config + RedisClient *redis.Config PresistenceType string } diff --git a/internal/application/identra/service.go b/internal/application/identra/service.go index 01b94fb..145a8be 100644 --- a/internal/application/identra/service.go +++ b/internal/application/identra/service.go @@ -12,9 +12,9 @@ import ( "strings" "time" - "github.com/poly-workshop/identra/internal/pkg/gormclient" - "github.com/poly-workshop/identra/internal/pkg/redisclient" - "github.com/poly-workshop/identra/internal/pkg/smtpmailer" + "github.com/poly-workshop/identra/internal/infrastructure/cache/redis" + "github.com/poly-workshop/identra/internal/infrastructure/notification/smtp" + "github.com/poly-workshop/identra/internal/infrastructure/persistence/gorm" identra_v1_pb "github.com/poly-workshop/identra/gen/go/identra/v1" "github.com/poly-workshop/identra/internal/domain" "github.com/poly-workshop/identra/internal/infrastructure/cache" @@ -47,19 +47,19 @@ type Service struct { tokenCfg security.TokenConfig githubOAuthConfig *oauth2.Config oauthFetchEmailIfMissing bool - mailer *smtpmailer.Mailer + mailer *smtp.Mailer } func NewService(ctx context.Context, cfg Config) (*Service, error) { mailerCfg := cfg.SmtpMailer - var mailer *smtpmailer.Mailer + var mailer *smtp.Mailer if strings.TrimSpace(mailerCfg.Host) != "" { if err := validateMailerConfig(mailerCfg); err != nil { return nil, fmt.Errorf("invalid mailer config: %w", err) } - mailer = smtpmailer.NewMailer(mailerCfg) + mailer = smtp.NewMailer(mailerCfg) } km := security.GetKeyManager() @@ -103,7 +103,7 @@ func NewService(ctx context.Context, cfg Config) (*Service, error) { Endpoint: github.Endpoint, } - emailStore, storeErr := cache.NewRedisEmailCodeStore(10*time.Minute, redisclient.NewRDB(*cfg.RedisClient)) + emailStore, storeErr := cache.NewRedisEmailCodeStore(10*time.Minute, redis.NewRDB(*cfg.RedisClient)) if storeErr != nil { return nil, fmt.Errorf("failed to initialize email code store: %w", storeErr) } @@ -420,7 +420,7 @@ func (s *Service) sendVerificationCode(to string, code string, expiryMinutes int `, code, expiryMinutes) - return s.mailer.SendEmail(smtpmailer.Message{ + return s.mailer.SendEmail(smtp.Message{ ToEmails: []string{to}, Subject: subject, Body: htmlBody, @@ -429,7 +429,7 @@ func (s *Service) sendVerificationCode(to string, code string, expiryMinutes int } body := fmt.Sprintf("Your verification code is: %s (valid for %d minutes)", code, expiryMinutes) - return s.mailer.SendEmail(smtpmailer.Message{ + return s.mailer.SendEmail(smtp.Message{ ToEmails: []string{to}, Subject: subject, Body: body, @@ -437,7 +437,7 @@ func (s *Service) sendVerificationCode(to string, code string, expiryMinutes int }) } -func validateMailerConfig(cfg smtpmailer.Config) error { +func validateMailerConfig(cfg smtp.Config) error { if strings.TrimSpace(cfg.Host) == "" { return nil } @@ -718,7 +718,7 @@ func buildUserStore(ctx context.Context, cfg Config) (domain.UserStore, func(con } return repo, cleanup, nil case "", "gorm", "postgres", "mysql", "sqlite": - db := gormclient.NewDB(*cfg.GORMClient) + db := gorm.NewDB(*cfg.GORMClient) if err := db.AutoMigrate(&domain.UserModel{}); err != nil { slog.Error("failed to migrate database", "error", err) } diff --git a/internal/pkg/app/config.go b/internal/infrastructure/bootstrap/config.go similarity index 91% rename from internal/pkg/app/config.go rename to internal/infrastructure/bootstrap/config.go index e4373ea..e8a2107 100644 --- a/internal/pkg/app/config.go +++ b/internal/infrastructure/bootstrap/config.go @@ -1,5 +1,5 @@ -// Package app provides application initialization and configuration management. -package app +// Package bootstrap provides application initialization and configuration management. +package bootstrap import ( "path" diff --git a/internal/pkg/app/init.go b/internal/infrastructure/bootstrap/init.go similarity index 97% rename from internal/pkg/app/init.go rename to internal/infrastructure/bootstrap/init.go index 32c0e7b..af58df2 100644 --- a/internal/pkg/app/init.go +++ b/internal/infrastructure/bootstrap/init.go @@ -1,4 +1,4 @@ -package app +package bootstrap import ( "log/slog" diff --git a/internal/pkg/grpcutils/interceptors.go b/internal/infrastructure/bootstrap/interceptors.go similarity index 84% rename from internal/pkg/grpcutils/interceptors.go rename to internal/infrastructure/bootstrap/interceptors.go index 1f3683d..e39c8c0 100644 --- a/internal/pkg/grpcutils/interceptors.go +++ b/internal/infrastructure/bootstrap/interceptors.go @@ -1,13 +1,12 @@ -// Package grpcutils provides gRPC server interceptors for logging and request +// Package bootstrap provides gRPC server interceptors for logging and request // ID tracking with context propagation. -package grpcutils +package bootstrap import ( "context" "log/slog" "github.com/google/uuid" - "github.com/poly-workshop/identra/internal/pkg/app" "google.golang.org/grpc" "google.golang.org/grpc/metadata" ) @@ -34,7 +33,7 @@ func BuildRequestIDInterceptor() grpc.UnaryServerInterceptor { requestID = uuid.New().String() } - ctx = app.WithLogAttrs(ctx, slog.String("request_id", requestID)) + ctx = WithLogAttrs(ctx, slog.String("request_id", requestID)) // Call the handler resp, err := handler(ctx, req) diff --git a/internal/pkg/app/log.go b/internal/infrastructure/bootstrap/log.go similarity index 99% rename from internal/pkg/app/log.go rename to internal/infrastructure/bootstrap/log.go index f5dbec0..5f0d88f 100644 --- a/internal/pkg/app/log.go +++ b/internal/infrastructure/bootstrap/log.go @@ -1,4 +1,4 @@ -package app +package bootstrap import ( "context" diff --git a/internal/pkg/redisclient/cache.go b/internal/infrastructure/cache/redis/cache.go similarity index 99% rename from internal/pkg/redisclient/cache.go rename to internal/infrastructure/cache/redis/cache.go index 745b90e..16db97b 100644 --- a/internal/pkg/redisclient/cache.go +++ b/internal/infrastructure/cache/redis/cache.go @@ -1,4 +1,4 @@ -package redisclient +package redis import ( "context" diff --git a/internal/pkg/redisclient/client.go b/internal/infrastructure/cache/redis/client.go similarity index 89% rename from internal/pkg/redisclient/client.go rename to internal/infrastructure/cache/redis/client.go index 3b04009..22df7da 100644 --- a/internal/pkg/redisclient/client.go +++ b/internal/infrastructure/cache/redis/client.go @@ -1,5 +1,5 @@ -// Package redisclient provides a factory for creating Redis connections. -package redisclient +// Package redis provides a factory for creating Redis connections. +package redis import ( "github.com/redis/go-redis/v9" diff --git a/internal/infrastructure/configs/gateway.go b/internal/infrastructure/configs/gateway.go index 9a949ae..3b0f2da 100644 --- a/internal/infrastructure/configs/gateway.go +++ b/internal/infrastructure/configs/gateway.go @@ -1,6 +1,6 @@ package configs -import "github.com/poly-workshop/identra/internal/pkg/app" +import "github.com/poly-workshop/identra/internal/infrastructure/bootstrap" type GatewayConfig struct { HTTPPort uint @@ -8,6 +8,6 @@ type GatewayConfig struct { func LoadGateway() GatewayConfig { return GatewayConfig{ - HTTPPort: app.Config().GetUint(HTTPPortKey), + HTTPPort: bootstrap.Config().GetUint(HTTPPortKey), } } diff --git a/internal/infrastructure/configs/grpc.go b/internal/infrastructure/configs/grpc.go index 761c6a7..5248ebb 100644 --- a/internal/infrastructure/configs/grpc.go +++ b/internal/infrastructure/configs/grpc.go @@ -3,17 +3,17 @@ package configs import ( "time" - "github.com/poly-workshop/identra/internal/pkg/app" - "github.com/poly-workshop/identra/internal/pkg/gormclient" - "github.com/poly-workshop/identra/internal/pkg/mongoclient" - "github.com/poly-workshop/identra/internal/pkg/redisclient" - "github.com/poly-workshop/identra/internal/pkg/smtpmailer" + "github.com/poly-workshop/identra/internal/infrastructure/bootstrap" + "github.com/poly-workshop/identra/internal/infrastructure/cache/redis" + "github.com/poly-workshop/identra/internal/infrastructure/notification/smtp" + "github.com/poly-workshop/identra/internal/infrastructure/persistence/gorm" + "github.com/poly-workshop/identra/internal/infrastructure/persistence/mongo" ) type GRPCConfig struct { GRPCPort uint - Redis redisclient.Config - SmtpMailer smtpmailer.Config + Redis redis.Config + SmtpMailer smtp.Config Persistence PersistenceConfig Auth AuthConfig } @@ -39,8 +39,8 @@ type TokenConfig struct { type PersistenceConfig struct { Type string - GORM *gormclient.Config - Mongo *mongoclient.Config + GORM *gorm.Config + Mongo *mongo.Config } const ( @@ -52,47 +52,47 @@ const ( func LoadGRPC() GRPCConfig { cfg := GRPCConfig{ - GRPCPort: app.Config().GetUint(GRPCPortKey), - SmtpMailer: smtpmailer.Config{ - Host: app.Config().GetString(SmtpMailerHostKey), - Port: app.Config().GetInt(SmtpMailerPortKey), - Username: app.Config().GetString(SmtpMailerUsernameKey), - Password: app.Config().GetString(SmtpMailerPasswordKey), - FromEmail: app.Config().GetString(SmtpMailerFromEmailKey), - FromName: app.Config().GetString(SmtpMailerFromNameKey), + GRPCPort: bootstrap.Config().GetUint(GRPCPortKey), + SmtpMailer: smtp.Config{ + Host: bootstrap.Config().GetString(SmtpMailerHostKey), + Port: bootstrap.Config().GetInt(SmtpMailerPortKey), + Username: bootstrap.Config().GetString(SmtpMailerUsernameKey), + Password: bootstrap.Config().GetString(SmtpMailerPasswordKey), + FromEmail: bootstrap.Config().GetString(SmtpMailerFromEmailKey), + FromName: bootstrap.Config().GetString(SmtpMailerFromNameKey), }, Persistence: PersistenceConfig{ - Type: app.Config().GetString(PersistenceTypeKey), - GORM: &gormclient.Config{ - Driver: app.Config().GetString(PersistenceGORMDriverKey), - Host: app.Config().GetString(PersistenceGORMHostKey), - Port: app.Config().GetInt(PersistenceGORMPortKey), - Username: app.Config().GetString(PersistenceGORMUsernameKey), - Password: app.Config().GetString(PersistenceGORMPasswordKey), - DbName: app.Config().GetString(PersistenceGORMNameKey), - SSLMode: app.Config().GetString(PersistenceGORMSSLModeKey), + Type: bootstrap.Config().GetString(PersistenceTypeKey), + GORM: &gorm.Config{ + Driver: bootstrap.Config().GetString(PersistenceGORMDriverKey), + Host: bootstrap.Config().GetString(PersistenceGORMHostKey), + Port: bootstrap.Config().GetInt(PersistenceGORMPortKey), + Username: bootstrap.Config().GetString(PersistenceGORMUsernameKey), + Password: bootstrap.Config().GetString(PersistenceGORMPasswordKey), + DbName: bootstrap.Config().GetString(PersistenceGORMNameKey), + SSLMode: bootstrap.Config().GetString(PersistenceGORMSSLModeKey), }, - Mongo: &mongoclient.Config{ - URI: app.Config().GetString(PersistenceMongoURIKey), - Database: app.Config().GetString(PersistenceMongoDatabaseKey), + Mongo: &mongo.Config{ + URI: bootstrap.Config().GetString(PersistenceMongoURIKey), + Database: bootstrap.Config().GetString(PersistenceMongoDatabaseKey), }, }, - Redis: redisclient.Config{ - Urls: app.Config().GetStringSlice(RedisUrlsKey), - Password: app.Config().GetString(RedisPasswordKey), + Redis: redis.Config{ + Urls: bootstrap.Config().GetStringSlice(RedisUrlsKey), + Password: bootstrap.Config().GetString(RedisPasswordKey), }, Auth: AuthConfig{ - RSAPrivateKey: app.Config().GetString(AuthRSAPrivateKeyKey), + RSAPrivateKey: bootstrap.Config().GetString(AuthRSAPrivateKeyKey), OAuth: OAuthConfig{ - StateExpirationDuration: app.Config().GetDuration(AuthOAuthStateExpirationKey), - GithubClientID: app.Config().GetString(AuthGithubClientIDKey), - GithubClientSecret: app.Config().GetString(AuthGithubClientSecretKey), - FetchEmailIfMissing: app.Config().GetBool(AuthOAuthFetchEmailIfMissingKey), + StateExpirationDuration: bootstrap.Config().GetDuration(AuthOAuthStateExpirationKey), + GithubClientID: bootstrap.Config().GetString(AuthGithubClientIDKey), + GithubClientSecret: bootstrap.Config().GetString(AuthGithubClientSecretKey), + FetchEmailIfMissing: bootstrap.Config().GetBool(AuthOAuthFetchEmailIfMissingKey), }, Token: TokenConfig{ - Issuer: app.Config().GetString(AuthTokenIssuerKey), - AccessTokenExpiration: app.Config().GetDuration(AuthAccessTokenExpirationKey), - RefreshTokenExpiration: app.Config().GetDuration(AuthRefreshTokenExpirationKey), + Issuer: bootstrap.Config().GetString(AuthTokenIssuerKey), + AccessTokenExpiration: bootstrap.Config().GetDuration(AuthAccessTokenExpirationKey), + RefreshTokenExpiration: bootstrap.Config().GetDuration(AuthRefreshTokenExpirationKey), }, }, } diff --git a/internal/infrastructure/mail/sender.go b/internal/infrastructure/mail/sender.go index a680e5f..c499aba 100644 --- a/internal/infrastructure/mail/sender.go +++ b/internal/infrastructure/mail/sender.go @@ -1,18 +1,18 @@ package mail -import "github.com/poly-workshop/identra/internal/pkg/smtpmailer" +import "github.com/poly-workshop/identra/internal/infrastructure/notification/smtp" // Sender defines the interface for sending emails. type Sender interface { - SendEmail(msg smtpmailer.Message) error + SendEmail(msg smtp.Message) error } // NewSMTPSender creates a new SMTP-based mail sender. -func NewSMTPSender(cfg smtpmailer.Config) Sender { - return smtpmailer.NewMailer(cfg) +func NewSMTPSender(cfg smtp.Config) Sender { + return smtp.NewMailer(cfg) } // NewSenderFromConfig creates a mail sender based on configuration. -func NewSenderFromConfig(cfg smtpmailer.Config) Sender { - return smtpmailer.NewMailer(cfg) +func NewSenderFromConfig(cfg smtp.Config) Sender { + return smtp.NewMailer(cfg) } diff --git a/internal/pkg/smtpmailer/mailer.go b/internal/infrastructure/notification/smtp/mailer.go similarity index 96% rename from internal/pkg/smtpmailer/mailer.go rename to internal/infrastructure/notification/smtp/mailer.go index 3eee5aa..fd549f7 100644 --- a/internal/pkg/smtpmailer/mailer.go +++ b/internal/infrastructure/notification/smtp/mailer.go @@ -1,5 +1,5 @@ -// Package smtpmailer provides SMTP email sending functionality. -package smtpmailer +// Package smtp provides SMTP email sending functionality. +package smtp import ( "crypto/tls" diff --git a/internal/pkg/gormclient/db.go b/internal/infrastructure/persistence/gorm/db.go similarity index 94% rename from internal/pkg/gormclient/db.go rename to internal/infrastructure/persistence/gorm/db.go index 1c758dc..8e4d92c 100644 --- a/internal/pkg/gormclient/db.go +++ b/internal/infrastructure/persistence/gorm/db.go @@ -1,5 +1,5 @@ -// Package gormclient provides a factory for creating GORM database connections. -package gormclient +// Package gorm provides a factory for creating GORM database connections. +package gorm import ( "fmt" diff --git a/internal/pkg/mongoclient/client.go b/internal/infrastructure/persistence/mongo/client.go similarity index 93% rename from internal/pkg/mongoclient/client.go rename to internal/infrastructure/persistence/mongo/client.go index 7087fab..5d8935f 100644 --- a/internal/pkg/mongoclient/client.go +++ b/internal/infrastructure/persistence/mongo/client.go @@ -1,5 +1,5 @@ -// Package mongoclient provides a factory for creating MongoDB connections. -package mongoclient +// Package mongo provides a factory for creating MongoDB connections. +package mongo import ( "context"