上周受本文启发,我正在重构我必须更明确地将上下文(数据库池,会话存储等)传递给处理程序的应用程序。
但是,我遇到的一个问题是,如果没有全局模板映射,ServeHTTP我的自定义处理程序类型(要满足http.Handler)上的方法将无法再访问该映射以呈现模板。
ServeHTTP
http.Handler
我需要保留全局templates变量,或者将我的自定义处理程序类型重新定义为结构。
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 }
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 }
有没有更干净的方法将context实例传递给ServeHTTP?
context
请注意,这go build -gcflags=-m表明在堆分配团队中,这两个选项似乎都不差:&appContext在两种情况下,文字均逃逸到堆(如预期的那样),尽管我的解释是基于struct的选项确实context在每个堆上传递了第二个指针()。要求- 如果我在这里错了 ,请 纠正我, 因为我想更好地理解这一点。
go build -gcflags=-m
&appContext
我并不完全相信全局变量在main包中是不好的(即不是lib),只要它们可以安全地以这种方式使用(只读/ mutexs /一个池),但是我确实很清楚必须显式传递上下文提供的内容。
在与#go-nuts上的几个有用的Gophers进行了讨论之后,根据我的判断,上述方法是“尽其所能”。
请注意,我们不能定义我们的处理程序上的方法appHandler,即func (ah *appHandler) IndexHandler(...)因为我们需要调用的处理程序ServeHTTP(即ah.h(w,r))。
appHandler
func (ah *appHandler) IndexHandler(...)
ah.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}) ...
最重要的是,它也与完全兼容,http.Handler因此我们仍然可以使用通用中间件来包装处理程序结构,如下所示:gzipHandler(appHandler{context.IndexHandler, context})。
gzipHandler(appHandler{context.IndexHandler, context})
(不过,我仍然愿意接受其他建议!)
更新资料
多亏了Reddit的这一出色答复,我才能够找到一个更好的解决方案,该解决方案不需要为context每个请求将两个引用传递给我的实例。
相反,我们仅创建一个接受嵌入式上下文和我们的处理程序类型的结构,并且http.Handler由于,我们仍然满足该接口ServeHTTP。处理程序不再是我们appContext类型的方法,而只是将其作为参数接受,这导致函数签名稍长,但仍然“显而易见”且易于阅读。如果我们担心“类型化”,那么我们将达到收支平衡,因为我们不再需要担心方法接收者。
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 }