gonify

package module
v3.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2025 License: MIT Imports: 14 Imported by: 1

README

✨ Gonify Middleware for Go Frameworks ⚡

Go Report Card GoDoc License Go

A middleware for multiple Go frameworks that automatically minifies HTTP responses before sending them to clients. Powered by the efficient tdewolff/minify/v2 library.

Supports:

  • Fiber
  • Gin
  • Standard Library (net/http)
  • Chi (via net/http compatibility)

🔹 Reduces transferred data size 🔹 Saves bandwidth 🔹 Improves page load times

🚀 Features

  • ✅ Automatic minification for HTML, CSS, JS, JSON, XML, and SVG responses
  • ⚙️ Easy configuration to enable/disable specific minification types
  • ⏭️ Next option to skip middleware for specific routes/conditions
  • 🛡️ Safe error handling - original response sent if minification fails

📦 Installation

go get github.com/NarmadaWeb/gonify/v3

🛠️ Usage

Fiber
package main

import (
	"log"

	"github.com/gofiber/fiber/v3"
	"github.com/NarmadaWeb/gonify/v3"
)

func main() {
	app := fiber.New()

	// Use gonify middleware
	app.Use(gonify.New(gonify.Config{
		MinifyHTML: true,
	}))

	app.Get("/", func(c fiber.Ctx) error {
		c.Set("Content-Type", "text/html")
		return c.SendString("<html><body><h1>Hello, Minified World!</h1></body></html>")
	})

	log.Fatal(app.Listen(":3000"))
}
Gin
package main

import (
	"net/http"
	"github.com/gin-gonic/gin"
	"github.com/NarmadaWeb/gonify/v3"
)

func main() {
	r := gin.New()

	// Use gonify middleware
	r.Use(gonify.NewGin(gonify.GinConfig{
		Settings: gonify.Settings{MinifyHTML: true},
	}))

	r.GET("/", func(c *gin.Context) {
		c.Data(http.StatusOK, "text/html", []byte("<html><body><h1>Hello, Minified World!</h1></body></html>"))
	})

	r.Run(":3000")
}
Standard Library / Chi
package main

import (
	"net/http"
	"github.com/NarmadaWeb/gonify/v3"
)

func main() {
	mux := http.NewServeMux()

	mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "text/html")
		w.Write([]byte("<html><body><h1>Hello, Minified World!</h1></body></html>"))
	})

	// Wrap the mux with gonify
	handler := gonify.NewHandler(gonify.HTTPConfig{
		Settings: gonify.Settings{MinifyHTML: true},
	})(mux)

	http.ListenAndServe(":3000", handler)
}

⚙️ Configuration

The Settings struct is shared across all frameworks:

type Settings struct {
	SuppressWarnings bool // Default: false
	MinifyHTML       bool // Default: true
	MinifyCSS        bool // Default: true
	MinifyJS         bool // Default: true
	MinifyJSON       bool // Default: false
	MinifyXML        bool // Default: false
	MinifySVG        bool // Default: false
}

Framework specific configs (e.g. Config, GinConfig, HTTPConfig) embed Settings and add a Next function for conditional skipping.

🤝 Contributing

Contributions are always welcome! Fork the repository, create a feature branch, and submit a Pull Request.

📜 License

This package is licensed under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ConfigDefault = Config{
	Next:             nil,
	SuppressWarnings: false,
	MinifyHTML:       true,
	MinifyCSS:        true,
	MinifyJS:         true,
	MinifyJSON:       false,
	MinifyXML:        false,
	MinifySVG:        false,
}

ConfigDefault is the default config

View Source
var DefaultGinConfig = GinConfig{
	Next:     nil,
	Settings: DefaultSettings,
}

DefaultGinConfig is the default config

View Source
var DefaultHTTPConfig = HTTPConfig{
	Next:     nil,
	Settings: DefaultSettings,
}

DefaultHTTPConfig is the default config

View Source
var DefaultSettings = Settings{
	SuppressWarnings: false,
	MinifyHTML:       true,
	MinifyCSS:        true,
	MinifyJS:         true,
	MinifyJSON:       false,
	MinifyXML:        false,
	MinifySVG:        false,
}

DefaultSettings is the default settings

Functions

func New

func New(config ...Config) fiber.Handler

New creates a new middleware handler

func NewGin

func NewGin(config ...GinConfig) gin.HandlerFunc

NewGin creates a new middleware handler for Gin

func NewHandler

func NewHandler(config ...HTTPConfig) func(http.Handler) http.Handler

NewHandler creates a new middleware handler for net/http (compatible with Chi)

Types

type Config

type Config struct {
	// Optional. Default: nil
	Next func(c *fiber.Ctx) bool

	// Optional. Default: false
	SuppressWarnings bool

	MinifyHTML bool
	MinifyCSS  bool
	MinifyJS   bool
	MinifyJSON bool
	MinifyXML  bool
	MinifySVG  bool
}

Config defines the config for middleware.

type GinConfig

type GinConfig struct {
	// Optional. Default: nil
	Next func(c *gin.Context) bool

	Settings
}

GinConfig defines the config for Gin middleware.

type HTTPConfig

type HTTPConfig struct {
	// Optional. Default: nil
	Next func(r *http.Request) bool

	Settings
}

HTTPConfig defines the config for Stdlib/Chi middleware.

type Settings

type Settings struct {
	// Optional. Default: false
	SuppressWarnings bool

	MinifyHTML bool
	MinifyCSS  bool
	MinifyJS   bool
	MinifyJSON bool
	MinifyXML  bool
	MinifySVG  bool
}

Settings defines the common settings for minification.

Jump to

Keyboard shortcuts

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