lus/dgc

Middleware does not support executing after commands

Closed this issue · 3 comments

Hello, I'm evaluating dgc and dgrouter today. One thing I noticed about dgc is that it's Middleware does not support a common use-case for Middleware. That is doing more after the command has run. For example when instrumenting metrics, you want to start a timer then stop it and report it after the command has executed. But since the dgc.Middleware type expects a bool return to continue or not you cannot do this.

A common example implanted using dgrouter's Middleware type.

var TimingMiddleware = func(next exrouter.HandlerFunc) exrouter.HandlerFunc {
	return func(ctx *exrouter.Context) {
		start := time.Now()

		// Continue on
		next(ctx)

		took := time.Now().Sub(start)
		fmt.Printf("That took %vms", took.Milliseconds())
	}
}

I really want to use dgc, I like the declarative API a lot, but this blocks my adoption.

How do you feel about breaking API changes? I think the golang Middleware pattern is fundamental enough that I'd break the API to change it to match the established pattern. I'm probably going to take a pass at making this change on a fork.

lus commented

Hey^^

Thank you for your feedback.
Yeah, the middlewares are things I definetely want to work on again to make them more abstract. I will think about a nice solution and I think I will push a new version today. I think I will release v1.0.0 today too, because I think the codebase is pretty usable for now.
I will come back to you if I have more information.

lus commented

The described middleware structure has been integrated. Thanks for your contribution.