BetterHandler

BetterHandler is a handler with some useful features that implements the standard net/http handler interface.

Feautures

  • Writing String into ResponseWriter
  • Writing JSON into ResponseWriter
  • Writing XML into ResponseWriter

    Installation
    go get -u github.com/4kord/BetterHandler

    Quickstart

    package main
    
    import (
        "net/http"
        "github.com/4kord/BetterHandler"
    )
    
    func main() {
        http.ListenAndServe(":3000", BetterHandler.BH(func(c *BetterHandler.Ctx) {
            c.rw.WriteHeader(http.StatusOK)
    
            c.String("Hello, World!")
        }))
    }

    Examples

    Writing String into ResponseWriter

    func handler(c *BetterHandler.Ctx) {
        c.String("Hello, World!")
    }
    Writing JSON into ResponseWriter

    type Json struct {
        Key1 string  `json:"key1"`
        Key2 int     `json:"key2"`
        Key3 float64 `json:"key3"`
    }
    
    func handler(c *BetterHandler.Ctx) {
        c.JSON(Json{
            Key1: "value1",
            Key2: 10,
            Key3: 3.14,
        })
    }
    
    func handler2(c *BetterHandler.Ctx) {
        c.JSON(BetterHandler.Map{
            "Key1": "value1",
            "Key2": 10,
            "Key3": 3.14,
        })
    }
    Writing XML into ResponseWriter

    type Xml struct {
        Key1 string  `xml:"key1"`
        Key2 int     `xml:"key2"`
        Key3 float64 `xml:"key3"`
    }
    
    func handler(c *BetterHandler.Ctx) {
        c.XML(Xml{
            Key1: "value1",
            Key2: 10,
            Key3: 3.14,
        })
    }
    
    func handler2(c *BetterHandler.Ctx) {
        c.XML(BetterHandler.Map{
            "Key1": "value1",
            "Key2": 10,
            "Key3": 3.14,
        })
    }
    Parsing request body into a struct
    STRUCT TAGS: application/json – json, application/xml – xml, multipart/form-data – form

    type V struct {
        Key1 string                  `form:"key1"`
        Key2 int                     `form:"key2"`
        Key3 float64                 `form:"key3"`
        Key4 []*Multipart.FileHeader `form:"key4"`
    }
    
    func handler(c *BetterHandler.Ctx) {
        var myStruct V
        
        c.BodyParser(&myStruct)
    }
    Getting base url

    func handler(c *BetterHandler.Ctx) {
        fmt.Println(c.BaseUrl())
    }
    Set cookie

    func handler(c *BetterHandler.Ctx) {
        cookie := &http.Cookie{
            Name: "myCookie",
            Value: "value",
            HttpOnly: true,
        }
    
        c.SetCookie(cookie)
    }
    Get cookie

    func handler(c *BetterHandler.Ctx) {
        cookie, err := c.GetCookie("myCookie")
        if err != nil {
            if errors.Is(err, http.ErrNoCookie) {
                fmt.Println("Cookie not found")
                return
            }
            fmt.Println("Unexpected error")
        }
    }
    Expire a client cookie / all cookies

    func handler(c *BetterHandler.Ctx) {
        c.ClearCookie("myCookie") // Expire myCookie
    }
    
    func handler2(c *BetterHandler.Ctx) {
        c.ClearCookie("myCookie2") // Expire myCookie, myCookie2
    }
    
    func handler3(c *BetterHandler.Ctx) {
        c.ClearCookie() // Expire all cookies
    }

    GitHub

    View Github