Skip to content

itpey/echocbor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Echo CBOR Middleware

Go Version License GoDoc Go Report Card

Echo CBOR is a middleware for the Echo web framework that enables encoding and decoding of CBOR (Concise Binary Object Representation) data in HTTP requests and responses using the fxamacker/cbor library.

Features

  • Adds a Cbor method to the Echo context for sending CBOR-encoded responses.
  • Provides a custom Binder to decode CBOR request bodies.
  • Middleware to wrap the Echo context for CBOR support.
  • Lightweight and easy to integrate with existing Echo applications.

Installation

To install the package, use the following command:

go get -u github.com/itpey/echocbor

Usage

Setting Up the Middleware

To use the CBOR middleware, wrap your Echo handlers with the ContextWrapper middleware and set the custom Binder. Here's an example:

package main

import (
    "net/http"

    "github.com/labstack/echo/v4"
    "github.com/itpey/echocbor"
)

func main() {
    e := echo.New()

    // Set the custom CBOR binder
    e.Binder = &echocbor.Binder{}

    // Use the CBOR context wrapper middleware
    e.Use(echocbor.ContextWrapper)

    // Example route
    e.POST("/example", func(c echo.Context) error {
        // Define a struct to bind the request body
        type Payload struct {
            Name string `json:"name" cbor:"name"`
            Age  int    `json:"age" cbor:"age"`
        }

        // Bind the CBOR request body
        var payload Payload
        if err := c.Bind(&payload); err != nil {
            return err
        }

        // Send a CBOR response
        return c.(echocbor.Context).Cbor(http.StatusOK, map[string]interface{}{
            "message": "Received",
            "data":    payload,
        })
    })

    e.Logger.Fatal(e.Start(":8080"))
}

Sending CBOR Responses

The Cbor method allows you to send a CBOR-encoded response with a specified HTTP status code. For example:

e.GET("/data", func(c echo.Context) error {
    data := map[string]interface{}{
        "id":   1,
        "name": "John Doe",
    }
    return c.(echocbor.Context).Cbor(http.StatusOK, data)
})

Feedback and Contributions

If you encounter any issues or have suggestions for improvement, please open an issue on GitHub.

We welcome contributions! Fork the repository, make your changes, and submit a pull request.

Support

If you enjoy using Echo CBOR, please consider giving it a star! Your support helps others discover the project and encourages further development.

License

Echo CBOR is open-source software released under the Apache License, Version 2.0. You can find a copy of the license in the LICENSE file.