renderer

package module
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2026 License: MIT Imports: 4 Imported by: 0

README

renderer

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Handle

func Handle(h HandlerFunc) http.HandlerFunc

Types

type BytesResponse

type BytesResponse struct {
	// contains filtered or unexported fields
}

func Bytes

func Bytes(status int, data []byte, contentType string) *BytesResponse

func (*BytesResponse) Header

func (r *BytesResponse) Header(key, value string) *BytesResponse

func (*BytesResponse) Render

func (r *BytesResponse) Render(w http.ResponseWriter) error

type FileResponse

type FileResponse struct {
	// contains filtered or unexported fields
}

func File

func File(status int, reader io.Reader, name string) *FileResponse

func (*FileResponse) Header

func (f *FileResponse) Header(key, value string) *FileResponse

func (*FileResponse) Inline

func (f *FileResponse) Inline() *FileResponse

func (*FileResponse) Render

func (f *FileResponse) Render(w http.ResponseWriter) error

type HandlerFunc

type HandlerFunc func(r *http.Request) (Response, error)

type JSONResponse

type JSONResponse[T any] struct {
	// contains filtered or unexported fields
}

func JSON

func JSON[T any](status int, data T) *JSONResponse[T]

func (*JSONResponse[T]) Header

func (r *JSONResponse[T]) Header(key, value string) *JSONResponse[T]

func (*JSONResponse[T]) Indent

func (r *JSONResponse[T]) Indent() *JSONResponse[T]

func (*JSONResponse[T]) Render

func (r *JSONResponse[T]) Render(w http.ResponseWriter) error

type NoContentResponse

type NoContentResponse struct {
	// contains filtered or unexported fields
}

func NoContent

func NoContent() *NoContentResponse

func (*NoContentResponse) Render

type Response

type Response interface {
	Render(w http.ResponseWriter) error
}

type TextResponse

type TextResponse struct {
	// contains filtered or unexported fields
}

func Text

func Text(status int, s string) *TextResponse

func (*TextResponse) Header

func (r *TextResponse) Header(key, value string) *TextResponse

func (*TextResponse) Render

func (r *TextResponse) Render(w http.ResponseWriter) error

type XMLResponse

type XMLResponse[T any] struct {
	// contains filtered or unexported fields
}

func XML

func XML[T any](status int, data T) *XMLResponse[T]

func (*XMLResponse[T]) Header

func (r *XMLResponse[T]) Header(key, value string) *XMLResponse[T]

func (*XMLResponse[T]) Indent

func (r *XMLResponse[T]) Indent() *XMLResponse[T]

func (*XMLResponse[T]) Render

func (r *XMLResponse[T]) Render(w http.ResponseWriter) error

Jump to

Keyboard shortcuts

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