避免 labstack/echo 路由中的全局变量

Bru*_*ams 0 go go-echo

我正在使用labstack/echo webserver 和gofight进行单元测试。在学习 go 时,go 想知道是否有 go 成语用于访问(嵌入式) echo 结构之外的状态。例如:

type WebPlusDB struct {
    web *echo.Echo
    db  *databaseInterface
}

func NewEngine() *echo.Echo {
    e := echo.New()
    e.GET("/hello", route_hello)
    return e    
}

func NewWebPlusDb() {
    e := NewEngine()
    db := database.New()   
    return WebPlusDB{e,db}
}

// for use in unit tests
func NewFakeEngine() *WebPlusDB {
    e := NewEngine()
    db := fakeDatabase.New()   
    return WebPlusDB{e,db}
}    

func route_hello(c echo.Context) error {
    log.Printf("Hello world\n")

    // how do I access WebPlusDB.db from here?

    return c.String(http.StatusOK, "hello world")
}
Run Code Online (Sandbox Code Playgroud)

然后在我使用的测试代码中:

import (
    "github.com/labstack/echo"
    "github.com/appleboy/gofight"
    "github.com/stretchr/testify/assert"
    "testing"
)

func TestHelloWorld(t *testing.T) {
    r := gofight.New()

    r.GET("/hello").
          Run(NewFakeEngine(), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
        assert.Equal(t, http.StatusOK, r.Code)
        assert.Equal(t, "hello world", r.Body.String())
        // test database access
    })

}
Run Code Online (Sandbox Code Playgroud)

最简单的解决方案是必须使用的,而不是在“WebPlusDB”嵌入回声和增加国家有一个全局变量。我想要更好的封装。我想我应该使用类似 WebPlusDB 结构的东西,而不是 echo.Echo 加上全局状态。这对于单元测试来说可能并不重要,但在更大的计划中,我想知道在 go 中正确做事(在这种情况下避免全局变量)。

有没有解决方案或者这是回声设计的弱点?它具有中间件的扩展点,但数据库后端并不是此处定义的真正中间件

注意:我在这里使用数据库来说明常见情况,但它可以是任何东西(我实际上使用的是amqp

看起来您可以扩展上下文接口,但它是在哪里创建的?这看起来像是使用了一种向下转换

e.GET("/", func(c echo.Context) error {
    cc := c.(*CustomContext)
}
Run Code Online (Sandbox Code Playgroud)

我认为(可能是错误的)这仅在接口上允许,并且 echo.Context.Echo() 返回的类型不是接口。

Adr*_*ian 5

您可以将实例的方法作为函数值传递,这可能是处理此问题的最直接的方法:

type WebPlusDB struct {
    web *echo.Echo
    db  *databaseInterface
}

func (w WebPlusDB) route_hello(c echo.Context) error {
    log.Printf("Hello world\n")

    // do whatever with w

    return c.String(http.StatusOK, "hello world")
}

func NewEngine() *echo.Echo {
    e := echo.New()
    w := NewWebPlusDb()
    e.GET("/hello", w.route_hello)
    return e    
}
Run Code Online (Sandbox Code Playgroud)