Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@DengY11
Copy link
Contributor

@DengY11 DengY11 commented Jul 27, 2025

Add custom middleware support to the gateway module:
Changes:

  • Add WithMiddleware function that accepts one or more middleware functions
  • Support both single and multiple middleware registration
  • Maintain compatibility with existing WithHeaderProcessor functionality
  • Add comprehensive tests for middleware functionality

Usage Examples
Single Middleware

server := gateway.MustNewServer(config, gateway.WithMiddleware(authMiddleware))

Multiple Middlewares (Single Call)

server := gateway.MustNewServer(config, 
    gateway.WithMiddleware(authMiddleware, loggingMiddleware, rateLimitMiddleware))

Multiple Middlewares (Multiple Calls)

server := gateway.MustNewServer(config, 
    gateway.WithMiddleware(authMiddleware),
    gateway.WithMiddleware(loggingMiddleware),
    gateway.WithMiddleware(rateLimitMiddleware))

Example Middleware Implementation

func AuthMiddleware(next http.HandlerFunc) http.HandlerFunc {
    return func(w http.ResponseWriter, r *http.Request) {
        // Pre-processing: check authentication
        token := r.Header.Get("Authorization")
        if !isValidToken(token) {
            http.Error(w, "Unauthorized", http.StatusUnauthorized)
            return
        }
        
        // Call next middleware/handler
        next(w, r)
        
        // Post-processing: cleanup if needed
        // ...
    }
}

close #5006

DengY11 added 2 commits July 31, 2025 21:26
- Add WithMiddleware function to support one or more middleware functions
- Support middleware chaining with onion model execution (like gin.Next())
- Compatible with existing WithHeaderProcessor functionality
- Multiple middlewares can be added in single call or multiple calls
- Middlewares execute in order: first->second->...->handler->...->second->first

Example usage:
// Single middleware
server := MustNewServer(config, WithMiddleware(authMiddleware))

// Multiple middlewares in one call
server := MustNewServer(config, WithMiddleware(authMiddleware, loggingMiddleware))

// Multiple calls
server := MustNewServer(config,
    WithMiddleware(authMiddleware),
    WithMiddleware(loggingMiddleware))

Closes zeromicro#5006
@kevwan kevwan force-pushed the feature/gateway-custom-middleware branch from 02ccc56 to 89631e9 Compare July 31, 2025 13:26
@kevwan kevwan added this to the v1.9.0 milestone Jul 31, 2025
@codecov
Copy link

codecov bot commented Jul 31, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@kevwan kevwan added this pull request to the merge queue Jul 31, 2025
Merged via the queue into zeromicro:master with commit b0b31f3 Jul 31, 2025
6 checks passed
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.

Support Custom Middleware for HTTP and gRPC in grpc-gateway

2 participants