echocbor

package module
v0.0.0-...-3588d74 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 15, 2025 License: Apache-2.0 Imports: 3 Imported by: 0

README

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.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContextWrapper

func ContextWrapper(next echo.HandlerFunc) echo.HandlerFunc

ContextWrapper wraps context for sending a Cbor response.

Types

type Binder

type Binder struct{}

Binder for Cbor.

func (*Binder) Bind

func (b *Binder) Bind(i interface{}, c echo.Context) error

Bind binds a request body to given interface.

type Context

type Context interface {
	echo.Context
	Cbor(code int, i interface{}) error
}

Context extends echo.Context to include Cbor response functionality.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL