echo

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2026 License: MIT Imports: 14 Imported by: 0

README

Echo Starter

Echo Web 框架自动配置模块,提供轻量级 HTTP 服务器支持。

功能特性

  • ✅ 自动配置 Echo 服务器
  • ✅ 内置中间件支持(Recover、Logger、CORS)
  • ✅ 优雅启动和关闭
  • ✅ 路由注册支持

快速开始

1. 引入依赖
import (
    "github.com/xudefa/enhance/starter/echo"
)
2. 配置文件

application.json 中添加 Echo 配置:

{
  "echo": {
    "enabled": true,
    "host": "0.0.0.0",
    "port": 8080,
    "hide_banner": false,
    "hide_port": false,
    "enable_recover": true,
    "enable_logger": true,
    "enable_cors": true
  }
}
3. 使用示例
package main

import (
    "net/http"
    
    "github.com/labstack/echo/v4"
    
    "github.com/xudefa/enhance/boot"
    "github.com/xudefa/enhance/core"
    "github.com/xudefa/enhance/starter/echo"
)

func main() {
    app, _ := boot.NewApplication(
        boot.WithAppName("echo-demo"),
    )
    defer app.Stop()
    
    // 获取 Echo 实例
    e := core.MustGetBean[*echo.Echo](app.Container())
    
    // 注册路由
    e.GET("/", func(c echo.Context) error {
        return c.String(http.StatusOK, "Hello, Echo!")
    })
    
    e.GET("/users/:id", func(c echo.Context) error {
        id := c.Param("id")
        return c.JSON(http.StatusOK, map[string]string{
            "user_id": id,
        })
    })
    
    // 启动服务器
    app.Start()
    app.WaitForSignal()
}

配置说明

配置项 类型 默认值 说明
echo.enabled bool false 是否启用 Echo
echo.host string 0.0.0.0 服务器地址
echo.port int 8080 服务器端口
echo.hide_banner bool false 是否隐藏启动横幅
echo.hide_port bool false 是否隐藏端口日志
echo.enable_recover bool true 是否启用 panic 恢复
echo.enable_logger bool true 是否启用请求日志
echo.enable_cors bool false 是否启用 CORS

高级用法

自定义中间件
e := core.MustGetBean[*echo.Echo](app.Container())

// 添加自定义中间件
e.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
    return func(c echo.Context) error {
        // 前置处理
        println("请求开始")
        err := next(c)
        // 后置处理
        println("请求结束")
        return err
    }
})
路由分组
api := e.Group("/api")
{
    api.GET("/users", getUsers)
    api.POST("/users", createUser)
    api.PUT("/users/:id", updateUser)
    api.DELETE("/users/:id", deleteUser)
}
优雅关闭
echoConfig := core.MustGetBean[*echo.EchoAutoConfiguration](app.Container())

// 手动关闭
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
echoConfig.Stop(ctx)

启动顺序

  • 优先级: OrderPriorityWebLayer (0)
  • 触发条件: echo.enabled=true

依赖

  • github.com/labstack/echo/v4

Documentation

Overview

Package echo 提供 Echo Web 框架自动配置。

Package echo 提供 Echo Web 框架自动配置。

Index

Constants

View Source
const (
	EchoEnabled   = "echo.enabled"
	ConditionTrue = "true"
)

配置常量。

View Source
const Doc = "Echo Web 框架自动配置 - 当 echo.enabled=true 时自动生效"

Doc 包文档说明。

Variables

This section is empty.

Functions

func GetServer

func GetServer(container core.Container) (*echo.Echo, error)

GetServer 从容器中获取 Echo 服务器实例。

func TracingMiddleware

func TracingMiddleware(tracer *tracing.Tracer) echo.MiddlewareFunc

TracingMiddleware Echo 框架的分布式链路追踪中间件。

自动从请求头提取追踪上下文,创建 Span 并记录请求信息。

Types

type EchoAutoConfiguration

type EchoAutoConfiguration struct {
	// contains filtered or unexported fields
}

EchoAutoConfiguration Echo Web 框架自动配置类。

func (*EchoAutoConfiguration) Configure

Configure 配置 Echo Web 服务器。

func (*EchoAutoConfiguration) Dependencies

func (c *EchoAutoConfiguration) Dependencies() []string

Dependencies 返回依赖的其他启动器名称。

func (*EchoAutoConfiguration) GetCondition

func (c *EchoAutoConfiguration) GetCondition() condition.Condition

GetCondition 返回启动器条件。

func (*EchoAutoConfiguration) Name

func (c *EchoAutoConfiguration) Name() string

Name 返回启动器名称。

func (*EchoAutoConfiguration) Start

Start 启动 Echo Web 服务器。

func (*EchoAutoConfiguration) Stop

Stop 停止 Echo Web 服务器。

type EchoConfig

type EchoConfig struct {
	Enabled       bool   `json:"enabled" mapstructure:"enabled"`
	Host          string `json:"host" mapstructure:"host"`
	Port          int    `json:"port" mapstructure:"port"`
	HideBanner    bool   `json:"hide_banner" mapstructure:"hide_banner"`
	HidePort      bool   `json:"hide_port" mapstructure:"hide_port"`
	EnableRecover bool   `json:"enable_recover" mapstructure:"enable_recover"`
	EnableLogger  bool   `json:"enable_logger" mapstructure:"enable_logger"`
	EnableCORS    bool   `json:"enable_cors" mapstructure:"enable_cors"`
}

EchoConfig Echo Web 服务器配置。

type EchoEndpointRegistry

type EchoEndpointRegistry struct {
	// contains filtered or unexported fields
}

EchoEndpointRegistry Echo 框架的 HttpEndpointRegistry 实现

该实现将 Actuator 的标准 http.Handler 适配为 Echo 的 HandlerFunc, 并注册到 Echo 引擎中。

func NewEchoEndpointRegistry

func NewEchoEndpointRegistry(engine *echo.Echo) *EchoEndpointRegistry

NewEchoEndpointRegistry 创建 Echo 端点注册表

func (*EchoEndpointRegistry) HasEndpoint

func (r *EchoEndpointRegistry) HasEndpoint(path string) bool

HasEndpoint 检查是否已注册指定路径的端点

func (*EchoEndpointRegistry) RegisterEndpoint

func (r *EchoEndpointRegistry) RegisterEndpoint(method, path string, handler http.Handler)

RegisterEndpoint 注册单个端点

func (*EchoEndpointRegistry) RegisterEndpoints

func (r *EchoEndpointRegistry) RegisterEndpoints(endpoints []actuator.EndpointConfig)

RegisterEndpoints 批量注册端点

Jump to

Keyboard shortcuts

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