endpoints

package module
v0.0.0-...-c43521c Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2024 License: MIT Imports: 9 Imported by: 0

README

endpoints

Structured endpoints in Go.

The HTTP library in Go is powerful and extendable, and I think it allows us to easily extend it into structured endpoints to more easily separate our logic and write tests with less setup.

Example

This will handle our JSON (de)serialization, errors, status codes, timeouts, etc. and it's easily configured by implementing a few interfaces.

package main

import (
	"fmt"
	"github.com/aliics/endpoints"
	"log/slog"
	"net/http"
)

type nameInput struct {
	Name string `json:"name"`
}

type greetingOutput struct {
	Result string `json:"result"`
}

type helloWorldEndpoint struct{}

func (e helloWorldEndpoint) EndpointPattern() string { return "POST /hello" }

func (e helloWorldEndpoint) Handle(in nameInput) (*greetingOutput, error) {
	return &greetingOutput{fmt.Sprintf("Hello, %s!", in.Name)}, nil
}

func main() {
	mux := endpoints.NewEndpointsMux(helloWorldEndpoint{})

	if err := http.ListenAndServe(":8080", mux); err != nil {
		slog.Error("http server failure", "err", err)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewEndpointsMux

func NewEndpointsMux(endpoints ...EndpointHandler) http.Handler

NewEndpointsMux will create a new mux with the given [EndpointHandler]s. This will also be setup with the appropriate routing, so they can serve HTTP requests.

Types

type EndpointHandler

type EndpointHandler interface {
	EndpointPattern() string
}

EndpointHandler allows for the registration of an endpoint with a route. The implementation will also need a "Handle" method.

type EndpointWithTimeout

type EndpointWithTimeout interface {
	WithTimeout() time.Duration
}

type HandlerError

type HandlerError struct {
	StatusCode int
	Message    string
}

HandlerError is the error to return from an [EndpointHandler] handler method to return with a message and status back to the caller.

func BadGatewayError

func BadGatewayError(message string) HandlerError

func BadRequestError

func BadRequestError(message string) HandlerError

func ConflictError

func ConflictError(message string) HandlerError

func ExpectationFailedError

func ExpectationFailedError(message string) HandlerError

func FailedDependencyError

func FailedDependencyError(message string) HandlerError

func ForbiddenError

func ForbiddenError(message string) HandlerError

func GatewayTimeoutError

func GatewayTimeoutError(message string) HandlerError

func GoneError

func GoneError(message string) HandlerError

func HTTPVersionNotSupportedError

func HTTPVersionNotSupportedError(message string) HandlerError

func InsufficientStorageError

func InsufficientStorageError(message string) HandlerError

func InternalServerErrorError

func InternalServerErrorError(message string) HandlerError

func LengthRequiredError

func LengthRequiredError(message string) HandlerError

func LockedError

func LockedError(message string) HandlerError

func LoopDetectedError

func LoopDetectedError(message string) HandlerError

func MethodNotAllowedError

func MethodNotAllowedError(message string) HandlerError

func MisdirectedRequestError

func MisdirectedRequestError(message string) HandlerError

func NetworkAuthenticationRequiredError

func NetworkAuthenticationRequiredError(message string) HandlerError

func NewHandlerError

func NewHandlerError(code int, message string) HandlerError

func NotAcceptableError

func NotAcceptableError(message string) HandlerError

func NotExtendedError

func NotExtendedError(message string) HandlerError

func NotFoundError

func NotFoundError(message string) HandlerError

func NotImplementedError

func NotImplementedError(message string) HandlerError

func PaymentRequiredError

func PaymentRequiredError(message string) HandlerError

func PreconditionFailedError

func PreconditionFailedError(message string) HandlerError

func PreconditionRequiredError

func PreconditionRequiredError(message string) HandlerError

func ProxyAuthRequiredError

func ProxyAuthRequiredError(message string) HandlerError

func RequestEntityTooLargeError

func RequestEntityTooLargeError(message string) HandlerError

func RequestHeaderFieldsTooLargeError

func RequestHeaderFieldsTooLargeError(message string) HandlerError

func RequestTimeoutError

func RequestTimeoutError(message string) HandlerError

func RequestURITooLongError

func RequestURITooLongError(message string) HandlerError

func RequestedRangeNotSatisfiableError

func RequestedRangeNotSatisfiableError(message string) HandlerError

func ServiceUnavailableError

func ServiceUnavailableError(message string) HandlerError

func TeapotError

func TeapotError(message string) HandlerError

func TooEarlyError

func TooEarlyError(message string) HandlerError

func TooManyRequestsError

func TooManyRequestsError(message string) HandlerError

func UnauthorizedError

func UnauthorizedError(message string) HandlerError

func UnavailableForLegalReasonsError

func UnavailableForLegalReasonsError(message string) HandlerError

func UnprocessableEntityError

func UnprocessableEntityError(message string) HandlerError

func UnsupportedMediaTypeError

func UnsupportedMediaTypeError(message string) HandlerError

func UpgradeRequiredError

func UpgradeRequiredError(message string) HandlerError

func VariantAlsoNegotiatesError

func VariantAlsoNegotiatesError(message string) HandlerError

func (HandlerError) Error

func (h HandlerError) Error() string

type MiddlewareFromSlice

type MiddlewareFromSlice struct {
	Middlewares []MiddlewareFunc
}

func (MiddlewareFromSlice) Middleware

func (m MiddlewareFromSlice) Middleware() []MiddlewareFunc

type MiddlewareFunc

type MiddlewareFunc func(w http.ResponseWriter, r *http.Request) func(next http.Handler)

type MiddlewareHandler

type MiddlewareHandler interface {
	Middleware() []MiddlewareFunc
}

MiddlewareHandler allows for [MiddlewareFunc]s to be attached to an EndpointHandler.

type Validator

type Validator interface {
	Validate() error
}

Validator should be used to validate JSON inputs for a request body.

Jump to

Keyboard shortcuts

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