Skip to content

Commit 7769ba6

Browse files
committed
refactor: cqrs improvements;
1 parent 79f8584 commit 7769ba6

13 files changed

+324
-80
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
tmp/
55

66
.env*
7-
nats/
7+
natsdata/
88
yeahapi*conf

aws/email.go

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package aws
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
"time"
8+
9+
"github.com/aws/aws-sdk-go-v2/aws"
10+
"github.com/aws/aws-sdk-go-v2/service/ses"
11+
"github.com/aws/aws-sdk-go-v2/service/ses/types"
12+
"github.com/nats-io/nats.go/jetstream"
13+
yeahapi "github.com/yeahuz/yeah-api"
14+
)
15+
16+
type EmailService struct {
17+
ses *ses.Client
18+
cqrssrv yeahapi.CQRSService
19+
}
20+
21+
func NewEmailService(cfg aws.Config, cqrssrv yeahapi.CQRSService) *EmailService {
22+
ses := ses.NewFromConfig(cfg)
23+
return &EmailService{
24+
ses: ses,
25+
cqrssrv: cqrssrv,
26+
}
27+
}
28+
29+
func (e *EmailService) HandleSendEmailCode(m jetstream.Msg) error {
30+
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
31+
defer cancel()
32+
33+
var cmd yeahapi.SendEmailCodeCmd
34+
if err := json.Unmarshal(m.Data(), &cmd); err != nil {
35+
return err
36+
}
37+
38+
_, err := e.ses.SendEmail(ctx, &ses.SendEmailInput{
39+
Destination: &types.Destination{
40+
ToAddresses: []string{
41+
cmd.Email,
42+
},
43+
},
44+
Source: aws.String("Needs <[email protected]>"),
45+
46+
Message: &types.Message{
47+
Subject: &types.Content{
48+
Data: aws.String(fmt.Sprintf("Your code: %s", cmd.Code)),
49+
},
50+
Body: &types.Body{
51+
Text: &types.Content{Data: aws.String(cmd.Code)},
52+
},
53+
},
54+
})
55+
56+
if err != nil {
57+
return err
58+
}
59+
60+
e.cqrssrv.Publish(ctx, yeahapi.NewEmailCodeSentEvent(cmd.Email))
61+
62+
return nil
63+
}

cmd/yeahapi/main.go

+30-13
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,16 @@ import (
1010
"path/filepath"
1111
"strings"
1212

13+
awsconf "github.com/aws/aws-sdk-go-v2/config"
14+
"github.com/aws/aws-sdk-go-v2/credentials"
1315
"github.com/jackc/pgx/v5/pgxpool"
16+
1417
"github.com/pelletier/go-toml/v2"
1518
yeahapi "github.com/yeahuz/yeah-api"
19+
"github.com/yeahuz/yeah-api/aws"
1620
"github.com/yeahuz/yeah-api/http"
1721
"github.com/yeahuz/yeah-api/inmem"
22+
"github.com/yeahuz/yeah-api/nats"
1823
"github.com/yeahuz/yeah-api/postgres"
1924
)
2025

@@ -88,18 +93,30 @@ func (m *Main) Run(ctx context.Context) (err error) {
8893
userService := postgres.NewUserService(m.Pool)
8994
localizerService := yeahapi.NewLocalizerService("en")
9095

91-
// cqrsService := nats.NewCQRSService(ctx, yeahapi.CQRSConfig{
92-
// NatsURL: m.Config.Nats.URL,
93-
// NatsAuthToken: m.Config.Nats.AuthToken,
94-
// Streams: map[string][]string{},
95-
// })
96+
cqrsService, err := nats.NewCQRSService(ctx, yeahapi.CQRSConfig{
97+
NatsURL: m.Config.Nats.URL,
98+
NatsAuthToken: m.Config.Nats.AuthToken,
99+
Streams: m.Config.Nats.Streams,
100+
})
101+
102+
if err != nil {
103+
return err
104+
}
105+
106+
awsconfig, err := awsconf.LoadDefaultConfig(ctx,
107+
awsconf.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(m.Config.AWS.Key, m.Config.AWS.Secret, "")),
108+
awsconf.WithRegion("en-north-1"),
109+
)
110+
111+
emailService := aws.NewEmailService(awsconfig, cqrsService)
112+
cqrsService.Handle("auth.sendEmailCode", emailService.HandleSendEmailCode)
96113

97114
m.Server.Addr = m.Config.HTTP.Addr
98115

99116
m.Server.UserService = userService
100117
m.Server.AuthService = authService
101118
m.Server.LocalizerService = localizerService
102-
// m.Server.CQRSService = cqrsService
119+
m.Server.CQRSService = cqrsService
103120

104121
if err := m.Server.Open(); err != nil {
105122
return err
@@ -109,16 +126,16 @@ func (m *Main) Run(ctx context.Context) (err error) {
109126
}
110127

111128
func (m *Main) Close() error {
129+
if m.Pool != nil {
130+
m.Pool.Close()
131+
}
132+
112133
if m.Server != nil {
113134
if err := m.Server.Close(); err != nil {
114135
return err
115136
}
116137
}
117138

118-
if m.Pool != nil {
119-
m.Pool.Close()
120-
}
121-
122139
return nil
123140
}
124141

@@ -167,9 +184,9 @@ type Config struct {
167184
} `toml:"highwayhash"`
168185

169186
Nats struct {
170-
AuthToken string `toml:"auth-token"`
171-
URL string `toml:"url"`
172-
Streams map[string]string `toml:"streams"`
187+
AuthToken string `toml:"auth-token"`
188+
URL string `toml:"url"`
189+
Streams map[string][]string `toml:"streams"`
173190
} `toml:"nats"`
174191

175192
Google struct {

cqrs.go

+74-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@ package yeahapi
22

33
import (
44
"context"
5+
6+
"github.com/nats-io/nats.go/jetstream"
7+
)
8+
9+
const (
10+
sendPhoneCode = "auth.sendPhoneCode"
11+
sendEmailCode = "auth.sendEmailCode"
12+
emailCodeSent = "auth.emailCodeSent"
13+
phoneCodeSent = "auth.phoneCodeSent"
514
)
615

716
type CQRSConfig struct {
@@ -14,6 +23,70 @@ type CQRSMessage interface {
1423
Subject() string
1524
}
1625

26+
type CQRSHandler func(message jetstream.Msg) error
27+
1728
type CQRSService interface {
18-
Send(ctx context.Context, message CQRSMessage) error
29+
Publish(ctx context.Context, message CQRSMessage) error
30+
Handle(subject string, handler CQRSHandler)
31+
Close() error
32+
}
33+
34+
type subject struct {
35+
subject string
36+
}
37+
38+
func (s subject) Subject() string {
39+
return s.subject
40+
}
41+
42+
type SendPhoneCodeCmd struct {
43+
subject
44+
PhoneNumber string `json:"phone_number"`
45+
Code string `json:"code"`
46+
}
47+
48+
type EmailCodeSentEvent struct {
49+
subject
50+
Email string `json:"email"`
51+
}
52+
53+
type PhoneCodeSentEvent struct {
54+
subject
55+
PhoneNumber string `json:"phone_number"`
56+
}
57+
58+
type SendEmailCodeCmd struct {
59+
subject
60+
Email string `json:"email"`
61+
Code string `json:"code"`
62+
}
63+
64+
func NewSendPhoneCodeCmd(phoneNumber string, code string) SendPhoneCodeCmd {
65+
return SendPhoneCodeCmd{
66+
subject: subject{sendPhoneCode},
67+
PhoneNumber: phoneNumber,
68+
Code: code,
69+
}
70+
}
71+
72+
func NewSendEmailCodeCmd(email string, code string) SendEmailCodeCmd {
73+
return SendEmailCodeCmd{
74+
subject: subject{sendEmailCode},
75+
Email: email,
76+
Code: code,
77+
}
78+
}
79+
80+
func NewEmailCodeSentEvent(email string) EmailCodeSentEvent {
81+
return EmailCodeSentEvent{
82+
subject: subject{emailCodeSent},
83+
Email: email,
84+
}
85+
}
86+
87+
func NewPhoneCodeSentEvent(phoneNumber string) PhoneCodeSentEvent {
88+
return PhoneCodeSentEvent{
89+
subject: subject{phoneCodeSent},
90+
PhoneNumber: phoneNumber,
91+
}
1992
}

db/pool.go

-5
This file was deleted.

go.mod

+14-12
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,38 @@ go 1.21
44

55
require (
66
github.com/aws/aws-sdk-go v1.48.9
7+
github.com/aws/aws-sdk-go-v2 v1.24.0
8+
github.com/aws/aws-sdk-go-v2/config v1.26.2
79
github.com/aws/aws-sdk-go-v2/service/ses v1.19.1
810
github.com/gofrs/uuid v4.4.0+incompatible
911
github.com/jackc/pgx/v5 v5.5.0
10-
github.com/joho/godotenv v1.5.1
1112
github.com/minio/highwayhash v1.0.2
1213
github.com/nats-io/nats.go v1.31.0
1314
github.com/pelletier/go-toml/v2 v2.1.1
1415
golang.org/x/crypto v0.16.0
15-
golang.org/x/oauth2 v0.15.0
1616
golang.org/x/text v0.14.0
1717
)
1818

1919
require (
20-
cloud.google.com/go/compute v1.20.1 // indirect
21-
cloud.google.com/go/compute/metadata v0.2.3 // indirect
22-
github.com/aws/aws-sdk-go-v2 v1.23.4 // indirect
23-
github.com/aws/aws-sdk-go-v2/internal/configsources v1.2.7 // indirect
24-
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.5.7 // indirect
25-
github.com/aws/smithy-go v1.18.1 // indirect
26-
github.com/golang/protobuf v1.5.3 // indirect
20+
github.com/aws/aws-sdk-go-v2/credentials v1.16.13 // indirect
21+
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.14.10 // indirect
22+
github.com/aws/aws-sdk-go-v2/internal/configsources v1.2.9 // indirect
23+
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.5.9 // indirect
24+
github.com/aws/aws-sdk-go-v2/internal/ini v1.7.2 // indirect
25+
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.10.4 // indirect
26+
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.10.9 // indirect
27+
github.com/aws/aws-sdk-go-v2/service/sso v1.18.5 // indirect
28+
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.21.5 // indirect
29+
github.com/aws/aws-sdk-go-v2/service/sts v1.26.6 // indirect
30+
github.com/aws/smithy-go v1.19.0 // indirect
31+
github.com/google/go-cmp v0.5.9 // indirect
2732
github.com/jackc/pgpassfile v1.0.0 // indirect
2833
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
2934
github.com/jackc/puddle/v2 v2.2.1 // indirect
3035
github.com/jmespath/go-jmespath v0.4.0 // indirect
3136
github.com/klauspost/compress v1.17.0 // indirect
3237
github.com/nats-io/nkeys v0.4.5 // indirect
3338
github.com/nats-io/nuid v1.0.1 // indirect
34-
golang.org/x/net v0.19.0 // indirect
3539
golang.org/x/sync v0.1.0 // indirect
3640
golang.org/x/sys v0.15.0 // indirect
37-
google.golang.org/appengine v1.6.7 // indirect
38-
google.golang.org/protobuf v1.31.0 // indirect
3941
)

0 commit comments

Comments
 (0)