Member-only story
A Do-it-yourself Implementation of the Golang Middleware
In-depth understanding of the principle of middleware
What do you think of when we talk about middleware? Some people would say that middleware is an interceptor/filter, which can be used to do some pre-processing and post-processing.
You are right, middleware is essentially to provide general process control.
What to control? That is, the pre-processing and post-processing mentioned above.
Generally, we will put common process control code into middleware to do it, such as:
- Permission verification
- Request tracking
- Pre-parameter check
- Logging
- …
If we were to implement a middleware, what would we need to do?
First, let’s analyze what functions a middleware needs to have.
- Can intercept requests, failure to intercept requests is meaningless.
- Can bind multiple generic process control functions.
- Binding order to execute process control functions.
After analysis, we can conclude that the middleware is used to handle the call relationship of generic functions.