func 共通化

func newHandlerFunc(handleFunc func(w http.ResponseWriter, r *http.Request)) http.Handler {
    handler := http.HandlerFunc(handleFunc)
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "common!")
        handler.ServeHTTP(w, r)
    })
}

func testFunc(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "test!") }

func main() {
    http.Handle("/", newHandlerFunc(testFunc))
    http.ListenAndServe(":8080", nil)
}

gin

func init() {
    err := godotenv.Load(fmt.Sprintf("kintone.env.%s", "dev"))
    if err != nil {
        log.Fatal("Error loading .env file")
    }
    r := gin.Default()
    h := newHandleFunc(testFunc)
    r.GET("/", h)
    http.Handle("/", r)
}

func newHandleFunc(handleFunc func(*gin.Context, *kintone.Client)) func(*gin.Context) {
    return func(c *gin.Context) {
        c.String(200, "base!")
        ctx := appengine.NewContext(c.Request)
        client, _ := newClient(ctx, os.Getenv("KINTONE_APPID"))
        handleFunc(c, client)
    }
}

func testFunc(c *gin.Context, client *kintone.Client) {
    c.String(200, "test!!")
    r, _ := client.GetRecord("39")
    c.String(200, r.ID)
}

gin wrap

func init() {
    r := gin.Default()
    r.GET("/", gin.WrapF(newHandlerFunc(testFunc)))
    http.Handle("/", r)
}

func newHandlerFunc(handleFunc func(w http.ResponseWriter, r *http.Request)) http.HandlerFunc {
    handler := http.HandlerFunc(handleFunc)
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "common!")
        handler.ServeHTTP(w, r)
    })
}

func testFunc(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "test!") }