优化结构
This commit is contained in:
@@ -13,18 +13,16 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"kra/internal/conf"
|
||||
"kra/internal/config"
|
||||
)
|
||||
|
||||
type emailRepo struct {
|
||||
runtime *conf.Runtime
|
||||
}
|
||||
type emailRepo struct{ runtime *config.Store }
|
||||
|
||||
func NewEmailRepo(runtime *conf.Runtime) system.EmailRepo {
|
||||
func NewEmailRepo(runtime *config.Store) system.EmailRepo {
|
||||
return &emailRepo{runtime: runtime}
|
||||
}
|
||||
|
||||
func (r *emailRepo) email() *conf.AdminBackend_Email {
|
||||
func (r *emailRepo) email() *config.Email {
|
||||
if r.runtime == nil {
|
||||
return nil
|
||||
}
|
||||
@@ -47,7 +45,7 @@ func (r *emailRepo) DefaultRecipients() []string {
|
||||
return []string{strings.TrimSpace(config.To)}
|
||||
}
|
||||
|
||||
func emailEnabled(config *conf.AdminBackend_Email) bool {
|
||||
func emailEnabled(config *config.Email) bool {
|
||||
return config != nil && strings.TrimSpace(config.Host) != "" &&
|
||||
strings.TrimSpace(config.From) != "" && strings.TrimSpace(config.Secret) != "" && config.Port > 0
|
||||
}
|
||||
@@ -82,7 +80,7 @@ func (r *emailRepo) Send(ctx context.Context, to []string, subject, body string)
|
||||
dialer := &net.Dialer{Timeout: 10 * time.Second}
|
||||
var conn net.Conn
|
||||
var err error
|
||||
if config.IsSsl {
|
||||
if config.IsSSL {
|
||||
conn, err = (&tls.Dialer{NetDialer: dialer, Config: &tls.Config{ServerName: config.Host, MinVersion: tls.VersionTLS12}}).DialContext(ctx, "tcp", address)
|
||||
} else {
|
||||
conn, err = dialer.DialContext(ctx, "tcp", address)
|
||||
|
||||
@@ -4,11 +4,11 @@ import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"kra/internal/conf"
|
||||
"kra/internal/config"
|
||||
)
|
||||
|
||||
func TestDefaultRecipientsIgnoreEmptyConfiguration(t *testing.T) {
|
||||
runtime := conf.NewRuntime(&conf.Data{}, &conf.AdminBackend{Email: &conf.AdminBackend_Email{To: " "}})
|
||||
runtime := config.NewStore(&config.Config{Data: &config.Data{}, Admin: &config.Admin{Email: &config.Email{To: " "}}})
|
||||
repo := &emailRepo{runtime: runtime}
|
||||
if got := repo.DefaultRecipients(); len(got) != 0 {
|
||||
t.Fatalf("DefaultRecipients() = %#v, want empty", got)
|
||||
@@ -16,7 +16,7 @@ func TestDefaultRecipientsIgnoreEmptyConfiguration(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestSendRejectsEmptyRecipientsBeforeNetworkDial(t *testing.T) {
|
||||
runtime := conf.NewRuntime(&conf.Data{}, &conf.AdminBackend{Email: &conf.AdminBackend_Email{Host: "smtp.example.com", From: "from@example.com", Secret: "secret", Port: 25}})
|
||||
runtime := config.NewStore(&config.Config{Data: &config.Data{}, Admin: &config.Admin{Email: &config.Email{Host: "smtp.example.com", From: "from@example.com", Secret: "secret", Port: 25}}})
|
||||
repo := &emailRepo{runtime: runtime}
|
||||
if err := repo.Send(context.Background(), []string{" ", ""}, "subject", "body"); err == nil {
|
||||
t.Fatal("Send() accepted empty recipients")
|
||||
|
||||
Reference in New Issue
Block a user