RespKit

HTTP frameworks

All examples use the module path github.com/ZihxS/RespKit and the same public package:

import "github.com/ZihxS/RespKit/resp"

Gin, Echo v4/v5, and Fiber v2/v3 accept their native framework contexts. Standard net/http, Chi, Gorilla/mux, and Beego v2 handlers pass an http.ResponseWriter and *http.Request through resp.HTTP.

Chi, net/http, and Gorilla/mux

Chi and Gorilla/mux register ordinary net/http handlers. The same handler works with either router:

func handler(w http.ResponseWriter, r *http.Request) {
	user := map[string]any{
		"user_id": 7,
		"name":    "Ada",
	}
	if err := resp.OK(resp.HTTP(w, r), user); err != nil {
		log.Printf("write response: %v", err)
	}
}

Register it with Chi or Gorilla/mux:

router.Get("/users", handler) // Chi
router.HandleFunc("/users", handler).Methods(http.MethodGet) // Gorilla/mux

Beego v2

Wrap the response writer and request exposed by the Beego context:

func handler(c *beecontext.Context) {
	user := map[string]any{
		"user_id": 7,
		"name":    "Ada",
	}
	if err := resp.OK(resp.HTTP(c.ResponseWriter, c.Request), user); err != nil {
		log.Printf("write response: %v", err)
	}
}

Import the context type from github.com/beego/beego/v2/server/web/context.

Gin

Pass the Gin context directly. Add a helper error to Gin's error list so the application's error middleware can inspect it:

func handler(c *gin.Context) {
	user := map[string]any{
		"user_id": 7,
		"name":    "Ada",
	}
	if err := resp.OK(c, user); err != nil {
		_ = c.Error(err)
	}
}

Echo v4 and v5

Return the helper error so Echo's configured HTTP error handler can process it. Echo v4 uses echo.Context:

func handler(c echo.Context) error {
	return resp.Fail(c, resp.ErrNotFound)
}

Echo v5 uses *echo.Context from github.com/labstack/echo/v5:

func handler(c *echo.Context) error {
	return resp.Fail(c, resp.ErrNotFound)
}

Fiber v2 and v3

Return the helper error to Fiber. Fiber v2 uses *fiber.Ctx:

func handler(c *fiber.Ctx) error {
	user := map[string]any{
		"user_id": 7,
		"name":    "Ada",
	}
	return resp.OK(c, user)
}

Fiber v3 uses fiber.Ctx from github.com/gofiber/fiber/v3:

func handler(c fiber.Ctx) error {
	user := map[string]any{
		"user_id": 7,
		"name":    "Ada",
	}
	return resp.OK(c, user)
}

Adapter details

  • The Gin adapter uses JSON(int, any), GetHeader(string), and Header(string, string).
  • The Echo adapter uses Blob(int, string, []byte), Request() *http.Request, and headers from Response().Header().
  • The Fiber adapter uses Get(string, ...string) string, Set(string, string), and Send([]byte) error. It reflects on Status(int) because Fiber v2 and v3 return different types from that method.
  • Chi, Gorilla/mux, and Beego v2 use resp.HTTP, so the core module does not import those frameworks.
  • Place RespKit at the final response point in a handler. Middleware that has already written a status or body may prevent RespKit from setting the response.

Upstream compatibility tests

The separate module at integrations/framework-tests sends requests through real upstream routers. It checks status codes, JSON bodies, Content-Language, Vary, Content-Type, error confidentiality, Location, and empty 204 responses. The compatibility guide explains where to find the selected dependency versions and their CI results. Applications should also test the versions they choose to use.

On this page