Better Programming

Advice for programmers.

Follow publication

Member-only story

A Do-it-yourself Implementation of the Golang Middleware

Dwen
Better Programming
Published in
6 min readApr 4, 2022
Photo by Surface on Unsplash

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.

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Dwen
Dwen

Written by Dwen

I'm an independent entrepreneur, a developer and a father, enjoys speaking, writing, and sharing.

Write a response