renderer

April 22, 2026 ยท View on GitHub

A minimal, fast, and production-ready HTTP response renderer for Go.

It provides a clean abstraction over net/http responses with support for JSON, XML, Text, Bytes, Files, and a safe handler adapter compatible with Chi and standard library HTTP.


โœจ Features

  • โšก Zero-dependency core (stdlib only)
  • ๐Ÿง  Clean Response abstraction
  • ๐Ÿ” Panic-safe HTTP handler wrapper
  • ๐Ÿ“ฆ JSON / XML / Text / Bytes / File responses
  • ๐Ÿ“ Streaming file support (no memory overload)
  • ๐Ÿ”ง Header support on all responses
  • ๐Ÿงฉ Chi / net/http compatible
  • ๐Ÿงช Fully testable design

๐Ÿ“ฆ Installation

go get github.com/thedevsaddam/renderer/v2

๐Ÿš€ Quick Start

package main

import (
	"net/http"

	"github.com/go-chi/chi/v5"
	"github.com/thedevsaddam/renderer/v2"
)

func main() {
	r := chi.NewRouter()

	r.Get("/hello", renderer.Handle(func(r *http.Request) (renderer.Response, error) {
		return renderer.JSON(200, map[string]string{
			"message": "hello renderer v2",
		}), nil
	}))

	http.ListenAndServe(":8080", r)
}

๐Ÿ“ค JSON Response

renderer.JSON(200, map[string]string{
	"name": "Saddam",
})

With indentation

renderer.JSON(200, data).Indent()

Add headers

renderer.JSON(200, data).
	Header("X-App", "renderer")

๐Ÿ“ Text Response

renderer.Text(200, "hello world")

๐Ÿ“„ XML Response

type User struct {
	Name string `xml:"name"`
}

renderer.XML(200, User{Name: "Saddam"})

๐Ÿ“ฆ Raw Bytes

renderer.Bytes(200, []byte("hello"), "application/octet-stream")

๐Ÿ“ File Streaming

Download file

file := strings.NewReader("file content")

renderer.File(200, file, "report.txt")

Inline file

renderer.File(200, file, "report.txt").Inline()

โš™๏ธ Handler Adapter

type HandlerFunc func(r *http.Request) (Response, error)
r.Get("/ping", renderer.Handle(func(r *http.Request) (renderer.Response, error) {
	return renderer.JSON(200, map[string]any{
		"ping": "pong",
	}), nil
}))

๐Ÿ’ฅ Panic Safety

All handlers are automatically protected from panics and return HTTP 500 safely.


๐Ÿง  Design Philosophy

  • Explicit over magic
  • Composition over framework lock-in
  • Streaming-first design
  • Minimal API surface

๐Ÿงช Testing

go test ./...

โšก Benchmarks

go test ./... -bench=.

๐Ÿ“„ License

MIT