将上下文传递给接口方法

eli*_*rar 8 go

上周这篇文章受到了一些启发,我正在重构一个应用程序,我必须更明确地将上下文(数据库池,会话存储等)传递给我的处理程序.

但是,我遇到的一个问题是,如果没有全局模板映射,ServeHTTP我的自定义处理程序类型(满足http.Handler)上的方法将无法再访问映射以呈现模板.

我需要保留全局templates变量,或者将我的自定义处理程序类型重新定义为结构.

有没有更好的方法来实现这一目标?

func.go

package main

import (
    "fmt"
    "log"
    "net/http"

    "html/template"

    "github.com/gorilla/sessions"
    "github.com/jmoiron/sqlx"
    "github.com/zenazn/goji/graceful"
    "github.com/zenazn/goji/web"
)

var templates map[string]*template.Template

type appContext struct {
    db    *sqlx.DB
    store *sessions.CookieStore
}

type appHandler func(w http.ResponseWriter, r *http.Request) (int, error)

func (ah appHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    // templates must be global for us to use it here
    status, err := ah(w, r)
    if err != nil {
        log.Printf("HTTP %d: %q", status, err)
        switch status {
        case http.StatusNotFound:
            // Would actually render a "http_404.tmpl" here...
            http.NotFound(w, r)
        case http.StatusInternalServerError:
            // Would actually render a "http_500.tmpl" here
            // (as above)
            http.Error(w, http.StatusText(status), status)
        default:
            // Would actually render a "http_error.tmpl" here
            // (as above)
            http.Error(w, http.StatusText(status), status)
        }
    }
}

func main() {
    // Both are 'nil' just for this example
    context := &appContext{db: nil, store: nil}

    r := web.New()
    r.Get("/", appHandler(context.IndexHandler))
    graceful.ListenAndServe(":8000", r)
}

func (app *appContext) IndexHandler(w http.ResponseWriter, r *http.Request) (int, error) {
    fmt.Fprintf(w, "db is %q and store is %q", app.db, app.store)
    return 200, nil
}
Run Code Online (Sandbox Code Playgroud)

struct.go

package main

import (
    "fmt"
    "log"
    "net/http"

    "html/template"

    "github.com/gorilla/sessions"
    "github.com/jmoiron/sqlx"
    "github.com/zenazn/goji/graceful"
    "github.com/zenazn/goji/web"
)

type appContext struct {
    db        *sqlx.DB
    store     *sessions.CookieStore
    templates map[string]*template.Template
}

// We need to define our custom handler type as a struct
type appHandler struct {
    handler func(w http.ResponseWriter, r *http.Request) (int, error)
    c       *appContext
}

func (ah appHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    status, err := ah.handler(w, r)
    if err != nil {
        log.Printf("HTTP %d: %q", status, err)
        switch status {
        case http.StatusNotFound:
            // Would actually render a "http_404.tmpl" here...
            http.NotFound(w, r)
        case http.StatusInternalServerError:
            // Would actually render a "http_500.tmpl" here
            // (as above)
            http.Error(w, http.StatusText(status), status)
        default:
            // Would actually render a "http_error.tmpl" here
            // (as above)
            http.Error(w, http.StatusText(status), status)
        }
    }
}

func main() {
    // Both are 'nil' just for this example
    context := &appContext{db: nil, store: nil}

    r := web.New()
    // A little ugly, but it works.
    r.Get("/", appHandler{context.IndexHandler, context})
    graceful.ListenAndServe(":8000", r)
}

func (app *appContext) IndexHandler(w http.ResponseWriter, r *http.Request) (int, error) {
    fmt.Fprintf(w, "db is %q and store is %q", app.db, app.store)
    return 200, nil
}
Run Code Online (Sandbox Code Playgroud)

是否有更简洁的方法将context实例传递给ServeHTTP

请注意,go build -gcflags=-m在堆分配的组中,两个选项似乎都没有变得更糟:&appContext在两种情况下,文字都转义到堆(如预期的那样),尽管我的解释是基于结构的选项确实context在每个上传递了第二个指针(to )请求 - 纠正我,如果我在这里错了,因为我希望能够更好地理解这一点.

