8

私はかなりgolangが初めてで、慣用的にこれを行うための最良の方法を見つけようとしています。

静的に定義して渡すルートの配列がありgorilla/muxます。リクエストのタイミングを合わせてパニックを処理するために、各ハンドラー関数をラップしています(主に、ラップがどのように機能するかを理解できるようにするためです)。

それぞれが「コンテキスト」にアクセスできるようにしたい-httpサーバーごとに1つになる構造体。これには、データベースハンドル、構成などが含まれる可能性があります。私がしたくないことは静的グローバル変数を使用します。

私が現在行っている方法では、ラッパーにコンテキスト構造へのアクセスを許可できますが、これを実際のハンドラーに取得する方法がわかりません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