Skip to content

Commit

Permalink
feat: Add middleware to add version in response headers (#1975)
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Poignant <[email protected]>
  • Loading branch information
thomaspoignant committed Aug 16, 2024
1 parent 2504b1f commit c819253
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
16 changes: 16 additions & 0 deletions cmd/relayproxy/api/middleware/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package middleware

import (
"github.com/labstack/echo/v4"
"github.com/thomaspoignant/go-feature-flag/cmd/relayproxy/config"
)

// VersionHeader is a middleware that adds the version of the relayproxy in the header
func VersionHeader(config *config.Config) echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
c.Response().Header().Set("X-GOFEATUREFLAG-VERSION", config.Version)
return next(c)
}
}
}
31 changes: 31 additions & 0 deletions cmd/relayproxy/api/middleware/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package middleware_test

import (
"github.com/labstack/echo/v4"
middleware2 "github.com/thomaspoignant/go-feature-flag/cmd/relayproxy/api/middleware"
"github.com/thomaspoignant/go-feature-flag/cmd/relayproxy/config"
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/assert"
)

func TestVersion(t *testing.T) {
e := echo.New()
req := httptest.NewRequest(http.MethodGet, "/whatever", nil)
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
conf := &config.Config{
Version: "1.0.0",
}
middleware := middleware2.VersionHeader(conf)
handler := middleware(func(c echo.Context) error {
return c.String(http.StatusOK, "Authorized")
})

err := handler(c)
assert.NoError(t, err)
assert.Equal(t, "1.0.0", rec.Header().Get("X-GOFEATUREFLAG-VERSION"))
}
1 change: 1 addition & 0 deletions cmd/relayproxy/api/routes_monitoring.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func (s *Server) addMonitoringRoutes() {
s.monitoringEcho.Debug = s.config.Debug
s.monitoringEcho.Use(custommiddleware.ZapLogger(s.zapLog, s.config))
s.monitoringEcho.Use(middleware.CORSWithConfig(middleware.DefaultCORSConfig))
s.monitoringEcho.Use(custommiddleware.VersionHeader(s.config))
s.monitoringEcho.Use(middleware.Recover())
s.initMonitoringEndpoint(s.monitoringEcho)
} else {
Expand Down
1 change: 1 addition & 0 deletions cmd/relayproxy/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func (s *Server) initRoutes() {
s.apiEcho.Use(otelecho.Middleware("go-feature-flag"))
s.apiEcho.Use(custommiddleware.ZapLogger(s.zapLog, s.config))
s.apiEcho.Use(middleware.CORSWithConfig(middleware.DefaultCORSConfig))
s.apiEcho.Use(custommiddleware.VersionHeader(s.config))
s.apiEcho.Use(middleware.Recover())
s.apiEcho.Use(middleware.TimeoutWithConfig(
middleware.TimeoutConfig{
Expand Down

0 comments on commit c819253

Please sign in to comment.