stahp

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2024 License: BSD-3-Clause Imports: 2 Imported by: 0

README

stahp

Strongly-Typed Adapters for HTTP-request Processing

Documentation

Overview

Package stahp provides enablement mechanisms for marshaling HTTP requests to strongly-typed handlers functions and marshaling the results back as a response.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NoReqParser

func NoReqParser(*http.Request) (struct{}, error)

NoReqParser is a convenience function to satisfy the requirement for a RequestParser when marshaling requests to a NoReqParser.

func Route

func Route[Req any, Resp any](
	target Target[Req, Resp],
	parser RequestParser[Req],
	responder Responder[Resp],
) http.HandlerFunc

Route generates an http.HandlerFunc from a RequestParser, a Target, and a Responder.

Types

type NoReqTarget

type NoReqTarget[Resp any] func(context.Context) (Resp, error)

A NoReqTarget is a function that can be used with NoReq to create a Target that doesn't need any input from the HTTP request.

type RequestParser

type RequestParser[Req any] func(*http.Request) (Req, error)

A RequestParser extracts a strongly-typed request from an HTTP request.

type Responder

type Responder[Resp any] interface {

	// Write marshals responses from a [Target] to an HTTP response.
	Write(Resp, http.ResponseWriter, *http.Request)

	// WriteParseErr marshals errors that occur parsing an HTTP request.
	WriteParseErr(error, http.ResponseWriter, *http.Request)

	// WriteErr marshals errors from [Target] to an HTTP response.
	WriteErr(error, http.ResponseWriter, *http.Request)
}

A Responder marshals responses and errors to an HTTP response.

func NewResponder

func NewResponder[Resp any](
	write ResponseWriter[Resp],
	writeParseErr ResponseWriter[error],
	writeErr ResponseWriter[error],
) Responder[Resp]

NewResponder builds a Responder from a ResponseWriter for each

type ResponseWriter

type ResponseWriter[T any] func(T, http.ResponseWriter, *http.Request)

A ResponseWriter marshals a value to an HTTP response.

type Target

type Target[Req any, Resp any] func(context.Context, Req) (Resp, error)

A Target is a strongly-typed function taking a request and returning a response or an error. Marshaling instances of HTTP requests to a Target and marshaling the response or the error back as a response is the mission of the stahp package.

func NoReq

func NoReq[Resp any](target NoReqTarget[Resp]) Target[struct{}, Resp]

NoReq builds a Target from a NoReqTarget.

Directories

Path Synopsis
examples
basic
This is an example of the most basic use of the github.com/ttd2089/stahp package.
This is an example of the most basic use of the github.com/ttd2089/stahp package.

Jump to

Keyboard shortcuts

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