GoWindGoWind
Home
Framework
GoWind Toolkit
  • Open Source

    • GoWind Admin (Admin Platform)
    • GoWind CMS (Content Management)
    • GoWind UBA (Behavior Analytics)
    • GoWind Shop (E-commerce)
    • GoWind Kanban (Kanban Boards)
    • GoWind Ledger (Personal Finance)
    • GoWind ShortPlay (Short Drama)
    • GoWind Ops (Operations Platform)
    • RushWind (Rust Ecosystem)
  • Open Core (Community + Enterprise)

    • GoWind OA (Office Collaboration)
    • GoWind ERP (Procure-to-Pay)
    • GoWind WMS (Warehousing)
  • Commercial

    • GoWind IM (Instant Messaging)
    • GoWind IoT (IoT Platform)
    • GoWind Edu (AI Education)
    • GoWind MES (Manufacturing Execution)
    • GoWind CDC (Data Pipeline)
    • GoWind SDP (Zero-Trust Gateway)
    • GoWind Quant (Quant Research)
    • GoWind Trade Radar (Export Sourcing)
    • GoWind Game Radar (Game Greenlight)
  • 简体中文
  • English
GitHub
Home
Framework
GoWind Toolkit
  • Open Source

    • GoWind Admin (Admin Platform)
    • GoWind CMS (Content Management)
    • GoWind UBA (Behavior Analytics)
    • GoWind Shop (E-commerce)
    • GoWind Kanban (Kanban Boards)
    • GoWind Ledger (Personal Finance)
    • GoWind ShortPlay (Short Drama)
    • GoWind Ops (Operations Platform)
    • RushWind (Rust Ecosystem)
  • Open Core (Community + Enterprise)

    • GoWind OA (Office Collaboration)
    • GoWind ERP (Procure-to-Pay)
    • GoWind WMS (Warehousing)
  • Commercial

    • GoWind IM (Instant Messaging)
    • GoWind IoT (IoT Platform)
    • GoWind Edu (AI Education)
    • GoWind MES (Manufacturing Execution)
    • GoWind CDC (Data Pipeline)
    • GoWind SDP (Zero-Trust Gateway)
    • GoWind Quant (Quant Research)
    • GoWind Trade Radar (Export Sourcing)
    • GoWind Game Radar (Game Greenlight)
  • 简体中文
  • English
GitHub
  • Introduction

    • GoWind Framework
    • Framework Architecture
  • go-wind Core

    • go-wind Core Framework
    • App Lifecycle Management
    • Context Propagation
    • Transport Abstraction
    • Log Facade Interface
  • go-wind-plugins

    • go-wind-plugins Plugin Overview
    • Plugin Configuration System
    • Plugin Registration Mechanism (SPI)
    • Logging Adapter Plugins
    • Transport Protocol Plugins
    • Message Middleware Plugins
    • Encoding Plugins
    • Security and Authentication Plugins
    • Distributed Tracing Plugins
    • Cache Plugins
    • Object Storage Plugins (OSS)
    • Rate Limiting Plugins
    • Metrics Plugins
    • AI Plugins
    • Workflow Plugins
    • Database and Cache Plugins
    • Bootstrap Integration and Gateway Plugins
    • Compression and Serialization Plugins
    • Template Rendering and Validation Plugins
  • go-wind-bootstrap

    • go-wind-bootstrap: The Declarative Bootstrap
    • The Bootstrap Configuration System
    • The Bootstrap SPI Mechanism
    • Declarative Middleware Orchestration
    • Bootstrap Command-Line Integration
    • Bootstrap Practical Examples
  • go-wind-flow Workflow Engine

    • GoWind Flow Process Engine
  • Tutorials

    • Quick Start Tutorial
    • Custom Plugin Development Tutorial
    • Multi-Protocol Listening Tutorial
    • Framework Migration Guide

Declarative Middleware Orchestration

go-wind-bootstrap lets you declare middleware chains in YAML without writing middleware initialization by hand in code: middleware is enabled through optional fields of the configuration file, and their parameters are passed in via configuration as well.

1. The Middleware Configuration Model

HTTP / gRPC middleware is defined as optional fields of the Middleware message in server.proto — the presence of a field enables it (recovery: {} means enabled with default parameters). Registration order follows the proto field order and is independent of the order in which fields are written in YAML.

server:
  http:
    addr: ":8080"
    middleware:
      recovery:
        stack_trace: true
      cors:
        allowed_origins:
          - "*"
        allowed_methods:
          - GET
          - POST
      logging:
        skip_paths:
          - /health
      request_id:
        header_name: X-Request-ID

2. Built-in Middleware

2.1 HTTP Middleware

MiddlewareConfiguration FieldDescription
RecoveryrecoveryPanic recovery, optional stack_trace (log the stack, default true)
CORScorsCross-origin resource sharing: allowed_origins / allowed_methods / allowed_headers / exposed_headers / allow_credentials / max_age
LoggingloggingRequest logging, optional skip_paths
Request IDrequest_idRequest ID injection, optional header_name (default X-Request-Id)
TracingtracingOpenTelemetry distributed tracing; uses the global TracerProvider automatically, no extra parameters
Rate Limitrate_limitRate limiting: rate (requests per second) / burst (burst capacity) / wait (waiting mode)
TimeouttimeoutRequest timeout: default_timeout_ms / status_code (default 503)

2.2 gRPC Middleware

gRPC middleware are all switch-type (empty message body; presence enables them):

MiddlewareConfiguration FieldDescription
RecoveryrecoveryPanic recovery, returns an Internal error
LoggingloggingRequest logging
TracingtracingOpenTelemetry distributed tracing
ValidatevalidateRequest validation
server:
  grpc:
    addr: ":9000"
    middleware:
      recovery: {}
      logging: {}
      tracing: {}
      validate: {}

3. Execution Order

HTTP middleware is registered in proto field order, forming the onion model:

recovery → cors → logging → request_id → tracing → rate_limit → timeout
    ↓ requests pass through each layer in order
  → → → Handler → → →
  ← ← ← responses return in reverse order ← ← ←

That is, Recovery catches panics first, and Timeout sits closest to the business Handler.

4. Custom Middleware

Middleware beyond the configuration surface (business auditing, custom authentication, etc.) is attached via the adapter's append-style registration API: callbacks registered with RegisterServerSetup run after the configured middleware has been mounted; inside the callback you can add custom middleware via srv.Use and register routes with methods such as srv.GET.

package main

import (
    "net/http"

    httpAdapter "github.com/tx7do/go-wind-bootstrap/transport/http"
)

// A custom middleware with type func(http.Handler) http.Handler
func auditLog(next http.Handler) http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        // Write audit logs...
        next.ServeHTTP(w, r)
    })
}

func init() {
    httpAdapter.RegisterServerSetup(func(srv *httpAdapter.Server) {
        srv.Use(auditLog)
        srv.GET("/health", func(w http.ResponseWriter, r *http.Request) {
            w.WriteHeader(http.StatusOK)
        })
    })
}

RegisterServerSetup registration is append-style and supports multiple call sites (code generators and hand-written code coexist); callbacks execute in registration order. For related route registration patterns, see Bootstrap Practical Examples.

Related Documentation

  • Bootstrap Introduction
  • The Bootstrap Configuration System
  • Bootstrap Practical Examples
  • Security and Authentication Plugins
  • Rate Limiting Plugin
Edit this page
Last Updated:: 11/3/26, 2:14 PM
Contributors: Bobo
Prev
The Bootstrap SPI Mechanism
Next
Bootstrap Command-Line Integration