summaryrefslogtreecommitdiff
path: root/internal/types/handler.go
blob: 4805c57ffe0acf25615b947d88501f88e16d189e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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