我并不完全相信全局包在主包中是坏的(即不是lib)只要它们以这种方式安全使用(只读/互斥/池),但我确实喜欢明确传递上下文提供的清晰度.

seo*_*ong 6

我会使用一个闭包,并做这样的事情:

func IndexHandler(a *appContext) http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *httpRequest) {
        // ... do stuff
        fmt.Fprintf(w, "db is %q and store is %q\n", a.db, a.store)
    })
}
Run Code Online (Sandbox Code Playgroud)

并只使用返回http.Handler.

你只需确保你appContext的goroutine安全.


eli*_*rar 4

在与#go-nuts 上的一些乐于助人的 Gophers 进行一些讨论后,据我所知,上述方法是“尽善尽美”。

  • 此方法的“缺点”是我们两次传递对上下文结构的引用:一次作为方法中的指针接收器,再次作为结构成员,以便ServeHTTP也可以访问它。
  • “优点”是,如果我们愿意的话,我们可以扩展我们的结构类型以接受请求上下文结构(就像gocraft/web那样)。

请注意,我们不能将处理程序定义为appHandlerie上的方法,因为我们需要在(ie )func (ah *appHandler) IndexHandler(...)中调用处理程序。ServeHTTPah.h(w,r)

type appContext struct {
    db        *sqlx.DB
    store     *sessions.CookieStore
    templates map[string]*template.Template
}

type appHandler struct {
    handler func(w http.ResponseWriter, r *http.Request) (int, error)
    *appContext // Embedded so we can just call app.db or app.store in our handlers.
}

// In main() ...
context := &appContext{db: nil, store: nil}
r.Get("/", appHandler{context.IndexHandler, context}) 
...
Run Code Online (Sandbox Code Playgroud)

最重要的是,这也是完全兼容的,http.Handler因此我们仍然可以使用通用中间件包装我们的处理程序结构,如下所示gzipHandler(appHandler{context.IndexHandler, context})

(不过,我仍然愿意接受其他建议!)


更新

感谢Reddit 上的这个精彩回复,我能够找到一个更好的解决方案,不需要每个请求传递两个对我的context实例的引用。

相反,我们只是创建一个接受嵌入上下文和处理程序类型的结构,并且http.Handler由于ServeHTTP. 处理程序不再是我们appContext类型上的方法,而是仅接受它作为参数,这导致函数签名稍长,但仍然“明显”且易于阅读。如果我们担心“类型”,我们就会收支平衡,因为我们不再需要担心方法接收器。

type appContext struct {
    db    *sqlx.DB
    store *sessions.CookieStore
    templates map[string]*template.Template

type appHandler struct {
    *appContext
    h func(a *appContext, w http.ResponseWriter, r *http.Request) (int, error)
}

func (ah appHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    // We can now access our context in here.
    status, err := ah.h(ah.appContext, w, r)
    log.Printf("Hello! DB: %v", ah.db)
    if err != nil {
        log.Printf("HTTP %d: %q", status, err)
        switch status {
        case http.StatusNotFound:
            // err := ah.renderTemplate(w, "http_404.tmpl", nil)
            http.NotFound(w, r)
        case http.StatusInternalServerError:
            // err := ah.renderTemplate(w, "http_500.tmpl", nil)
            http.Error(w, http.StatusText(status), status)
        default:
            // err := ah.renderTemplate(w, "http_error.tmpl", nil)
            http.Error(w, http.StatusText(status), status)
        }
    }
}

func main() {
    context := &appContext{
        db:    nil,
        store: nil,
        templates: nil,
    }

    r := web.New()
    // We pass a reference to context *once* per request, and it looks simpler
    r.Get("/", appHandler{context, IndexHandler})

    graceful.ListenAndServe(":8000", r)
}

func IndexHandler(a *appContext, w http.ResponseWriter, r *http.Request) (int, error) {
    fmt.Fprintf(w, "db is %q and store is %q\n", a.db, a.store)
    return 200, nil
}
Run Code Online (Sandbox Code Playgroud)