jsendx

package
v1.80.7 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2023 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package jsendx implements a custom JSEND model to wrap HTTP responses in a JSON object with default fields.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultIPHandler

func DefaultIPHandler(info *AppInfo, fn httpserver.GetPublicIPFunc) http.HandlerFunc

DefaultIPHandler returns the route ip in JSendX format.

func DefaultIndexHandler

func DefaultIndexHandler(info *AppInfo) httpserver.IndexHandlerFunc

DefaultIndexHandler returns the route index in JSendX format.

func DefaultMethodNotAllowedHandlerFunc

func DefaultMethodNotAllowedHandlerFunc(info *AppInfo) http.HandlerFunc

DefaultMethodNotAllowedHandlerFunc http handler called when a request cannot be routed.

func DefaultNotFoundHandlerFunc

func DefaultNotFoundHandlerFunc(info *AppInfo) http.HandlerFunc

DefaultNotFoundHandlerFunc http handler called when no matching route is found.

func DefaultPanicHandlerFunc

func DefaultPanicHandlerFunc(info *AppInfo) http.HandlerFunc

DefaultPanicHandlerFunc http handler to handle panics recovered from http handlers.

func DefaultPingHandler

func DefaultPingHandler(info *AppInfo) http.HandlerFunc

DefaultPingHandler returns a ping request in JSendX format.

func DefaultStatusHandler

func DefaultStatusHandler(info *AppInfo) http.HandlerFunc

DefaultStatusHandler returns the server status in JSendX format.

func HealthCheckResultWriter

func HealthCheckResultWriter(info *AppInfo) healthcheck.ResultWriter

HealthCheckResultWriter returns a new healthcheck result writer.

func NewRouter

func NewRouter(info *AppInfo, instrumentHandler httpserver.InstrumentHandler) *httprouter.Router

NewRouter is deprecated. Deprecated: Set instead the router error handlers with the following options:

httpserver.WithNotFoundHandlerFunc(jsendx.DefaultNotFoundHandlerFunc(appInfo))
httpserver.WithMethodNotAllowedHandlerFunc(jsendx.DefaultMethodNotAllowedHandlerFunc(appInfo))
httpserver.WithPanicHandlerFunc(jsendx.DefaultPanicHandlerFunc(appInfo))

func Send

func Send(ctx context.Context, w http.ResponseWriter, statusCode int, info *AppInfo, data any)

Send sends a JSON respoonse wrapped in a JSendX container.

Types

type AppInfo

type AppInfo struct {
	ProgramName    string
	ProgramVersion string
	ProgramRelease string
}

AppInfo is a struct containing data to enrich the JSendX response.

type Response

type Response struct {
	// Program is the application name.
	Program string `json:"program"`

	// Version is the program semantic version (e.g. 1.2.3).
	Version string `json:"version"`

	// Release is the program build number that is appended to the version.
	Release string `json:"release"`

	// DateTime is the human-readable date and time when the response is sent.
	DateTime string `json:"datetime"`

	// Timestamp is the machine-readable UTC timestamp in nanoseconds since EPOCH.
	Timestamp int64 `json:"timestamp"`

	// Status code string (i.e.: error, fail, success).
	Status httputil.Status `json:"status"`

	// Code is the HTTP status code number.
	Code int `json:"code"`

	// Message is the error or general HTTP status message.
	Message string `json:"message"`

	// Data is the content payload.
	Data any `json:"data"`
}

Response wraps data into a JSend compliant response.

func Wrap

func Wrap(statusCode int, info *AppInfo, data any) *Response

Wrap sends an Response object.

type RouterArgs

type RouterArgs struct {
	// TraceIDHeaderName is the Trace ID header name.
	TraceIDHeaderName string

	// RedactFunc is the function used to redact HTTP request and response dumps in the logs.
	RedactFunc httpserver.RedactFn
}

RouterArgs extra arguments for the router.

Jump to

Keyboard shortcuts

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