Skip to content

Commit

Permalink
Merge pull request #18 from PickHD/SRS-11
Browse files Browse the repository at this point in the history
SRS-11
  • Loading branch information
PickHD authored May 17, 2023
2 parents e454d3b + 7846bbd commit 3889e2c
Show file tree
Hide file tree
Showing 23 changed files with 562 additions and 51 deletions.
5 changes: 5 additions & 0 deletions shortener/api/v1/proto/shortener/shortener.proto
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,8 @@ message UpdateVisitorCountMessage {
string short_url=1;
}

message UpdateShortenerMessage {
string id =1;
string full_url=2;
}

1 change: 1 addition & 0 deletions shortener/cmd/v1/example.env
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ REDIS_TTL=5
AMQP_SERVER_URL=amqp://guest:guest@amqp:5672/
AMQP_QUEUE_CREATE_SHORTENER=create-shortener-queue
AMQP_QUEUE_UPDATE_VISITOR_COUNT=update-visitor-count-queue
AMQP_QUEUE_UPDATE_SHORTENER=update-shortener-queue

GRPC_PORT=9091

Expand Down
2 changes: 1 addition & 1 deletion shortener/cmd/v1/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func main() {
// Make a channel to receive messages into infinite loop.
forever := make(chan bool)

queues := []string{app.Config.RabbitMQ.QueueCreateShortener, app.Config.RabbitMQ.QueueUpdateVisitor}
queues := []string{app.Config.RabbitMQ.QueueCreateShortener, app.Config.RabbitMQ.QueueUpdateVisitor, app.Config.RabbitMQ.QueueUpdateShortener}

for _, q := range queues {
go infrastructure.ConsumeMessages(app, q)
Expand Down
2 changes: 1 addition & 1 deletion shortener/internal/v1/application/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func SetupApplication(ctx context.Context) (*App, error) {
return app, err
}

queues := []string{app.Config.RabbitMQ.QueueCreateShortener, app.Config.RabbitMQ.QueueUpdateVisitor}
queues := []string{app.Config.RabbitMQ.QueueCreateShortener, app.Config.RabbitMQ.QueueUpdateVisitor, app.Config.RabbitMQ.QueueUpdateShortener}

for _, q := range queues {
_, err = amqpClient.QueueDeclare(
Expand Down
2 changes: 2 additions & 0 deletions shortener/internal/v1/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type (
ConnURL string
QueueCreateShortener string
QueueUpdateVisitor string
QueueUpdateShortener string
}

Tracer struct {
Expand Down Expand Up @@ -75,6 +76,7 @@ func loadConfiguration() *Configuration {
ConnURL: helper.GetEnvString("AMQP_SERVER_URL"),
QueueCreateShortener: helper.GetEnvString("AMQP_QUEUE_CREATE_SHORTENER"),
QueueUpdateVisitor: helper.GetEnvString("AMQP_QUEUE_UPDATE_VISITOR_COUNT"),
QueueUpdateShortener: helper.GetEnvString("AMQP_QUEUE_UPDATE_SHORTENER"),
},
Tracer: &Tracer{
JaegerURL: helper.GetEnvString("JAEGER_URL"),
Expand Down
19 changes: 19 additions & 0 deletions shortener/internal/v1/controller/short.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type (
// rabbitmq
ProcessCreateShortUser(ctx context.Context, msg *shortenerpb.CreateShortenerMessage) error
ProcessUpdateVisitorCount(ctx context.Context, msg *shortenerpb.UpdateVisitorCountMessage) error
ProcessUpdateShortUser(ctx context.Context, msg *shortenerpb.UpdateShortenerMessage) error
}

// ShortControllerImpl is an app short struct that consists of all the dependencies needed for short controller
Expand Down Expand Up @@ -150,3 +151,21 @@ func (sc *ShortControllerImpl) ProcessUpdateVisitorCount(ctx context.Context, ms

return nil
}

func (sc *ShortControllerImpl) ProcessUpdateShortUser(ctx context.Context, msg *shortenerpb.UpdateShortenerMessage) error {
tr := sc.Tracer.Tracer("Shortener-ProcessUpdateShortUser Controller")
_, span := tr.Start(sc.Context, "Start ProcessUpdateShortUser")
defer span.End()

req := &model.UpdateShortRequest{
ID: msg.GetId(),
FullURL: msg.GetFullUrl(),
}

err := sc.ShortSvc.UpdateShort(ctx, req)
if err != nil {
return model.NewError(model.Internal, err.Error())
}

return nil
}
15 changes: 15 additions & 0 deletions shortener/internal/v1/infrastructure/rabbitmq.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,22 @@ func ConsumeMessages(app *application.App, queueName string) {
}

app.Logger.Info(fmt.Sprintf("[%s] Success Process Message :", queueName), req)
case app.Config.RabbitMQ.QueueUpdateShortener:
req := &shortenerpb.UpdateShortenerMessage{}

err := proto.Unmarshal(msg.Body, req)
if err != nil {
app.Logger.Error("Unmarshal proto UpdateShortenerMessage ERROR, ", err)
}

app.Logger.Info(fmt.Sprintf("[%s] Success Consume Message :", queueName), req)

err = dep.ShortController.ProcessUpdateShortUser(app.Context, req)
if err != nil {
app.Logger.Error("ProcessUpdateShortUser ERROR, ", err)
}

app.Logger.Info(fmt.Sprintf("[%s] Success Process Message :", queueName), req)
}
}
}()
Expand Down
5 changes: 5 additions & 0 deletions shortener/internal/v1/model/short.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,9 @@ type (
UpdateVisitorRequest struct {
ShortURL string `json:"short_url"`
}

UpdateShortRequest struct {
ID string `json:"id"`
FullURL string `json:"full_url"`
}
)
25 changes: 25 additions & 0 deletions shortener/internal/v1/repository/short.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/sirupsen/logrus"
"github.com/streadway/amqp"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.opentelemetry.io/otel/sdk/trace"
Expand All @@ -28,6 +29,7 @@ type (
SetFullURLByKey(ctx context.Context, shortURL string, fullURL string, duration time.Duration) error
PublishUpdateVisitorCount(ctx context.Context, req *model.UpdateVisitorRequest) error
UpdateVisitorByShortURL(ctx context.Context, req *model.UpdateVisitorRequest, lastVisitedCount int64) error
UpdateFullURLByID(ctx context.Context, req *model.UpdateShortRequest) error
}

// ShortRepositoryImpl is an app short struct that consists of all the dependencies needed for short repository
Expand Down Expand Up @@ -212,6 +214,29 @@ func (sr *ShortRepositoryImpl) UpdateVisitorByShortURL(ctx context.Context, req
return nil
}

func (sr *ShortRepositoryImpl) UpdateFullURLByID(ctx context.Context, req *model.UpdateShortRequest) error {
tr := sr.Tracer.Tracer("Shortener-UpdateFullURLByID Repository")
ctx, span := tr.Start(ctx, "Start UpdateFullURLByID")
defer span.End()

objShortID, err := primitive.ObjectIDFromHex(req.ID)
if err != nil {
sr.Logger.Error("ShortRepositoryImpl.UpdateFullURLByID primitive.ObjectIDFromHex ERROR, ", err)
return err
}

_, err = sr.DB.Collection(sr.Config.Database.ShortenersCollection).UpdateOne(ctx,
bson.D{{Key: "_id", Value: objShortID}}, bson.M{
"$set": bson.D{{Key: "full_url", Value: req.FullURL}, {Key: "updated_at", Value: time.Now()}},
})
if err != nil {
sr.Logger.Error("ShortRepositoryImpl.UpdateFullURLByID UpdateOne ERROR, ", err)
return err
}

return nil
}

func (sr *ShortRepositoryImpl) prepareProtoPublishUpdateVisitorCountMessage(req *model.UpdateVisitorRequest) *shortenerpb.UpdateVisitorCountMessage {
return &shortenerpb.UpdateVisitorCountMessage{
ShortUrl: req.ShortURL,
Expand Down
13 changes: 13 additions & 0 deletions shortener/internal/v1/service/short.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type (
CreateShort(ctx context.Context, req *model.CreateShortRequest) error
ClickShort(shortURL string) (*model.ClickShortResponse, error)
UpdateVisitorShort(ctx context.Context, req *model.UpdateVisitorRequest) error
UpdateShort(ctx context.Context, req *model.UpdateShortRequest) error
}

// ShortServiceImpl is an app short struct that consists of all the dependencies needed for short repository
Expand Down Expand Up @@ -143,6 +144,18 @@ func (ss *ShortServiceImpl) UpdateVisitorShort(ctx context.Context, req *model.U
return nil
}

func (ss *ShortServiceImpl) UpdateShort(ctx context.Context, req *model.UpdateShortRequest) error {
tr := ss.Tracer.Tracer("Shortener-UpdateShort Service")
ctx, span := tr.Start(ctx, "Start UpdateShort")
defer span.End()

if _, err := url.ParseRequestURI(req.FullURL); err != nil {
return model.NewError(model.Validation, err.Error())
}

return ss.ShortRepo.UpdateFullURLByID(ctx, req)
}

func (ss *ShortServiceImpl) validateCreateShort(req *model.CreateShortRequest) error {
if _, err := url.ParseRequestURI(req.FullURL); err != nil {
return model.NewError(model.Validation, err.Error())
Expand Down
107 changes: 90 additions & 17 deletions shortener/pkg/api/v1/proto/shortener/shortener.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions user/api/v1/proto/shortener/shortener.proto
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,8 @@ message CreateShortenerMessage {
message UpdateVisitorCountMessage {
string short_url=1;
}

message UpdateShortenerMessage {
string id =1;
string full_url=2;
}
1 change: 1 addition & 0 deletions user/cmd/v1/example.env
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ AMQP_SERVER_URL=amqp://guest:guest@amqp:5672/
AMQP_QUEUE_CREATE_SHORTENER=create-shortener-queue
AMQP_QUEUE_UPDATE_VISITOR_COUNT=update-visitor-count-queue
AMQP_QUEUE_UPLOAD_AVATAR=upload-avatar-queue
AMQP_QUEUE_UPDATE_SHORTENER=update-shortener-queue

JWT_SECRET=secret
JWT_EXPIRE=7
Expand Down
Loading

0 comments on commit 3889e2c

Please sign in to comment.