From 649f807eedbbe7479658689f871a784b4358a4f0 Mon Sep 17 00:00:00 2001 From: Auke Bakker Date: Tue, 30 Jan 2024 13:48:44 +0100 Subject: [PATCH] Move rules into rules package and other smaller code cleanups --- cmd/main.go | 6 +++--- internal/app/config/config.go | 12 +++++------ internal/app/config/config_test.go | 12 +++++------ internal/business/protect/protect.go | 21 ++++++++----------- .../business/{ => rules}/aliases/aliases.go | 0 .../{ => rules}/aliases/aliases_test.go | 0 internal/business/{ => rules}/batch/batch.go | 0 .../business/{ => rules}/batch/batch_test.go | 0 .../block_field_suggestions.go | 0 .../block_field_suggestions_test.go | 0 .../{ => rules}/enforce_post/enforce_post.go | 0 .../enforce_post/enforce_post_test.go | 0 .../{ => rules}/max_depth/max_depth.go | 0 .../{ => rules}/max_depth/max_depth_test.go | 0 .../business/{ => rules}/tokens/tokens.go | 0 .../{ => rules}/tokens/tokens_test.go | 0 internal/http/proxy/proxy.go | 2 +- internal/http/proxy/proxy_test.go | 2 +- 18 files changed, 26 insertions(+), 29 deletions(-) rename internal/business/{ => rules}/aliases/aliases.go (100%) rename internal/business/{ => rules}/aliases/aliases_test.go (100%) rename internal/business/{ => rules}/batch/batch.go (100%) rename internal/business/{ => rules}/batch/batch_test.go (100%) rename internal/business/{ => rules}/block_field_suggestions/block_field_suggestions.go (100%) rename internal/business/{ => rules}/block_field_suggestions/block_field_suggestions_test.go (100%) rename internal/business/{ => rules}/enforce_post/enforce_post.go (100%) rename internal/business/{ => rules}/enforce_post/enforce_post_test.go (100%) rename internal/business/{ => rules}/max_depth/max_depth.go (100%) rename internal/business/{ => rules}/max_depth/max_depth_test.go (100%) rename internal/business/{ => rules}/tokens/tokens.go (100%) rename internal/business/{ => rules}/tokens/tokens_test.go (100%) diff --git a/cmd/main.go b/cmd/main.go index 49ca5a4..2fc5450 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -7,9 +7,9 @@ import ( "fmt" "github.com/ardanlabs/conf/v3" "github.com/ldebruijn/graphql-protect/internal/app/config" - "github.com/ldebruijn/graphql-protect/internal/business/block_field_suggestions" "github.com/ldebruijn/graphql-protect/internal/business/persisted_operations" "github.com/ldebruijn/graphql-protect/internal/business/protect" + "github.com/ldebruijn/graphql-protect/internal/business/rules/block_field_suggestions" "github.com/ldebruijn/graphql-protect/internal/business/schema" "github.com/ldebruijn/graphql-protect/internal/http/middleware" "github.com/ldebruijn/graphql-protect/internal/http/proxy" @@ -114,7 +114,7 @@ func run(log *slog.Logger, cfg *config.Config, shutdown chan os.Signal) error { mux := http.NewServeMux() - mid := ProtectMiddlewareChain(log) + mid := protectMiddlewareChain(log) mux.Handle("/metrics", promhttp.Handler()) mux.Handle("/internal/healthz/readiness", readiness.NewReadinessHandler()) @@ -158,7 +158,7 @@ func run(log *slog.Logger, cfg *config.Config, shutdown chan os.Signal) error { return nil } -func ProtectMiddlewareChain(log *slog.Logger) func(next http.Handler) http.Handler { +func protectMiddlewareChain(log *slog.Logger) func(next http.Handler) http.Handler { rec := middleware.Recover(log) httpInstrumentation := middleware.RequestMetricMiddleware() diff --git a/internal/app/config/config.go b/internal/app/config/config.go index 1e6d7ef..db69110 100644 --- a/internal/app/config/config.go +++ b/internal/app/config/config.go @@ -5,14 +5,14 @@ import ( "fmt" "github.com/ardanlabs/conf/v3" "github.com/ardanlabs/conf/v3/yaml" - "github.com/ldebruijn/graphql-protect/internal/business/aliases" - "github.com/ldebruijn/graphql-protect/internal/business/batch" - "github.com/ldebruijn/graphql-protect/internal/business/block_field_suggestions" - "github.com/ldebruijn/graphql-protect/internal/business/enforce_post" - "github.com/ldebruijn/graphql-protect/internal/business/max_depth" "github.com/ldebruijn/graphql-protect/internal/business/persisted_operations" + "github.com/ldebruijn/graphql-protect/internal/business/rules/aliases" + "github.com/ldebruijn/graphql-protect/internal/business/rules/batch" + "github.com/ldebruijn/graphql-protect/internal/business/rules/block_field_suggestions" + "github.com/ldebruijn/graphql-protect/internal/business/rules/enforce_post" + "github.com/ldebruijn/graphql-protect/internal/business/rules/max_depth" + "github.com/ldebruijn/graphql-protect/internal/business/rules/tokens" "github.com/ldebruijn/graphql-protect/internal/business/schema" - "github.com/ldebruijn/graphql-protect/internal/business/tokens" "github.com/ldebruijn/graphql-protect/internal/http/proxy" "os" "time" diff --git a/internal/app/config/config_test.go b/internal/app/config/config_test.go index c5b8618..c2b5272 100644 --- a/internal/app/config/config_test.go +++ b/internal/app/config/config_test.go @@ -1,14 +1,14 @@ package config import ( - "github.com/ldebruijn/graphql-protect/internal/business/aliases" - "github.com/ldebruijn/graphql-protect/internal/business/batch" - "github.com/ldebruijn/graphql-protect/internal/business/block_field_suggestions" - "github.com/ldebruijn/graphql-protect/internal/business/enforce_post" - "github.com/ldebruijn/graphql-protect/internal/business/max_depth" "github.com/ldebruijn/graphql-protect/internal/business/persisted_operations" + "github.com/ldebruijn/graphql-protect/internal/business/rules/aliases" + "github.com/ldebruijn/graphql-protect/internal/business/rules/batch" + "github.com/ldebruijn/graphql-protect/internal/business/rules/block_field_suggestions" + "github.com/ldebruijn/graphql-protect/internal/business/rules/enforce_post" + "github.com/ldebruijn/graphql-protect/internal/business/rules/max_depth" + "github.com/ldebruijn/graphql-protect/internal/business/rules/tokens" "github.com/ldebruijn/graphql-protect/internal/business/schema" - "github.com/ldebruijn/graphql-protect/internal/business/tokens" "github.com/ldebruijn/graphql-protect/internal/http/proxy" "github.com/stretchr/testify/assert" "gopkg.in/yaml.v3" diff --git a/internal/business/protect/protect.go b/internal/business/protect/protect.go index 4f6c770..44a2bde 100644 --- a/internal/business/protect/protect.go +++ b/internal/business/protect/protect.go @@ -4,14 +4,14 @@ import ( "encoding/json" "errors" "github.com/ldebruijn/graphql-protect/internal/app/config" - "github.com/ldebruijn/graphql-protect/internal/business/aliases" - "github.com/ldebruijn/graphql-protect/internal/business/batch" - "github.com/ldebruijn/graphql-protect/internal/business/enforce_post" "github.com/ldebruijn/graphql-protect/internal/business/gql" - "github.com/ldebruijn/graphql-protect/internal/business/max_depth" "github.com/ldebruijn/graphql-protect/internal/business/persisted_operations" + "github.com/ldebruijn/graphql-protect/internal/business/rules/aliases" + "github.com/ldebruijn/graphql-protect/internal/business/rules/batch" + "github.com/ldebruijn/graphql-protect/internal/business/rules/enforce_post" + "github.com/ldebruijn/graphql-protect/internal/business/rules/max_depth" + "github.com/ldebruijn/graphql-protect/internal/business/rules/tokens" "github.com/ldebruijn/graphql-protect/internal/business/schema" - "github.com/ldebruijn/graphql-protect/internal/business/tokens" "github.com/vektah/gqlparser/v2/ast" "github.com/vektah/gqlparser/v2/gqlerror" "github.com/vektah/gqlparser/v2/parser" @@ -104,13 +104,10 @@ func (p *GraphQLProtect) validateRequest(r *http.Request) gqlerror.List { return errs } - // only process the rest if no error yet - if err == nil { - for _, data := range payload { - validationErrors := p.validateQuery(data) - if len(validationErrors) > 0 { - errs = append(errs, validationErrors...) - } + for _, data := range payload { + validationErrors := p.validateQuery(data) + if len(validationErrors) > 0 { + errs = append(errs, validationErrors...) } } diff --git a/internal/business/aliases/aliases.go b/internal/business/rules/aliases/aliases.go similarity index 100% rename from internal/business/aliases/aliases.go rename to internal/business/rules/aliases/aliases.go diff --git a/internal/business/aliases/aliases_test.go b/internal/business/rules/aliases/aliases_test.go similarity index 100% rename from internal/business/aliases/aliases_test.go rename to internal/business/rules/aliases/aliases_test.go diff --git a/internal/business/batch/batch.go b/internal/business/rules/batch/batch.go similarity index 100% rename from internal/business/batch/batch.go rename to internal/business/rules/batch/batch.go diff --git a/internal/business/batch/batch_test.go b/internal/business/rules/batch/batch_test.go similarity index 100% rename from internal/business/batch/batch_test.go rename to internal/business/rules/batch/batch_test.go diff --git a/internal/business/block_field_suggestions/block_field_suggestions.go b/internal/business/rules/block_field_suggestions/block_field_suggestions.go similarity index 100% rename from internal/business/block_field_suggestions/block_field_suggestions.go rename to internal/business/rules/block_field_suggestions/block_field_suggestions.go diff --git a/internal/business/block_field_suggestions/block_field_suggestions_test.go b/internal/business/rules/block_field_suggestions/block_field_suggestions_test.go similarity index 100% rename from internal/business/block_field_suggestions/block_field_suggestions_test.go rename to internal/business/rules/block_field_suggestions/block_field_suggestions_test.go diff --git a/internal/business/enforce_post/enforce_post.go b/internal/business/rules/enforce_post/enforce_post.go similarity index 100% rename from internal/business/enforce_post/enforce_post.go rename to internal/business/rules/enforce_post/enforce_post.go diff --git a/internal/business/enforce_post/enforce_post_test.go b/internal/business/rules/enforce_post/enforce_post_test.go similarity index 100% rename from internal/business/enforce_post/enforce_post_test.go rename to internal/business/rules/enforce_post/enforce_post_test.go diff --git a/internal/business/max_depth/max_depth.go b/internal/business/rules/max_depth/max_depth.go similarity index 100% rename from internal/business/max_depth/max_depth.go rename to internal/business/rules/max_depth/max_depth.go diff --git a/internal/business/max_depth/max_depth_test.go b/internal/business/rules/max_depth/max_depth_test.go similarity index 100% rename from internal/business/max_depth/max_depth_test.go rename to internal/business/rules/max_depth/max_depth_test.go diff --git a/internal/business/tokens/tokens.go b/internal/business/rules/tokens/tokens.go similarity index 100% rename from internal/business/tokens/tokens.go rename to internal/business/rules/tokens/tokens.go diff --git a/internal/business/tokens/tokens_test.go b/internal/business/rules/tokens/tokens_test.go similarity index 100% rename from internal/business/tokens/tokens_test.go rename to internal/business/rules/tokens/tokens_test.go diff --git a/internal/http/proxy/proxy.go b/internal/http/proxy/proxy.go index 0408165..aea0870 100644 --- a/internal/http/proxy/proxy.go +++ b/internal/http/proxy/proxy.go @@ -3,7 +3,7 @@ package proxy import ( "bytes" "encoding/json" - "github.com/ldebruijn/graphql-protect/internal/business/block_field_suggestions" + "github.com/ldebruijn/graphql-protect/internal/business/rules/block_field_suggestions" "io" "net" "net/http" diff --git a/internal/http/proxy/proxy_test.go b/internal/http/proxy/proxy_test.go index 90efda9..bc7189d 100644 --- a/internal/http/proxy/proxy_test.go +++ b/internal/http/proxy/proxy_test.go @@ -1,7 +1,7 @@ package proxy import ( - "github.com/ldebruijn/graphql-protect/internal/business/block_field_suggestions" + "github.com/ldebruijn/graphql-protect/internal/business/rules/block_field_suggestions" "github.com/stretchr/testify/assert" "io" "net/http"