Skip to content

Conversation

@pengdst
Copy link

@pengdst pengdst commented Mar 30, 2023

Improve middleware filter functionality

  • menyederhanakan apply filter middleware dengan function Default() untuk simple usecase

  • apply filter menggunakan variadic functions dengan arguments type string

  • apply filter function arguments dapat dikosongkan (optional) karena menggunakan variadic functions

  • update README.md

r.Use(echolog.Default())

atau

r.Use(echolog.Default("healthcheck", "foo"))

Pengkuh Dwi Septiandi added 2 commits March 30, 2023 20:47
- middleware now can receive multiple filter with type string (optional)
- middleware filter with function now renamed to MiddlewareFunc
@syahidfrd syahidfrd self-requested a review March 30, 2023 17:21
Copy link
Member

@syahidfrd syahidfrd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @pengdst thanks for your PR
But, the idea of using function parameters with a return bool makes it easier for developers to customize filter conditions using the echo context property. Your suggested new middleware can also be easily implemented using this approach:

r.Use(echolog.Middleware(func(c echo.Context) (skip bool) {
	for _, path := range []string{"/healthcheck", "/foo"} {
		if path == c.Request().RequestURI {
			skip = true
			break
		}
	}
	return
}))

@pengdst
Copy link
Author

pengdst commented Mar 30, 2023

The developer also able to use that way, i just rename the function to MiddlewareFilterFunc. The difference is when developers don't filter they don't need to add 'nil'

r.Use(echolog.Middleware())

Copy link
Author

@pengdst pengdst left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update @syahidfrd : i just rename the function Middleware() to Default() and MiddlewareFilterFunc to Middleware.

So, now for simple usecase developers can do this way

e.Use(echolog.Default())

or to add filter

e.Use(echolog.Default("/healthcheck", "foo", "bar"))

Also, for advance filter usecase, they can do this too:

r.Use(echolog.Middleware(func(c echo.Context) (skip bool) {
	for _, path := range []string{"/healthcheck", "/foo"} {
		if path == c.Request().RequestURI {
			skip = true
			break
		}
	}
	return
}))

I think create new function Default is better than rename the Middleware function from my previous commit. How do you think?

@syahidfrd
Copy link
Member

update @syahidfrd : i just rename the function Middleware() to Default() and MiddlewareFilterFunc to Middleware.

So, now for simple usecase developers can do this way

e.Use(echolog.Default())

or to add filter

e.Use(echolog.Default("/healthcheck", "foo", "bar"))

Also, for advance filter usecase, they can do this too:

r.Use(echolog.Middleware(func(c echo.Context) (skip bool) {
	for _, path := range []string{"/healthcheck", "/foo"} {
		if path == c.Request().RequestURI {
			skip = true
			break
		}
	}
	return
}))

I think create new function Default is better than rename the Middleware function from my previous commit. How do you think?

Hi @pengdst I don't quite agree, the filter function is not just for filtering URIs, but more than that, developers can use the echo context to get header data, host, IP address etc. for filtering.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants