diff --git a/docs/advanced-guide/middlewares/page.md b/docs/advanced-guide/middlewares/page.md index 7369757bb..4ddcf023f 100644 --- a/docs/advanced-guide/middlewares/page.md +++ b/docs/advanced-guide/middlewares/page.md @@ -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 -} -``` - diff --git a/pkg/gofr/gofr.go b/pkg/gofr/gofr.go index 979983016..c580844be 100644 --- a/pkg/gofr/gofr.go +++ b/pkg/gofr/gofr.go @@ -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 [*App.UseMiddleware] method that does not depend on the container. 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`