Skip to content

Commit 6b2e88d

Browse files
refactor: move common code into cmdhelpers/ (#5121)
* chore: Move common code into cmdhelper/ Signed-off-by: Thomas Poignant <thomas.poignant@gofeatureflag.org> * chore: simplify nil check in ZapLogger call * fix: use the correct middleware Signed-off-by: Thomas Poignant <thomas.poignant@gofeatureflag.org> --------- Signed-off-by: Thomas Poignant <thomas.poignant@gofeatureflag.org>
1 parent 9bc7da0 commit 6b2e88d

17 files changed

Lines changed: 76 additions & 49 deletions

File tree

cmd/cli/evaluate/evaluate_cmd_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ import (
77
"github.com/stretchr/testify/assert"
88
"github.com/stretchr/testify/require"
99
"github.com/thomaspoignant/go-feature-flag/cmd/cli/evaluate"
10-
"github.com/thomaspoignant/go-feature-flag/cmd/relayproxy/helper"
1110
)
1211

12+
const XAPIKeyHeader = "X-API-Key"
13+
1314
func TestCmdEvaluate(t *testing.T) {
1415
tests := []struct {
1516
name string
@@ -123,9 +124,9 @@ func TestCmdEvaluate(t *testing.T) {
123124
"--header",
124125
"Content-Type: application/json",
125126
"--header",
126-
helper.XAPIKeyHeader + ": 123456",
127+
XAPIKeyHeader + ": 123456",
127128
"--header",
128-
helper.XAPIKeyHeader + ": 654321",
129+
XAPIKeyHeader + ": 654321",
129130
"--flag",
130131
"test-flag",
131132
"--ctx",

cmd/editor/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77
echoadapter "github.com/awslabs/aws-lambda-go-api-proxy/echo"
88
"github.com/labstack/echo/v4"
99
"github.com/labstack/echo/v4/middleware"
10-
custommiddleware "github.com/thomaspoignant/go-feature-flag/cmd/relayproxy/api/middleware"
11-
"github.com/thomaspoignant/go-feature-flag/cmd/relayproxy/log"
10+
custommiddleware "github.com/thomaspoignant/go-feature-flag/cmdhelpers/api/middleware"
11+
"github.com/thomaspoignant/go-feature-flag/cmdhelpers/log"
1212
"github.com/thomaspoignant/go-feature-flag/modules/core/dto"
1313
"github.com/thomaspoignant/go-feature-flag/modules/core/flag"
1414
"github.com/thomaspoignant/go-feature-flag/modules/core/model"
@@ -24,7 +24,7 @@ func main() {
2424
defer func() { _ = logger.ZapLogger.Sync() }()
2525

2626
e := echo.New()
27-
e.Use(custommiddleware.ZapLogger(logger.ZapLogger, nil))
27+
e.Use(custommiddleware.ZapLogger(logger.ZapLogger, false))
2828
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
2929
AllowOrigins: []string{
3030
"http://gofeatureflag.org",

cmd/relayproxy/api/opentelemetry/otel_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"github.com/stretchr/testify/assert"
1313
"github.com/stretchr/testify/require"
1414
"github.com/thomaspoignant/go-feature-flag/cmd/relayproxy/config"
15-
"github.com/thomaspoignant/go-feature-flag/cmd/relayproxy/log"
15+
"github.com/thomaspoignant/go-feature-flag/cmdhelpers/log"
1616
"go.opentelemetry.io/otel"
1717
"go.opentelemetry.io/otel/attribute"
1818
sdktrace "go.opentelemetry.io/otel/sdk/trace"

cmd/relayproxy/api/routes_monitoring.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
custommiddleware "github.com/thomaspoignant/go-feature-flag/cmd/relayproxy/api/middleware"
99
"github.com/thomaspoignant/go-feature-flag/cmd/relayproxy/controller"
1010
"github.com/thomaspoignant/go-feature-flag/cmd/relayproxy/metric"
11+
helpermiddleware "github.com/thomaspoignant/go-feature-flag/cmdhelpers/api/middleware"
1112
)
1213

1314
func (s *Server) addMonitoringRoutes() {
@@ -16,7 +17,7 @@ func (s *Server) addMonitoringRoutes() {
1617
s.monitoringEcho.HideBanner = true
1718
s.monitoringEcho.HidePort = true
1819
s.monitoringEcho.Debug = s.config.IsDebugEnabled()
19-
s.monitoringEcho.Use(custommiddleware.ZapLogger(s.zapLog, s.config))
20+
s.monitoringEcho.Use(helpermiddleware.ZapLogger(s.zapLog, s.config.IsDebugEnabled()))
2021
s.monitoringEcho.Use(middleware.CORSWithConfig(middleware.DefaultCORSConfig))
2122
s.apiEcho.Use(custommiddleware.VersionHeader(custommiddleware.VersionHeaderConfig{
2223
RelayProxyConfig: s.config,

cmd/relayproxy/api/server.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/thomaspoignant/go-feature-flag/cmd/relayproxy/metric"
2222
"github.com/thomaspoignant/go-feature-flag/cmd/relayproxy/ofrep"
2323
"github.com/thomaspoignant/go-feature-flag/cmd/relayproxy/service"
24+
helpermiddleware "github.com/thomaspoignant/go-feature-flag/cmdhelpers/api/middleware"
2425
"go.opentelemetry.io/contrib/instrumentation/github.com/labstack/echo/otelecho"
2526
"go.uber.org/zap"
2627
)
@@ -57,7 +58,7 @@ func (s *Server) initRoutes() {
5758
s.apiEcho.HidePort = true
5859
s.apiEcho.Debug = s.config.IsDebugEnabled()
5960
s.apiEcho.Use(otelecho.Middleware("go-feature-flag"))
60-
s.apiEcho.Use(custommiddleware.ZapLogger(s.zapLog, s.config))
61+
s.apiEcho.Use(helpermiddleware.ZapLogger(s.zapLog, s.config.IsDebugEnabled()))
6162
s.apiEcho.Use(middleware.BodyDumpWithConfig(middleware.BodyDumpConfig{
6263
Skipper: func(c echo.Context) bool {
6364
isSwagger := strings.HasPrefix(c.Request().URL.String(), "/swagger")

cmd/relayproxy/api/server_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ import (
1919
"github.com/stretchr/testify/require"
2020
"github.com/thomaspoignant/go-feature-flag/cmd/relayproxy/api"
2121
"github.com/thomaspoignant/go-feature-flag/cmd/relayproxy/config"
22-
"github.com/thomaspoignant/go-feature-flag/cmd/relayproxy/log"
2322
"github.com/thomaspoignant/go-feature-flag/cmd/relayproxy/metric"
2423
"github.com/thomaspoignant/go-feature-flag/cmd/relayproxy/service"
24+
"github.com/thomaspoignant/go-feature-flag/cmdhelpers/log"
2525
"github.com/thomaspoignant/go-feature-flag/cmdhelpers/retrieverconf"
2626
"github.com/thomaspoignant/go-feature-flag/notifier"
2727
"go.uber.org/zap"

cmd/relayproxy/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import (
1111
"github.com/thomaspoignant/go-feature-flag/cmd/relayproxy/api"
1212
"github.com/thomaspoignant/go-feature-flag/cmd/relayproxy/config"
1313
"github.com/thomaspoignant/go-feature-flag/cmd/relayproxy/docs"
14-
"github.com/thomaspoignant/go-feature-flag/cmd/relayproxy/log"
1514
"github.com/thomaspoignant/go-feature-flag/cmd/relayproxy/metric"
1615
"github.com/thomaspoignant/go-feature-flag/cmd/relayproxy/service"
16+
"github.com/thomaspoignant/go-feature-flag/cmdhelpers/log"
1717
"github.com/thomaspoignant/go-feature-flag/notifier"
1818
"go.uber.org/zap"
1919
)

cmd/wasm/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ require (
1919
github.com/diegoholiveira/jsonlogic/v3 v3.9.0 // indirect
2020
github.com/nikunjy/rules v1.5.0 // indirect
2121
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
22-
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 // indirect
22+
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 // indirect
2323
gopkg.in/yaml.v3 v3.0.1 // indirect
2424
)
2525

cmd/wasm/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu
2525
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
2626
github.com/thomaspoignant/go-feature-flag/modules/core v0.6.1 h1:9+SSjY/i4EXgNlGHqiNYXjPYB4q7SwWROvCtVwPvv2w=
2727
github.com/thomaspoignant/go-feature-flag/modules/core v0.6.1/go.mod h1:9Ps6MAWMG/SfwOufGVoX88y+SMk+b1Uhwn5txYusVF0=
28-
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 h1:vr/HnozRka3pE4EsMEg1lgkXJkTFJCVUX+S/ZT6wYzM=
29-
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842/go.mod h1:XtvwrStGgqGPLc4cjQfWqZHG1YFdYs6swckp8vpsjnc=
28+
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 h1:2dVuKD2vS7b0QIHQbpyTISPd0LeHDbnYEryqj5Q1ug8=
29+
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56/go.mod h1:M4RDyNAINzryxdtnbRXRL/OHtkFuWGRjvuhBJpk2IlY=
3030
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
3131
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
3232
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77

88
"github.com/labstack/echo/v4"
99
"github.com/labstack/echo/v4/middleware"
10-
"github.com/thomaspoignant/go-feature-flag/cmd/relayproxy/config"
1110
"go.uber.org/zap"
1211
"go.uber.org/zap/zapcore"
1312
)
@@ -31,10 +30,10 @@ func DebugSkipper(_ echo.Context) bool {
3130
}
3231

3332
// ZapLogger is a middleware and zap to provide an "access log" like logging for each request.
34-
func ZapLogger(log *zap.Logger, cfg *config.Config) echo.MiddlewareFunc {
33+
func ZapLogger(log *zap.Logger, isDebugEnabled bool) echo.MiddlewareFunc {
3534
// select the right skipper
3635
skipper := DefaultSkipper
37-
if cfg != nil && cfg.IsDebugEnabled() {
36+
if isDebugEnabled {
3837
skipper = DebugSkipper
3938
}
4039

0 commit comments

Comments
 (0)