Table of contents
Part of nurago, a collection of independent Go packages for backend services.
import "github.com/tecnickcom/nurago/pkg/httputil/jsendx"
Package jsendx implements an extended JSend response envelope for HTTP APIs.
The package wraps payloads in a Response schema that extends JSend with runtime
metadata:
- program name, version, and release
- RFC3339 datetime and Unix-nanosecond timestamp
- HTTP code/message and JSend status projection
- arbitrary response data payload
The JSXResp helper integrates with httputil
to send wrapped JSON responses and provides default handlers for common service
endpoints and router fallbacks (not-found, method-not-allowed, panic, index,
ping, status, and public-IP), plus a healthcheck.ResultWriter adapter via
JSXResp.HealthCheckResultWriter.
When To Use
- An API contract specifies the JSend format for success, failure, and error responses.
- You want every handler in a service to answer with the same envelope shape.
Example
info := &jsendx.AppInfo{
ProgramName: "demo",
ProgramVersion: "1.0.0",
ProgramRelease: "1",
}
resp := jsendx.Wrap(http.StatusOK, info, "payload")
// Only the deterministic fields are printed; DateTime and Timestamp vary per call.
fmt.Println(resp.Program, resp.Version, resp.Release, resp.Code, resp.Message, resp.Data)
// Output:
// demo 1.0.0 1 200 OK payload
Full source is in example_jsendx_test.go. More runnable examples are on pkg.go.dev.
Dependencies
Importing this package pulls 1 external module:
github.com/julienschmidt/httprouter