Search code examples
gogo-gin

How to wrap route handler function gin.HandlerFunc


Given:

package main

import (
    "github.com/gin-gonic/gin"
)

func main() {
    r := gin.Default()
    r.POST("/status", handler)
    r.Run(":8080")
}

func handler(c *gin.Context) {

    var status string
    if err := c.ShouldBindJSON(&status); err != nil {
        c.JSON(503, gin.H{"status": "failed"})
    }
    c.JSON(200, gin.H{"status": "OK"})
}

the handler function is returning error messages depending what it is supposed to do, given that I have lots of functions with duplicate error handling code to produce error response.

How to I wrap this handler to externalise the error handling and response formatting. E.g.:

package main

import (
    "github.com/gin-gonic/gin"
)

func main() {
    r := gin.Default()
    r.POST("/status", wrapper(handler_status))
    r.GET("/health", wrapper(handler_health))
    r.Run(":8080")
}

func wrapper(f func(c *gin.Context) (string, error)) gin.HandlerFunc {

    status, err := f()

    if err != nil {
        c.JSON(503, gin.H{"status": err})
        return
    }
    c.JSON(200, gin.H{"status": "OK"})
}

func handler_status(c *gin.Context) (string, error) {

    var status string
    if err := c.ShouldBindJSON(&status); err != nil {
        return "failed", err
    }
    return status, nil
}

func handler_health(c *gin.Context) (string, error) {

    return "roger", nil
}

But I don't have access to *gin.Context in wrapper() ... what is the best way to solve this?


Solution

  • Thanks your suggestion, that helped :-) The correct wrapper function is:

    func wrapper(f func(c *gin.Context) (string, error)) gin.HandlerFunc {
    
        return func(c *gin.Context) {
            _, err := f(c)
            if err != nil {
                c.JSON(503, gin.H{"status": err})
                return
            }
            c.JSON(200, gin.H{"status": "OK"})
        }
    }