Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deprecate UseMiddlewarWithContainer and remove it from tests and docs #1228

Merged
merged 4 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 0 additions & 53 deletions docs/advanced-guide/middlewares/page.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,56 +67,3 @@ func main() {
}
```

### Using UseMiddlewareWithContainer for Custom Middleware with Container Access

The UseMiddlewareWithContainer method allows middleware to access the application's container, providing access to
services like logging, configuration, and databases. This method is especially useful for middleware that needs access
to resources in the container to modify request processing flow.

#### Example:

```go
import (
"fmt"
"net/http"

"gofr.dev/pkg/gofr"
"gofr.dev/pkg/gofr/container"
)

func main() {
// Create a new GoFr application instance
a := gofr.New()

// Add custom middleware with container access
a.UseMiddlewareWithContainer(customMiddleware)

// Define the application's routes
a.GET("/hello", HelloHandler)

// Run the application
a.Run()
}

// Define middleware with container access
func customMiddleware(c *container.Container, handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
c.Logger.Log("Hey! Welcome to GoFr")

// Continue with the request processing
handler.ServeHTTP(w, r)
})
}

// Sample handler function
func HelloHandler(c *gofr.Context) (interface{}, error) {
name := c.Param("name")
if name == "" {
c.Log("Name came empty")
name = "World"
}

return fmt.Sprintf("Hello %s!", name), nil
}
```

2 changes: 2 additions & 0 deletions pkg/gofr/gofr.go
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,8 @@ func (a *App) UseMiddleware(middlewares ...gofrHTTP.Middleware) {
//
// The `middleware` function receives the container and the handler, allowing
// the middleware to modify the request processing flow.
// Deprecated: UseMiddlewareWithContainer will be removed in a future release.
// Please use the UseMiddleware method that does not depend on the container.
Umang01-hash marked this conversation as resolved.
Show resolved Hide resolved
func (a *App) UseMiddlewareWithContainer(middlewareHandler func(c *container.Container, handler http.Handler) http.Handler) {
a.httpServer.router.Use(func(h http.Handler) http.Handler {
// Wrap the provided handler `h` with the middleware function `middlewareHandler`
Expand Down
51 changes: 0 additions & 51 deletions pkg/gofr/gofr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -620,57 +620,6 @@ func Test_UseMiddleware(t *testing.T) {
assert.Equal(t, "applied", testHeaderValue, "Test_UseMiddleware Failed! header value mismatch.")
}

// Test the UseMiddlewareWithContainer function.
Umang01-hash marked this conversation as resolved.
Show resolved Hide resolved
func TestUseMiddlewareWithContainer(t *testing.T) {
// Initialize the mock container
mockContainer := container.NewContainer(config.NewMockConfig(nil))

// Create a simple handler to test middleware functionality
handler := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("Hello, world!"))
})

// Middleware to modify response and test container access
middleware := func(c *container.Container, handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Ensure the container is passed correctly (for this test, we are just logging)
assert.NotNil(t, c, "Container should not be nil in the middleware")

// Continue with the handler execution
handler.ServeHTTP(w, r)
})
}

// Create a new App with a mock server
app := &App{
httpServer: &httpServer{
router: gofrHTTP.NewRouter(),
port: 8001,
},
container: mockContainer,
Config: config.NewMockConfig(map[string]string{"REQUEST_TIMEOUT": "5"}),
}

// Use the middleware with the container
app.UseMiddlewareWithContainer(middleware)

// Register the handler to a route for testing
app.httpServer.router.Handle("/test", handler)

// Create a test request
req := httptest.NewRequest(http.MethodGet, "/test", http.NoBody)
// Create a test response recorder
rr := httptest.NewRecorder()

// Call the handler with the request and recorder
app.httpServer.router.ServeHTTP(rr, req)

// Assert the status code and response body
assert.Equal(t, http.StatusOK, rr.Code)
assert.Equal(t, "Hello, world!", rr.Body.String())
}

func Test_APIKeyAuthMiddleware(t *testing.T) {
c, _ := container.NewMockContainer(t)

Expand Down
Loading