A thin wrapper around Go's standard net/http with ergonomic routing, middleware, and response helpers. No external dependencies.
go get github.com/bobTheBuilder7/httpxHandlers return error, and you supply a central errHandler to convert errors into HTTP responses.
errHandler := func(h httpx.ErrorHandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if err := h(w, r); err != nil {
utils.TextResponse(w, err.Error(), http.StatusInternalServerError)
}
}
}
router := httpx.NewRouter(errHandler)
router.GET("/users", listUsers)
router.POST("/users", createUser)Groups inherit the parent's middlewares and prepend a base path.
api := router.NewGroup("/api/v1", authMiddleware)
api.GET("/users", listUsers) // → GET /api/v1/users
api.POST("/users", createUser) // → POST /api/v1/userstype Middleware func(h httpx.ErrorHandlerFunc) httpx.ErrorHandlerFunc
func logging(next httpx.ErrorHandlerFunc) httpx.ErrorHandlerFunc {
return func(w http.ResponseWriter, r *http.Request) error {
log.Println(r.Method, r.URL.Path)
return next(w, r)
}
}
router := httpx.NewRouter(errHandler, logging)// Server with HTTP/1 enabled
server := httpx.NewServer(router)
server.Addr = ":8080"
server.ListenAndServe()
// Client with sensible defaults (HTTP/2, connection pooling, timeouts)
client := httpx.NewClient()import "github.com/bobTheBuilder7/httpx/utils"
func handler(w http.ResponseWriter, r *http.Request) error {
return utils.JSONResponse(w, map[string]string{"status": "ok"}, http.StatusOK)
}
func hello(w http.ResponseWriter, r *http.Request) error {
return utils.TextResponse(w, "hello", http.StatusOK)
}