8

我对 golang 相当陌生,并且正在尝试找出以惯用方式执行此操作的最佳方法。

我有一组静态定义并传递给的路由gorilla/mux。我用一些东西来包装每个处理函数来计时请求和处理恐慌(主要是为了让我能理解包装是如何工作的)。

我希望他们每个人都能够访问一个“上下文”——一个每个 http-server 一个的结构,它可能有数据库句柄、配置等。我不想做的是使用静态全局变量。

我目前这样做的方式可以让包装器访问上下文结构,但我看不到如何将它放入实际的处理程序中,因为它希望它是一个http.HandlerFunc. 我想我能做的就是转换http.HandlerFunc成我自己的类型,它是一个接收器Context(并且对包装器也做类似的事情,但是(经过多次尝试)我无法Handler()接受这一点。

我不禁认为我在这里遗漏了一些明显的东西。代码如下。

package main

import (
    "fmt"
    "github.com/gorilla/mux"
    "html"
    "log"
    "net/http"
    "time"
)

type Route struct {
    Name        string
    Method      string
    Pattern     string
    HandlerFunc http.HandlerFunc
}

type Context struct {
    route *Route
    // imagine other stuff here, like database handles, config etc.
}

type Routes []Route

var routes = Routes{
    Route{
        "Index",
        "GET",
        "/",
        index,
    },
    // imagine lots more routes here
}

func wrapLogger(inner http.Handler, context *Context) http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        start := time.Now()

        inner.ServeHTTP(w, r)

        log.Printf(
            "%s\t%s\t%s\t%s",
            r.Method,
            r.RequestURI,
            context.route.Name,
            time.Since(start),
        )
    })
}

func wrapPanic(inner http.Handler, context *Context) http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        defer func() {
            if err := recover(); err != nil {
                log.Printf("panic caught: %+v", err)
                http.Error(w, http.StatusText(500), 500)
            }
        }()

        inner.ServeHTTP(w, r)
    })
}

func newRouter() *mux.Router {

    router := mux.NewRouter().StrictSlash(true)
    for _, route := range routes {
        // the context object is created here
        context := Context {
            &route,
            // imagine more stuff here
        }
        router.
            Methods(route.Method).
            Path(route.Pattern).
            Name(route.Name).
            Handler(wrapLogger(wrapPanic(route.HandlerFunc, &context), &context))
    }

    return router
}

func index(w http.ResponseWriter, r *http.Request) {
    // I want this function to be able to have access to 'context'
    fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
}

func main() {
    fmt.Print("Starting\n");
    router := newRouter()
    log.Fatal(http.ListenAndServe("127.0.0.1:8080", router))
}

这是一种方法,但它看起来很可怕。我不禁认为必须有一些更好的方法来做到这一点 - 也许是子类 (?) http.Handler

package main

import (
    "fmt"
    "github.com/gorilla/mux"
    "html"
    "log"
    "net/http"
    "time"
)

type Route struct {
    Name        string
    Method      string
    Pattern     string
    HandlerFunc ContextHandlerFunc
}

type Context struct {
    route  *Route
    secret string
}

type ContextHandlerFunc func(c *Context, w http.ResponseWriter, r *http.Request)

type Routes []Route

var routes = Routes{
    Route{
        "Index",
        "GET",
        "/",
        index,
    },
}

func wrapLogger(inner ContextHandlerFunc) ContextHandlerFunc {
    return func(c *Context, w http.ResponseWriter, r *http.Request) {
        start := time.Now()

        inner(c, w, r)

        log.Printf(
            "%s\t%s\t%s\t%s",
            r.Method,
            r.RequestURI,
            c.route.Name,
            time.Since(start),
        )
    }
}

func wrapPanic(inner ContextHandlerFunc) ContextHandlerFunc {
    return func(c *Context, w http.ResponseWriter, r *http.Request) {
        defer func() {
            if err := recover(); err != nil {
                log.Printf("panic caught: %+v", err)
                http.Error(w, http.StatusText(500), 500)
            }
        }()

        inner(c, w, r)
    }
}

func newRouter() *mux.Router {

    router := mux.NewRouter().StrictSlash(true)
    for _, route := range routes {
        context := Context{
            &route,
            "test",
        }
        router.Methods(route.Method).
            Path(route.Pattern).
            Name(route.Name).
            HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
            wrapLogger(wrapPanic(route.HandlerFunc))(&context, w, r)
        })
    }

    return router
}

func index(c *Context, w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello, %q secret is %s\n", html.EscapeString(r.URL.Path), c.secret)
}

func main() {
    fmt.Print("Starting\n")
    router := newRouter()
    log.Fatal(http.ListenAndServe("127.0.0.1:8080", router))
}
4

1 回答 1

8

我正在学习 Go,目前遇到了一个几乎相同的问题,这就是我处理它的方式:


首先,我认为你错过了一个重要的细节:Go 中没有全局变量。变量的最广泛范围是包范围。Go 中唯一真正的全局变量是预先声明的标识符,例如trueand false(您不能更改这些标识符或制作自己的标识符)。

因此,设置一个作用域为的变量package main来保存程序的上下文是非常好的。来自 C/C++ 背景,这花了我一点时间来适应。由于变量是包作用域的,它们不会受到全局变量问题的影响。如果另一个包中的某些东西需要这样的变量,则必须显式传递它。

在有意义的时候不要害怕使用包变量。这可以帮助您降低程序的复杂性,并且在很多情况下使您的自定义处理程序更加简单(调用http.HandlerFunc()和传递闭包就足够了)。

这样一个简单的处理程序可能如下所示:

func simpleHandler(c Context, next http.Handler) http.Handler {
  return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    // FIXME Do something with our context
    next.ServeHTTP(w, r)
  })
}

并由以下人员使用:

r = mux.NewRouter()
http.Handle("/", simpleHandler(c, r))

如果您的需求更复杂,您可能需要实现自己的http.Handler. 请记住, anhttp.Handler只是实现的接口ServeHTTP(w http.ResponseWriter, r *http.Request)

这是未经测试的,但应该能让你完成大约 95% 的工作:

package main

import (
    "net/http"
)

type complicatedHandler struct {
    h    http.Handler
    opts ComplicatedOptions
}

type ComplicatedOptions struct {
    // FIXME All of the variables you want to set for this handler
}

func (m complicatedHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    // FIXME Do stuff before serving page

    // Call the next handler
    m.h.ServeHTTP(w, r)

    // FIXME Do stuff after serving page
}

func ComplicatedHandler(o ComplicatedOptions) func(http.Handler) http.Handler {
    return func(h http.Handler) http.Handler {
        return complicatedHandler{h, o}
    }
}

要使用它:

r := mux.NewRouter()
// FIXME: Add routes to the mux

opts := ComplicatedOptions{/* FIXME */}
myHandler := ComplicatedHandler(opts)

http.Handle("/", myHandler(r))

有关更发达的处理程序示例,请参阅goji/httpauth 中的 basicAuth,此示例被无耻地抄袭。


一些进一步的阅读:

于 2015-07-10T00:41:32.093 回答