gin

package
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 22, 2026 License: MIT Imports: 22 Imported by: 0

Documentation

Overview

Package gin provides Gin-based HTTP server implementation for gorp framework. This file implements handler and middleware adaptation from framework contracts to Gin handlers. Converts transport Handler/Middleware into Gin.HandlerFunc.

Gin HTTP 服务包,提供基于 Gin 的 HTTP 服务器实现,用于 gorp 框架。 本文件实现 handler 和 middleware 适配,从框架契约转为 Gin handler。 将 transport Handler/Middleware 转换为 Gin.HandlerFunc。

Package gin provides Gin-based HTTP server implementation for gorp framework. This file implements Context interface by delegating to gin.Context.

Gin HTTP 服务包,提供基于 Gin 的 HTTP 服务器实现,用于 gorp 框架。 本文件通过委托给 gin.Context 实现 Context 接口。

Package gin provides Gin-based HTTP server implementation for gorp framework. This file implements engine-level middleware attachment and container injection. Mounts tracing, metadata propagation, and optional service auth middleware.

Gin HTTP 服务包,提供基于 Gin 的 HTTP 服务器实现,用于 gorp 框架。 本文件实现 engine 级中间件挂载和容器注入。 挂载 tracing、元数据传播和可选的服务认证中间件。

Package gin provides Gin-based HTTP server implementation for gorp framework. This file provides helper functions for Gin-first users to access native Gin capabilities. These functions allow users to use *gin.Engine and *gin.RouterGroup directly while remaining within the framework's governance boundary.

Gin HTTP 服务包,提供基于 Gin 的 HTTP 服务器实现,用于 gorp 框架。 本文件为 Gin-first 用户提供访问原生 Gin 能力的辅助函数。 这些函数允许用户直接使用 *gin.Engine 和 *gin.RouterGroup, 同时保持在框架的治理边界内。

Package gin provides Gin-based HTTP server implementation for gorp framework. This file provides Prometheus metrics handler and Go runtime metrics registration. Exposes /metrics endpoint and registers GC, memory, goroutine collectors.

Gin HTTP 服务包,提供基于 Gin 的 HTTP 服务器实现,用于 gorp 框架。 本文件提供 Prometheus 指标 handler 和 Go 运行时指标注册。 暴露 /metrics 端点并注册 GC、内存、goroutine 采集器。

Eg:

router.Mount("/metrics", ginprovider.PrometheusHandler())
ginprovider.RegisterGoRuntimeMetrics()

Package gin provides Gin-based HTTP server implementation for gorp framework. Implements framework-level HTTP service contract with Gin engine and net/http server. Includes container injection, default responder, Prometheus metrics, and graceful shutdown.

Gin HTTP 服务包,提供基于 Gin 的 HTTP 服务器实现,用于 gorp 框架。 实现框架级 HTTP 服务契约,包含 Gin engine 和 net/http server。 包括容器注入、默认响应器、Prometheus 指标和优雅关闭。

Eg:

// 注册 Provider
app.Register(gin.NewProvider())

// 获取 HTTP 服务
httpSvc := c.MustMake(transportcontract.HTTPKey).(transportcontract.HTTP)
httpSvc.Router().GET("/hello", helloHandler)
httpSvc.Run()

Package gin provides Gin-based HTTP server implementation for gorp framework. This file implements the HTTP router interface wrapping Gin RouterGroup. Supports route registration, middleware mounting, and child group creation.

Gin HTTP 服务包,提供基于 Gin 的 HTTP 服务器实现,用于 gorp 框架。 本文件实现 HTTP 路由接口,包装 Gin RouterGroup。 支持路由注册、中间件挂载和子路由组创建。

Package gin provides Gin-based HTTP server implementation for gorp framework. This file implements the HTTP service interface with Gin engine and net/http server. Supports application lifecycle operations: Run, Shutdown, Router, Server access.

Gin HTTP 服务包,提供基于 Gin 的 HTTP 服务器实现,用于 gorp 框架。 本文件实现 HTTP 服务接口,包含 Gin engine 和 net/http server。 支持应用生命周期操作:Run、Shutdown、Router、Server 访问。

Index

Constants

View Source
const HTTPEngineKey = httpEngineKey

HTTPEngineKey is the public container key for the underlying Gin engine. Use this key to retrieve *gin.Engine from the framework container.

HTTPEngineKey 是底层 Gin engine 的公开容器键。 使用此键从框架容器获取 *gin.Engine。

Variables

This section is empty.

Functions

func AdaptHandler

func AdaptHandler(handler transportcontract.Handler) gin.HandlerFunc

AdaptHandler adapts a framework Handler into a Gin.HandlerFunc. This allows gorp handler functions to be used directly on Gin router groups.

AdaptHandler 将框架 Handler 适配为 Gin.HandlerFunc。 允许 gorp handler 函数直接在 Gin 路由组上使用。

func AdaptMiddleware

func AdaptMiddleware(middleware transportcontract.Middleware) gin.HandlerFunc

AdaptMiddleware adapts a framework Middleware into a Gin.HandlerFunc. This allows gorp governance middleware to be used directly on *gin.Engine or *gin.RouterGroup.

AdaptMiddleware 将框架 Middleware 适配为 Gin.HandlerFunc。 允许 gorp 治理 middleware 直接在 *gin.Engine 或 *gin.RouterGroup 上使用。

Example:

engine := gin.New()
engine.Use(ginprovider.AdaptMiddleware(httpmiddleware.RequestIdentity()))

func MustEngine

func MustEngine(c runtimecontract.Container) (*gin.Engine, bool)

MustEngine directly extracts *gin.Engine from the container without requiring the user to first obtain the HTTP service.

MustEngine 直接从容器提取 *gin.Engine,无需用户先获取 HTTP 服务。

Example:

engine, ok := gin.MustEngine(c)
if ok {
    engine.Use(gin.AdaptMiddleware(httpmiddleware.RequestIdentity()))
}

func NativeEngine

func NativeEngine(httpSvc transportcontract.HTTP) (*gin.Engine, bool)

NativeEngine extracts the underlying *gin.Engine from an HTTP service. Returns the engine and true if the HTTP implementation is Gin-backed. Returns nil and false otherwise.

NativeEngine 从 HTTP 服务中提取底层 *gin.Engine。 如果 HTTP 实现基于 Gin,返回 engine 和 true。 否则返回 nil 和 false。

Example:

httpSvc := c.MustMake(transportcontract.HTTPKey).(transportcontract.HTTP)
engine, ok := gin.NativeEngine(httpSvc)
if ok {
    engine.Use(gin.AdaptMiddleware(httpmiddleware.RequestIdentity()))
}

func NativeRouterGroup

func NativeRouterGroup(httpSvc transportcontract.HTTP) (*gin.RouterGroup, bool)

NativeRouterGroup extracts the root *gin.RouterGroup from an HTTP service. Returns the router group and true if the HTTP implementation is Gin-backed. Returns nil and false otherwise.

NativeRouterGroup 从 HTTP 服务中提取根 *gin.RouterGroup。 如果 HTTP 实现基于 Gin,返回 routerGroup 和 true。 否则返回 nil 和 false。

func NewTestEngine added in v0.1.2

func NewTestEngine() *gin.Engine

NewTestEngine creates a gin.Engine configured for testing with ContextWithFallback enabled. This ensures gin.Context.Value() properly delegates to Request.Context().Value() for non-string keys, which is required for context.Context value propagation.

NewTestEngine 创建用于测试的 gin.Engine,启用 ContextWithFallback。 确保 gin.Context.Value() 正确委托到 Request.Context().Value() 处理非字符串 key, 这是 context.Context 值传播的必要设置。

Example:

engine := NewTestEngine()
engine.Use(YourMiddleware())
engine.GET("/test", handler)

func PrometheusHandler

func PrometheusHandler() http.Handler

PrometheusHandler returns a standard http.Handler for exposing Prometheus metrics.

PrometheusHandler 返回一个用于暴露 Prometheus 指标的标准 http.Handler。

Example:

router.Mount("/metrics", ginprovider.PrometheusHandler())

func RegisterGoRuntimeMetrics

func RegisterGoRuntimeMetrics()

RegisterGoRuntimeMetrics registers Go runtime collectors into the default Prometheus registry.

RegisterGoRuntimeMetrics 将 Go 运行时采集器注册到默认 Prometheus 注册表。

func UnwrapContext

func UnwrapContext(c transportcontract.Context) (*gin.Context, bool)

UnwrapContext extracts the raw gin.Context from a transport Context. This is the public API for users who need to access Gin-specific features.

UnwrapContext 从 transport Context 中提取原始 gin.Context。 这是公开 API,供需要访问 Gin 特有功能的用户使用。

Example:

func handler(c gorp.Context) {
    gc, ok := gin.UnwrapContext(c)
    if ok {
        // Use native Gin context
        gc.HTML(200, "index.html", gin.H{})
    }
}

Types

type Provider

type Provider struct{}

Provider wires the Gin-based HTTP server into the framework container.

Provider 将基于 Gin 的 HTTP 服务接入框架容器。

func NewProvider

func NewProvider() *Provider

NewProvider creates a Gin HTTP provider instance.

NewProvider 创建一个 Gin HTTP Provider 实例。

func (*Provider) Boot

Boot warms up optional dependencies needed at startup.

Boot 预热启动阶段需要的可选依赖。

func (*Provider) DependsOn

func (p *Provider) DependsOn() []string

DependsOn returns the keys this provider depends on. Gin provider depends on Config and Log.

DependsOn 返回该 provider 依赖的 key。 Gin provider 依赖 Config 和 Log。

func (*Provider) IsDefer

func (p *Provider) IsDefer() bool

IsDefer reports whether provider registration should be deferred.

IsDefer 表示该 provider 是否需要延迟注册。

func (*Provider) Name

func (p *Provider) Name() string

Name returns the provider name used by the runtime container.

Name 返回运行时容器使用的 provider 名称。

func (*Provider) Provides

func (p *Provider) Provides() []string

Provides declares the services exported by this provider.

Provides 声明该 provider 对外提供的服务键。

func (*Provider) Register

func (p *Provider) Register(c runtimecontract.Container) error

Register binds the Gin engine, HTTP service, and middleware registry into the container.

Register 将 Gin engine、HTTP service 和中间件注册表绑定到容器。

Example:

container.RegisterProviders(ginprovider.NewProvider())

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL