You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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"
)
funcmain() {
// Create a new muxmux:=httpz.NewServeMux()
// add logger middleware, it 's copy from chi/middlewaremux.Use(middleware.Logger)
// register a GET /hello route// GET /hellomux.Get("/hello", func(w http.ResponseWriter, r*http.Request) error {
returnhttpz.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 ServerMuxhttp.ListenAndServe(":8080", mux)
}
the middleware package is copied from chi/middleware.
The complete example can be found in the _example directory