From 23bc5f4fb7542e64c94eaa7fe2c7a6aa55010898 Mon Sep 17 00:00:00 2001 From: tjpcc Date: Sat, 12 Aug 2023 09:40:39 -0600 Subject: move common types to an internal package This helps avoid import cycles. --- internal/types/handler.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 internal/types/handler.go (limited to 'internal/types/handler.go') diff --git a/internal/types/handler.go b/internal/types/handler.go new file mode 100644 index 0000000..4805c57 --- /dev/null +++ b/internal/types/handler.go @@ -0,0 +1,28 @@ +package types + +import "context" + +// Handler is a type which can turn a request into a response. +// +// Handle may return a nil response, in which case the Server is expected +// to build the protocol-appropriate "Not Found" response. +type Handler interface { + Handle(context.Context, *Request) *Response +} + +// Middleware is a handler decorator. +// +// It returns a handler which may call the passed-in handler or not, or may +// transform the request or response in some way. +type Middleware func(Handler) Handler + +func HandlerFunc(f func(context.Context, *Request) *Response) Handler { + return handlerFunc(f) +} + +// Handle implements Handler. +func (f handlerFunc) Handle(ctx context.Context, request *Request) *Response { + return f(ctx, request) +} + +type handlerFunc func(context.Context, *Request) *Response -- cgit v1.2.3