Skip to content
/ httpz Public

Lightweight net/http 1.22+ enhancement library that combines the best of chi & Echo Enabling you to effortlessly write handlers using net/http

License

Notifications You must be signed in to change notification settings

aeilang/httpz

Repository files navigation

Logo

httpz v1.0.0 is release, its API is stable.

简体中文

httpz is not a new framework but rather an enhancement library for net/http. When you use httpz, you’re essentially working with the net/http standard library. Its goal is to address the usability gap in net/http 1.22, which, despite its improved routing capabilities, remains less convenient compared to frameworks like Echo and chi.

Key Features:

  • Built on net/http version 1.22+
  • Global error handling
  • Convenient grouping
  • Middleware adopted from chi
  • Data binding adopted from Echo
  • Easy response shortcuts

httpz is fully compatible with net/http. You can choose to leverage the enhanced features of httpz or stick to using plain net/http directly.

Quick Start

1.Installation

To install httpz, Go 1.22 or higher is required.

go get github.com/aeilang/httpz

2.Hello World

import (
	"net/http"

	"github.com/aeilang/httpz"
	"github.com/aeilang/httpz/middleware"
)

func main() {
	// Create a new mux
	mux := httpz.NewServeMux()

	// add logger middleware, it 's copy from chi/middleware
	mux.Use(middleware.Logger)

	// register a GET /hello route
	// GET /hello
	mux.Get("/hello", func(w http.ResponseWriter, r *http.Request) error {
		return httpz.JSON(w, http.StatusOK, "hello httpz")

		// or
		// hw is a helper responsewriter to send response
		// hw := httpz.NewHelperRW(w)
		// return hw.String(http.StatusOK, "hello httpz")
		
		// or you can write it by yourself.
		// hw.Header().Set("Content-Type", "text/plain; charset=UTF-8")
		// hw.WriteHeader(http.StatusOK)
		// hw.Write([]byte("hello httpz"))
		// return nil
	})
  
  // just like net/http's ServerMux
	http.ListenAndServe(":8080", mux)
}

the middleware package is copied from chi/middleware.

The complete example can be found in the _example directory

Benchmark

Generated by Go web framework benchmark:

benchmark

For detailed benchmark results, please refer to benchmark.

Feel free to contribute your code.

  • test

  • example

  • middleware

  • other

About

Lightweight net/http 1.22+ enhancement library that combines the best of chi & Echo Enabling you to effortlessly write handlers using net/http

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages