action

package module
v0.1.3-alpha Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2025 License: MIT Imports: 6 Imported by: 0

README

GitHub Actions Workflow Status Go Reference

chi-action

Generic HTTP request handler abstraction using github.com/go-chi/chi/v5 and github.com/go-chi/render.

Usage

package main

import (
    "context"
    "io"
    "net"
    "net/http"

    "github.com/bdreece/chi-action"
    "github.com/go-chi/chi/v5"
    "github.com/go-chi/render"
)

func main() {
    mux := chi.NewMux()

    mux.Post("/echo", action.HandleFunc(echo))

    // ...
}

func echo(ctx context.Context, req *echoRequest) (*echoResponse, error) {
    return &echoResponse(*echoRequest), nil
}

type (
    echoRequest string
    echoResponse string
)

func (req *echoRequest) Bind(r *http.Request) error {
    return render.DecodeJSON(r.Body, req)
}


func (res *echoResponse) Render(w http.ResponseWriter, r *http.Request) error {
    render.JSON(w, r, res)
    return nil
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrBadRequest                    = NewError(http.StatusBadRequest)                    // RFC 9110, 15.5.1
	ErrUnauthorized                  = NewError(http.StatusUnauthorized)                  // RFC 9110, 15.5.2
	ErrPaymentRequired               = NewError(http.StatusPaymentRequired)               // RFC 9110, 15.5.3
	ErrForbidden                     = NewError(http.StatusForbidden)                     // RFC 9110, 15.5.4
	ErrNotFound                      = NewError(http.StatusNotFound)                      // RFC 9110, 15.5.5
	ErrMethodNotAllowed              = NewError(http.StatusMethodNotAllowed)              // RFC 9110, 15.5.6
	ErrNotAcceptable                 = NewError(http.StatusNotAcceptable)                 // RFC 9110, 15.5.7
	ErrProxyAuthRequired             = NewError(http.StatusProxyAuthRequired)             // RFC 9110, 15.5.8
	ErrRequestTimeout                = NewError(http.StatusRequestTimeout)                // RFC 9110, 15.5.9
	ErrConflict                      = NewError(http.StatusConflict)                      // RFC 9110, 15.5.10
	ErrGone                          = NewError(http.StatusGone)                          // RFC 9110, 15.5.11
	ErrLengthRequired                = NewError(http.StatusLengthRequired)                // RFC 9110, 15.5.12
	ErrPreconditionFailed            = NewError(http.StatusPreconditionFailed)            // RFC 9110, 15.5.13
	ErrRequestEntityTooLarge         = NewError(http.StatusRequestEntityTooLarge)         // RFC 9110, 15.5.14
	ErrRequestURITooLong             = NewError(http.StatusRequestURITooLong)             // RFC 9110, 15.5.15
	ErrUnsupportedMediaType          = NewError(http.StatusUnsupportedMediaType)          // RFC 9110, 15.5.16
	ErrRequestedRangeNotSatisfiable  = NewError(http.StatusRequestedRangeNotSatisfiable)  // RFC 9110, 15.5.17
	ErrExpectationFailed             = NewError(http.StatusExpectationFailed)             // RFC 9110, 15.5.18
	ErrTeapot                        = NewError(http.StatusTeapot)                        // RFC 9110, 15.5.19 (Unused)
	ErrMisdirectedRequest            = NewError(http.StatusMisdirectedRequest)            // RFC 9110, 15.5.20
	ErrUnprocessableEntity           = NewError(http.StatusUnprocessableEntity)           // RFC 9110, 15.5.21
	ErrLocked                        = NewError(http.StatusLocked)                        // RFC 4918, 11.3
	ErrFailedDependency              = NewError(http.StatusFailedDependency)              // RFC 4918, 11.4
	ErrTooEarly                      = NewError(http.StatusTooEarly)                      // RFC 8470, 5.2.
	ErrUpgradeRequired               = NewError(http.StatusUpgradeRequired)               // RFC 9110, 15.5.22
	ErrPreconditionRequired          = NewError(http.StatusPreconditionRequired)          // RFC 6585, 3
	ErrTooManyRequests               = NewError(http.StatusTooManyRequests)               // RFC 6585, 4
	ErrRequestHeaderFieldsTooLarge   = NewError(http.StatusRequestHeaderFieldsTooLarge)   // RFC 6585, 5
	ErrUnavailableForLegalReasons    = NewError(http.StatusUnavailableForLegalReasons)    // RFC 7725, 3
	ErrInternalServerError           = NewError(http.StatusInternalServerError)           // RFC 9110, 15.6.1
	ErrNotImplemented                = NewError(http.StatusNotImplemented)                // RFC 9110, 15.6.2
	ErrBadGateway                    = NewError(http.StatusBadGateway)                    // RFC 9110, 15.6.3
	ErrServiceUnavailable            = NewError(http.StatusServiceUnavailable)            // RFC 9110, 15.6.4
	ErrGatewayTimeout                = NewError(http.StatusGatewayTimeout)                // RFC 9110, 15.6.5
	ErrHTTPVersionNotSupported       = NewError(http.StatusHTTPVersionNotSupported)       // RFC 9110, 15.6.6
	ErrVariantAlsoNegotiates         = NewError(http.StatusVariantAlsoNegotiates)         // RFC 2295, 8.1
	ErrInsufficientStorage           = NewError(http.StatusInsufficientStorage)           // RFC 4918, 11.5
	ErrLoopDetected                  = NewError(http.StatusLoopDetected)                  // RFC 5842, 7.2
	ErrNotExtended                   = NewError(http.StatusNotExtended)                   // RFC 2774, 7
	ErrNetworkAuthenticationRequired = NewError(http.StatusNetworkAuthenticationRequired) // RFC 6585, 6
)
View Source
var HandleError = DefaultErrorHandler
View Source
var Validate = DefaultValidator

Functions

func DefaultErrorHandler

func DefaultErrorHandler(w http.ResponseWriter, r *http.Request, err error)

func DefaultValidator

func DefaultValidator(ctx context.Context, v any) error

func Handle

func Handle[Req, Res any](handler Handler[Req, Res]) http.HandlerFunc

func HandleFunc

func HandleFunc[Req, Res any](handler HandlerFunc[Req, Res]) http.HandlerFunc

Types

type Error

type Error struct {
	// contains filtered or unexported fields
}

func NewError

func NewError(code int) Error

func (Error) Code

func (err Error) Code() int

func (Error) Error

func (err Error) Error() string

func (Error) LogValue

func (err Error) LogValue() slog.Value

func (Error) Render

func (err Error) Render(w http.ResponseWriter, r *http.Request) error

func (Error) Unwrap

func (err Error) Unwrap() error

func (Error) WithInternal

func (err Error) WithInternal(internal error) Error

type Handler

type Handler[Req, Res any] interface {
	Handle(ctx context.Context, req *Req) (*Res, error)
}

type HandlerFunc

type HandlerFunc[Req, Res any] func(ctx context.Context, req *Req) (*Res, error)

func (HandlerFunc[Req, Res]) Handle

func (handle HandlerFunc[Req, Res]) Handle(ctx context.Context, req *Req) (*Res, error)

Jump to

Keyboard shortcuts

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