Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cmd/identra-gateway/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
15 changes: 7 additions & 8 deletions cmd/identra-grpc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -20,7 +19,7 @@ import (
)

func init() {
app.Init("grpc")
bootstrap.Init("grpc")
}

// InterceptorLogger adapts slog logger to interceptor logger.
Expand Down Expand Up @@ -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())),
),
)
Expand All @@ -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{
Expand Down
16 changes: 8 additions & 8 deletions internal/application/identra/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
}

Expand Down
22 changes: 11 additions & 11 deletions internal/application/identra/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -420,7 +420,7 @@ func (s *Service) sendVerificationCode(to string, code string, expiryMinutes int
</html>
`, code, expiryMinutes)

return s.mailer.SendEmail(smtpmailer.Message{
return s.mailer.SendEmail(smtp.Message{
ToEmails: []string{to},
Subject: subject,
Body: htmlBody,
Expand All @@ -429,15 +429,15 @@ 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,
IsHTML: false,
})
}

func validateMailerConfig(cfg smtpmailer.Config) error {
func validateMailerConfig(cfg smtp.Config) error {
if strings.TrimSpace(cfg.Host) == "" {
return nil
}
Expand Down Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package app
package bootstrap

import (
"log/slog"
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
)
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package app
package bootstrap

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package redisclient
package redis

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
4 changes: 2 additions & 2 deletions internal/infrastructure/configs/gateway.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
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
}

func LoadGateway() GatewayConfig {
return GatewayConfig{
HTTPPort: app.Config().GetUint(HTTPPortKey),
HTTPPort: bootstrap.Config().GetUint(HTTPPortKey),
}
}
80 changes: 40 additions & 40 deletions internal/infrastructure/configs/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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 (
Expand All @@ -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),
},
},
}
Expand Down
12 changes: 6 additions & 6 deletions internal/infrastructure/mail/sender.go
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
Loading