From 33ce3b6255a6dfddc43375ceb7a0d28f17f9b130 Mon Sep 17 00:00:00 2001
From: Valera Shaitorov <shaitorov@perx.ru>
Date: Mon, 17 Apr 2023 19:00:13 +0700
Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?=
 =?UTF-8?q?=D0=BD=D1=8B=20Organizations=20middlewares?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../middleware/caching_middleware.go          |  62 +++++
 .../middleware/caching_middleware_test.go     | 119 ++++++++++
 .../middleware/error_logging_middleware.go    |  81 +++++++
 .../middleware/logging_middleware.go          | 215 ++++++++++++++++++
 pkg/organizations/middleware/middleware.go    |  28 +++
 .../middleware/recovering_middleware.go       |  92 ++++++++
 6 files changed, 597 insertions(+)
 create mode 100644 pkg/organizations/middleware/caching_middleware.go
 create mode 100644 pkg/organizations/middleware/caching_middleware_test.go
 create mode 100644 pkg/organizations/middleware/error_logging_middleware.go
 create mode 100644 pkg/organizations/middleware/logging_middleware.go
 create mode 100644 pkg/organizations/middleware/middleware.go
 create mode 100644 pkg/organizations/middleware/recovering_middleware.go

diff --git a/pkg/organizations/middleware/caching_middleware.go b/pkg/organizations/middleware/caching_middleware.go
new file mode 100644
index 00000000..2017c991
--- /dev/null
+++ b/pkg/organizations/middleware/caching_middleware.go
@@ -0,0 +1,62 @@
+package service
+
+import (
+	"context"
+
+	"git.perx.ru/perxis/perxis-go/pkg/cache"
+	"git.perx.ru/perxis/perxis-go/pkg/options"
+	service "git.perx.ru/perxis/perxis-go/pkg/organizations"
+)
+
+func CachingMiddleware(cache *cache.Cache) Middleware {
+	return func(next service.Organizations) service.Organizations {
+		return &cachingMiddleware{
+			cache: cache,
+			next:  next,
+		}
+	}
+}
+
+type cachingMiddleware struct {
+	cache *cache.Cache
+	next  service.Organizations
+}
+
+func (m cachingMiddleware) Create(ctx context.Context, org *service.Organization) (organization *service.Organization, err error) {
+	return m.next.Create(ctx, org)
+}
+
+func (m cachingMiddleware) Get(ctx context.Context, orgId string) (organization *service.Organization, err error) {
+
+	value, e := m.cache.Get(orgId)
+	if e == nil {
+		return value.(*service.Organization), err
+	}
+	organization, err = m.next.Get(ctx, orgId)
+	if err == nil {
+		m.cache.Set(orgId, organization)
+	}
+	return organization, err
+}
+
+func (m cachingMiddleware) Update(ctx context.Context, org *service.Organization) (err error) {
+
+	err = m.next.Update(ctx, org)
+	if err == nil {
+		m.cache.Remove(org.ID)
+	}
+	return err
+}
+
+func (m cachingMiddleware) Delete(ctx context.Context, orgId string) (err error) {
+
+	err = m.next.Delete(ctx, orgId)
+	if err == nil {
+		m.cache.Remove(orgId)
+	}
+	return err
+}
+
+func (m cachingMiddleware) Find(ctx context.Context, filter *service.Filter, opts *options.FindOptions) (organizations []*service.Organization, total int, err error) {
+	return m.next.Find(ctx, filter, opts)
+}
diff --git a/pkg/organizations/middleware/caching_middleware_test.go b/pkg/organizations/middleware/caching_middleware_test.go
new file mode 100644
index 00000000..59248ded
--- /dev/null
+++ b/pkg/organizations/middleware/caching_middleware_test.go
@@ -0,0 +1,119 @@
+package service
+
+import (
+	"context"
+	"testing"
+	"time"
+
+	"git.perx.ru/perxis/perxis-go/pkg/cache"
+	"git.perx.ru/perxis/perxis-go/pkg/errors"
+	"git.perx.ru/perxis/perxis-go/pkg/organizations"
+	mocksorgs "git.perx.ru/perxis/perxis-go/pkg/organizations/mocks"
+	"github.com/stretchr/testify/assert"
+	"github.com/stretchr/testify/mock"
+	"github.com/stretchr/testify/require"
+)
+
+func TestOrganizationsCache(t *testing.T) {
+
+	const (
+		orgId = "orgId"
+		size  = 5
+		ttl   = 20 * time.Millisecond
+	)
+
+	errNotFound := errors.NotFound(errors.New("not found"))
+
+	ctx := context.Background()
+
+	t.Run("Get from cache", func(t *testing.T) {
+		orgs := &mocksorgs.Organizations{}
+		svc := CachingMiddleware(cache.NewCache(size, ttl))(orgs)
+
+		orgs.On("Get", mock.Anything, orgId).Return(&organizations.Organization{ID: orgId, Name: "Organization"}, nil).Once()
+
+		v1, err := svc.Get(ctx, orgId)
+		require.NoError(t, err)
+
+		v2, err := svc.Get(ctx, orgId)
+		require.NoError(t, err)
+		assert.Same(t, v1, v2, "Ожидается получение объекта из кэша.")
+
+		orgs.AssertExpectations(t)
+	})
+
+	t.Run("Invalidate cache", func(t *testing.T) {
+		t.Run("After Update", func(t *testing.T) {
+			orgs := &mocksorgs.Organizations{}
+			svc := CachingMiddleware(cache.NewCache(size, ttl))(orgs)
+
+			orgs.On("Get", mock.Anything, orgId).Return(&organizations.Organization{ID: orgId, Name: "Organization"}, nil).Once()
+
+			v1, err := svc.Get(ctx, orgId)
+			require.NoError(t, err)
+
+			v2, err := svc.Get(ctx, orgId)
+			require.NoError(t, err)
+			assert.Same(t, v1, v2, "Ожидается получение объекта из кэша.")
+
+			orgs.On("Update", mock.Anything, mock.Anything).Return(nil).Once()
+			err = svc.Update(ctx, &organizations.Organization{ID: orgId, Name: "OrganizationUPD"})
+
+			orgs.On("Get", mock.Anything, orgId).Return(&organizations.Organization{ID: orgId, Name: "OrganizationUPD"}, nil).Once()
+
+			v3, err := svc.Get(ctx, orgId)
+			require.NoError(t, err)
+			assert.NotSame(t, v2, v3, "Ожидается удаление объекта из кэша после обновления и получение заново из сервиса.")
+
+			orgs.AssertExpectations(t)
+		})
+
+		t.Run("After Delete", func(t *testing.T) {
+			orgs := &mocksorgs.Organizations{}
+			svc := CachingMiddleware(cache.NewCache(size, ttl))(orgs)
+
+			orgs.On("Get", mock.Anything, orgId).Return(&organizations.Organization{ID: orgId, Name: "Organization"}, nil).Once()
+
+			v1, err := svc.Get(ctx, orgId)
+			require.NoError(t, err)
+
+			v2, err := svc.Get(ctx, orgId)
+			require.NoError(t, err)
+			assert.Same(t, v1, v2, "Ожидается получение объекта из кэша.")
+
+			orgs.On("Delete", mock.Anything, mock.Anything).Return(nil).Once()
+			err = svc.Delete(ctx, orgId)
+
+			orgs.On("Get", mock.Anything, orgId).Return(nil, errNotFound).Once()
+
+			_, err = svc.Get(ctx, orgId)
+			require.Error(t, err)
+			assert.EqualError(t, err, "not found", "Ожидается удаление объекта из кэша после удаления из хранилища и получение ошибки от сервиса.")
+
+		})
+
+		t.Run("After TTL expired", func(t *testing.T) {
+			orgs := &mocksorgs.Organizations{}
+			svc := CachingMiddleware(cache.NewCache(size, ttl))(orgs)
+
+			orgs.On("Get", mock.Anything, orgId).Return(&organizations.Organization{ID: orgId, Name: "Organization"}, nil).Once()
+
+			v1, err := svc.Get(ctx, orgId)
+			require.NoError(t, err)
+
+			v2, err := svc.Get(ctx, orgId)
+			require.NoError(t, err)
+			assert.Same(t, v1, v2, "Ожидается получение объекта из кэша.")
+
+			time.Sleep(2 * ttl)
+
+			orgs.On("Get", mock.Anything, orgId).Return(&organizations.Organization{ID: orgId, Name: "Organization"}, nil).Once()
+
+			v3, err := svc.Get(ctx, orgId)
+			require.NoError(t, err)
+			assert.NotSame(t, v2, v3, "Ожидается удаление объекта из кэша и получение заново из сервиса.")
+
+			orgs.AssertExpectations(t)
+		})
+	})
+}
diff --git a/pkg/organizations/middleware/error_logging_middleware.go b/pkg/organizations/middleware/error_logging_middleware.go
new file mode 100644
index 00000000..5c2612fc
--- /dev/null
+++ b/pkg/organizations/middleware/error_logging_middleware.go
@@ -0,0 +1,81 @@
+// Code generated by gowrap. DO NOT EDIT.
+// template: ../../../assets/templates/middleware/error_log
+// gowrap: http://github.com/hexdigest/gowrap
+
+package service
+
+//go:generate gowrap gen -p git.perx.ru/perxis/perxis-go/pkg/organizations -i Organizations -t ../../../assets/templates/middleware/error_log -o error_logging_middleware.go -l ""
+
+import (
+	"context"
+
+	"git.perx.ru/perxis/perxis-go/pkg/options"
+	"git.perx.ru/perxis/perxis-go/pkg/organizations"
+	"go.uber.org/zap"
+)
+
+// errorLoggingMiddleware implements organizations.Organizations that is instrumented with logging
+type errorLoggingMiddleware struct {
+	logger *zap.Logger
+	next   organizations.Organizations
+}
+
+// ErrorLoggingMiddleware instruments an implementation of the organizations.Organizations with simple logging
+func ErrorLoggingMiddleware(logger *zap.Logger) Middleware {
+	return func(next organizations.Organizations) organizations.Organizations {
+		return &errorLoggingMiddleware{
+			next:   next,
+			logger: logger,
+		}
+	}
+}
+
+func (m *errorLoggingMiddleware) Create(ctx context.Context, org *organizations.Organization) (created *organizations.Organization, err error) {
+	logger := m.logger
+	defer func() {
+		if err != nil {
+			logger.Warn("response error", zap.Error(err))
+		}
+	}()
+	return m.next.Create(ctx, org)
+}
+
+func (m *errorLoggingMiddleware) Delete(ctx context.Context, orgId string) (err error) {
+	logger := m.logger
+	defer func() {
+		if err != nil {
+			logger.Warn("response error", zap.Error(err))
+		}
+	}()
+	return m.next.Delete(ctx, orgId)
+}
+
+func (m *errorLoggingMiddleware) Find(ctx context.Context, filter *organizations.Filter, opts *options.FindOptions) (orgs []*organizations.Organization, total int, err error) {
+	logger := m.logger
+	defer func() {
+		if err != nil {
+			logger.Warn("response error", zap.Error(err))
+		}
+	}()
+	return m.next.Find(ctx, filter, opts)
+}
+
+func (m *errorLoggingMiddleware) Get(ctx context.Context, orgId string) (org *organizations.Organization, err error) {
+	logger := m.logger
+	defer func() {
+		if err != nil {
+			logger.Warn("response error", zap.Error(err))
+		}
+	}()
+	return m.next.Get(ctx, orgId)
+}
+
+func (m *errorLoggingMiddleware) Update(ctx context.Context, org *organizations.Organization) (err error) {
+	logger := m.logger
+	defer func() {
+		if err != nil {
+			logger.Warn("response error", zap.Error(err))
+		}
+	}()
+	return m.next.Update(ctx, org)
+}
diff --git a/pkg/organizations/middleware/logging_middleware.go b/pkg/organizations/middleware/logging_middleware.go
new file mode 100644
index 00000000..ac446d73
--- /dev/null
+++ b/pkg/organizations/middleware/logging_middleware.go
@@ -0,0 +1,215 @@
+// Code generated by gowrap. DO NOT EDIT.
+// template: ../../../assets/templates/middleware/access_log
+// gowrap: http://github.com/hexdigest/gowrap
+
+package service
+
+//go:generate gowrap gen -p git.perx.ru/perxis/perxis-go/pkg/organizations -i Organizations -t ../../../assets/templates/middleware/access_log -o logging_middleware.go -l ""
+
+import (
+	"context"
+	"fmt"
+	"time"
+
+	"git.perx.ru/perxis/perxis-go/pkg/auth"
+	"git.perx.ru/perxis/perxis-go/pkg/options"
+	"git.perx.ru/perxis/perxis-go/pkg/organizations"
+	"go.uber.org/zap"
+	"go.uber.org/zap/zapcore"
+)
+
+// loggingMiddleware implements organizations.Organizations that is instrumented with logging
+type loggingMiddleware struct {
+	logger *zap.Logger
+	next   organizations.Organizations
+}
+
+// LoggingMiddleware instruments an implementation of the organizations.Organizations with simple logging
+func LoggingMiddleware(logger *zap.Logger) Middleware {
+	return func(next organizations.Organizations) organizations.Organizations {
+		return &loggingMiddleware{
+			next:   next,
+			logger: logger,
+		}
+	}
+}
+
+func (m *loggingMiddleware) Create(ctx context.Context, org *organizations.Organization) (created *organizations.Organization, err error) {
+	begin := time.Now()
+	var fields []zapcore.Field
+	for k, v := range map[string]interface{}{
+		"ctx": ctx,
+		"org": org} {
+		if k == "ctx" {
+			fields = append(fields, zap.String("principal", fmt.Sprint(auth.GetPrincipal(ctx))))
+			continue
+		}
+		fields = append(fields, zap.Reflect(k, v))
+	}
+
+	m.logger.Debug("Create.Request", fields...)
+
+	created, err = m.next.Create(ctx, org)
+
+	fields = []zapcore.Field{
+		zap.Duration("time", time.Since(begin)),
+		zap.Error(err),
+	}
+
+	for k, v := range map[string]interface{}{
+		"created": created,
+		"err":     err} {
+		if k == "err" {
+			continue
+		}
+		fields = append(fields, zap.Reflect(k, v))
+	}
+
+	m.logger.Debug("Create.Response", fields...)
+
+	return created, err
+}
+
+func (m *loggingMiddleware) Delete(ctx context.Context, orgId string) (err error) {
+	begin := time.Now()
+	var fields []zapcore.Field
+	for k, v := range map[string]interface{}{
+		"ctx":   ctx,
+		"orgId": orgId} {
+		if k == "ctx" {
+			fields = append(fields, zap.String("principal", fmt.Sprint(auth.GetPrincipal(ctx))))
+			continue
+		}
+		fields = append(fields, zap.Reflect(k, v))
+	}
+
+	m.logger.Debug("Delete.Request", fields...)
+
+	err = m.next.Delete(ctx, orgId)
+
+	fields = []zapcore.Field{
+		zap.Duration("time", time.Since(begin)),
+		zap.Error(err),
+	}
+
+	for k, v := range map[string]interface{}{
+		"err": err} {
+		if k == "err" {
+			continue
+		}
+		fields = append(fields, zap.Reflect(k, v))
+	}
+
+	m.logger.Debug("Delete.Response", fields...)
+
+	return err
+}
+
+func (m *loggingMiddleware) Find(ctx context.Context, filter *organizations.Filter, opts *options.FindOptions) (orgs []*organizations.Organization, total int, err error) {
+	begin := time.Now()
+	var fields []zapcore.Field
+	for k, v := range map[string]interface{}{
+		"ctx":    ctx,
+		"filter": filter,
+		"opts":   opts} {
+		if k == "ctx" {
+			fields = append(fields, zap.String("principal", fmt.Sprint(auth.GetPrincipal(ctx))))
+			continue
+		}
+		fields = append(fields, zap.Reflect(k, v))
+	}
+
+	m.logger.Debug("Find.Request", fields...)
+
+	orgs, total, err = m.next.Find(ctx, filter, opts)
+
+	fields = []zapcore.Field{
+		zap.Duration("time", time.Since(begin)),
+		zap.Error(err),
+	}
+
+	for k, v := range map[string]interface{}{
+		"orgs":  orgs,
+		"total": total,
+		"err":   err} {
+		if k == "err" {
+			continue
+		}
+		fields = append(fields, zap.Reflect(k, v))
+	}
+
+	m.logger.Debug("Find.Response", fields...)
+
+	return orgs, total, err
+}
+
+func (m *loggingMiddleware) Get(ctx context.Context, orgId string) (org *organizations.Organization, err error) {
+	begin := time.Now()
+	var fields []zapcore.Field
+	for k, v := range map[string]interface{}{
+		"ctx":   ctx,
+		"orgId": orgId} {
+		if k == "ctx" {
+			fields = append(fields, zap.String("principal", fmt.Sprint(auth.GetPrincipal(ctx))))
+			continue
+		}
+		fields = append(fields, zap.Reflect(k, v))
+	}
+
+	m.logger.Debug("Get.Request", fields...)
+
+	org, err = m.next.Get(ctx, orgId)
+
+	fields = []zapcore.Field{
+		zap.Duration("time", time.Since(begin)),
+		zap.Error(err),
+	}
+
+	for k, v := range map[string]interface{}{
+		"org": org,
+		"err": err} {
+		if k == "err" {
+			continue
+		}
+		fields = append(fields, zap.Reflect(k, v))
+	}
+
+	m.logger.Debug("Get.Response", fields...)
+
+	return org, err
+}
+
+func (m *loggingMiddleware) Update(ctx context.Context, org *organizations.Organization) (err error) {
+	begin := time.Now()
+	var fields []zapcore.Field
+	for k, v := range map[string]interface{}{
+		"ctx": ctx,
+		"org": org} {
+		if k == "ctx" {
+			fields = append(fields, zap.String("principal", fmt.Sprint(auth.GetPrincipal(ctx))))
+			continue
+		}
+		fields = append(fields, zap.Reflect(k, v))
+	}
+
+	m.logger.Debug("Update.Request", fields...)
+
+	err = m.next.Update(ctx, org)
+
+	fields = []zapcore.Field{
+		zap.Duration("time", time.Since(begin)),
+		zap.Error(err),
+	}
+
+	for k, v := range map[string]interface{}{
+		"err": err} {
+		if k == "err" {
+			continue
+		}
+		fields = append(fields, zap.Reflect(k, v))
+	}
+
+	m.logger.Debug("Update.Response", fields...)
+
+	return err
+}
diff --git a/pkg/organizations/middleware/middleware.go b/pkg/organizations/middleware/middleware.go
new file mode 100644
index 00000000..fe3c3d64
--- /dev/null
+++ b/pkg/organizations/middleware/middleware.go
@@ -0,0 +1,28 @@
+// Code generated by gowrap. DO NOT EDIT.
+// template: ../../../assets/templates/middleware/middleware
+// gowrap: http://github.com/hexdigest/gowrap
+
+package service
+
+//go:generate gowrap gen -p git.perx.ru/perxis/perxis-go/pkg/organizations -i Organizations -t ../../../assets/templates/middleware/middleware -o middleware.go -l ""
+
+import (
+	"git.perx.ru/perxis/perxis-go/pkg/organizations"
+	"go.uber.org/zap"
+)
+
+type Middleware func(organizations.Organizations) organizations.Organizations
+
+func WithLog(s organizations.Organizations, logger *zap.Logger, log_access bool) organizations.Organizations {
+	if logger == nil {
+		logger = zap.NewNop()
+	}
+
+	logger = logger.Named("Organizations")
+	s = ErrorLoggingMiddleware(logger)(s)
+	if log_access {
+		s = LoggingMiddleware(logger)(s)
+	}
+	s = RecoveringMiddleware(logger)(s)
+	return s
+}
diff --git a/pkg/organizations/middleware/recovering_middleware.go b/pkg/organizations/middleware/recovering_middleware.go
new file mode 100644
index 00000000..35f3a6c5
--- /dev/null
+++ b/pkg/organizations/middleware/recovering_middleware.go
@@ -0,0 +1,92 @@
+// Code generated by gowrap. DO NOT EDIT.
+// template: ../../../assets/templates/middleware/recovery
+// gowrap: http://github.com/hexdigest/gowrap
+
+package service
+
+//go:generate gowrap gen -p git.perx.ru/perxis/perxis-go/pkg/organizations -i Organizations -t ../../../assets/templates/middleware/recovery -o recovering_middleware.go -l ""
+
+import (
+	"context"
+	"fmt"
+
+	"git.perx.ru/perxis/perxis-go/pkg/options"
+	"git.perx.ru/perxis/perxis-go/pkg/organizations"
+	"go.uber.org/zap"
+)
+
+// recoveringMiddleware implements organizations.Organizations that is instrumented with logging
+type recoveringMiddleware struct {
+	logger *zap.Logger
+	next   organizations.Organizations
+}
+
+// RecoveringMiddleware instruments an implementation of the organizations.Organizations with simple logging
+func RecoveringMiddleware(logger *zap.Logger) Middleware {
+	return func(next organizations.Organizations) organizations.Organizations {
+		return &recoveringMiddleware{
+			next:   next,
+			logger: logger,
+		}
+	}
+}
+
+func (m *recoveringMiddleware) Create(ctx context.Context, org *organizations.Organization) (created *organizations.Organization, err error) {
+	logger := m.logger
+	defer func() {
+		if r := recover(); r != nil {
+			logger.Error("panic", zap.Error(fmt.Errorf("%v", r)))
+			err = fmt.Errorf("%v", r)
+		}
+	}()
+
+	return m.next.Create(ctx, org)
+}
+
+func (m *recoveringMiddleware) Delete(ctx context.Context, orgId string) (err error) {
+	logger := m.logger
+	defer func() {
+		if r := recover(); r != nil {
+			logger.Error("panic", zap.Error(fmt.Errorf("%v", r)))
+			err = fmt.Errorf("%v", r)
+		}
+	}()
+
+	return m.next.Delete(ctx, orgId)
+}
+
+func (m *recoveringMiddleware) Find(ctx context.Context, filter *organizations.Filter, opts *options.FindOptions) (orgs []*organizations.Organization, total int, err error) {
+	logger := m.logger
+	defer func() {
+		if r := recover(); r != nil {
+			logger.Error("panic", zap.Error(fmt.Errorf("%v", r)))
+			err = fmt.Errorf("%v", r)
+		}
+	}()
+
+	return m.next.Find(ctx, filter, opts)
+}
+
+func (m *recoveringMiddleware) Get(ctx context.Context, orgId string) (org *organizations.Organization, err error) {
+	logger := m.logger
+	defer func() {
+		if r := recover(); r != nil {
+			logger.Error("panic", zap.Error(fmt.Errorf("%v", r)))
+			err = fmt.Errorf("%v", r)
+		}
+	}()
+
+	return m.next.Get(ctx, orgId)
+}
+
+func (m *recoveringMiddleware) Update(ctx context.Context, org *organizations.Organization) (err error) {
+	logger := m.logger
+	defer func() {
+		if r := recover(); r != nil {
+			logger.Error("panic", zap.Error(fmt.Errorf("%v", r)))
+			err = fmt.Errorf("%v", r)
+		}
+	}()
+
+	return m.next.Update(ctx, org)
+}
-- 
GitLab