Skip to content

thedevsaddam/renderer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Simple, lightweight and faster response (JSON, JSONP, XML, YAML, HTML, File) rendering package for Go

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages