summaryrefslogtreecommitdiff
path: root/gemini/handler.go
blob: ded77a5e734fa7fa115d20d2cab868432b4550dd (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
29
package gemini

import "context"

// Handler is a function which can turn a gemini request into a gemini response.
//
// A Handler MUST NOT return a nil response. Errors should be returned in the form
// of error responses (4x, 5x, 6x response status). If the Handler should not be
// responsible for the requested resource it can return a "51 Not Found" response.
type Handler func(context.Context, *Request) *Response

// Middleware is a handle 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 Fallthrough(handlers ...Handler) Handler {
	return func(ctx context.Context, req *Request) *Response {
		for _, handler := range handlers {
			response := handler(ctx, req)
			if response.Status != StatusNotFound {
				return response
			}
		}

		return NotFound("Resource does not exist.")
	}
}