endpoint

package
v0.22.2 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2025 License: MIT Imports: 5 Imported by: 0

README

Endpoint

Look. I set a goal for myself to minimize the number of libraries I import when creating polyform

It's generic, for better or for worst.

This works. It's "fine". But there are way better libraries to use. Probably shouldn't use this package for your personal projects.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BinaryRequestReader

type BinaryRequestReader struct{}

func (BinaryRequestReader) Interpret

func (jrbi BinaryRequestReader) Interpret(r *http.Request) ([]byte, error)

type BinaryResponseWriter

type BinaryResponseWriter struct{}

func (BinaryResponseWriter) ContentType

func (jrw BinaryResponseWriter) ContentType() ContentType

func (BinaryResponseWriter) Serialize

func (jrw BinaryResponseWriter) Serialize(w http.ResponseWriter, response []byte) (err error)

type BodyMethod

type BodyMethod[Body any] struct {
	Request RequestReader[Body]
	Handler func(request Request[Body]) error
}

func (BodyMethod[Body]) ContentType

func (jse BodyMethod[Body]) ContentType() ContentType

func (BodyMethod[Body]) Handle

func (jse BodyMethod[Body]) Handle(w http.ResponseWriter, r *http.Request)

type BodyResponseMethod

type BodyResponseMethod[Body any, Response any] struct {
	Request        RequestReader[Body]
	ResponseWriter ResponseWriter[Response]
	Handler        func(request Request[Body]) (Response, error)
}

func JsonMethod

func JsonMethod[Body any, Response any](handler func(request Request[Body]) (Response, error)) BodyResponseMethod[Body, Response]

func (BodyResponseMethod[Body, Response]) ContentType

func (jse BodyResponseMethod[Body, Response]) ContentType() ContentType

func (BodyResponseMethod[Body, Response]) Handle

func (jse BodyResponseMethod[Body, Response]) Handle(w http.ResponseWriter, r *http.Request)

type ContentType

type ContentType string
const (
	BinaryContentType    ContentType = "application/octet-stream"
	JsonContentType      ContentType = "application/json"
	PlainTextContentType ContentType = "text/plain"
)

type Func added in v0.21.0

type Func func(r *http.Request) error

func (Func) ContentType added in v0.21.0

func (jse Func) ContentType() ContentType

func (Func) Handle added in v0.21.0

func (jse Func) Handle(w http.ResponseWriter, r *http.Request)

type Handler

type Handler struct {
	Methods map[string]Method
}

func (Handler) ServeHTTP

func (se Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

type JsonRequestReader

type JsonRequestReader[T any] struct{}

func (JsonRequestReader[T]) Interpret

func (jrbi JsonRequestReader[T]) Interpret(r *http.Request) (T, error)

type JsonResponseWriter

type JsonResponseWriter[Response any] struct{}

func (JsonResponseWriter[Response]) ContentType

func (jrw JsonResponseWriter[Response]) ContentType() ContentType

func (JsonResponseWriter[Response]) Serialize

func (jrw JsonResponseWriter[Response]) Serialize(w http.ResponseWriter, response Response) (err error)

type Method

type Method interface {
	ContentType() ContentType
	Handle(w http.ResponseWriter, r *http.Request)
}

type Request

type Request[Body any] struct {
	Body Body
	Url  string
}

type RequestReader

type RequestReader[T any] interface {
	Interpret(r *http.Request) (T, error)
}

type ResponseMethod

type ResponseMethod[Response any] struct {
	ResponseWriter ResponseWriter[Response]
	Handler        func(r *http.Request) (Response, error)
}

func (ResponseMethod[Response]) ContentType

func (jse ResponseMethod[Response]) ContentType() ContentType

func (ResponseMethod[Response]) Handle

func (jse ResponseMethod[Response]) Handle(w http.ResponseWriter, r *http.Request)

type ResponseWriter

type ResponseWriter[Response any] interface {
	Serialize(w http.ResponseWriter, response Response) (err error)
	ContentType() ContentType
}

type TextRequestReader added in v0.21.0

type TextRequestReader struct{}

func (TextRequestReader) Interpret added in v0.21.0

func (jrbi TextRequestReader) Interpret(r *http.Request) (string, error)

type TextResponseWriter added in v0.21.0

type TextResponseWriter struct{}

func (TextResponseWriter) ContentType added in v0.21.0

func (jrw TextResponseWriter) ContentType() ContentType

func (TextResponseWriter) Serialize added in v0.21.0

func (jrw TextResponseWriter) Serialize(w http.ResponseWriter, response string) (err error)

Jump to

Keyboard shortcuts

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