-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.go
More file actions
64 lines (58 loc) · 2.02 KB
/
middleware.go
File metadata and controls
64 lines (58 loc) · 2.02 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package rsvp
import (
"bytes"
"fmt"
"io"
"log"
"net/http"
"github.com/Teajey/rsvp/internal/dev"
)
// AdaptHandlerFunc wraps a [HandlerFunc] as an [http.HandlerFunc] with the given config.
//
// This is the primary entrypoint to using rsvp.
func AdaptHandlerFunc(cfg Config, next HandlerFunc) http.HandlerFunc {
return func(rw http.ResponseWriter, r *http.Request) {
err := WriteHandler(cfg, rw, r, next)
if err != nil {
log.Printf("[RSVP ERROR]: %s", err)
}
}
}
// WriteHandler writes the result of handler to rw according to cfg.
//
// NOTE: This function is for advanced lower-level use cases.
func WriteHandler(cfg Config, rw http.ResponseWriter, r *http.Request, handler HandlerFunc) error {
var buf bytes.Buffer
status, err := Write(&buf, cfg, rw.Header(), r, handler)
if err != nil {
http.Error(rw, "RSVP failed to write a response", http.StatusInternalServerError)
return fmt.Errorf("writing response: %w", err)
}
err = WriteResponse(status, rw, &buf)
if err != nil {
return fmt.Errorf("writing header: %w", err)
}
return nil
}
// WriteResponse calls w.WriteHeader(status) and copies r to w.
//
// NOTE: This function is for advanced lower-level use cases.
//
// This function, alongside [Write], should be used to wrap [Handler] in middleware that requires _write_ access to [http.ResponseWriter]. [AdaptHandler] and [AdaptHandlerFunc] may be used for simpler standard middleware that does not write to [http.ResponseWriter].
//
// See this test for an example: https://github.com/Teajey/rsvp/blob/main/middleware_test.go
func WriteResponse(status int, w http.ResponseWriter, r io.Reader) error {
dev.Log("Setting status to %d", status)
w.WriteHeader(status)
_, err := io.Copy(w, r)
if err != nil {
return fmt.Errorf("copying to http.ResponseWriter: %w", err)
}
return nil
}
// AdaptHandler wraps a [Handler] as an [http.Handler] with the given config.
//
// This is the primary entrypoint to using rsvp.
func AdaptHandler(config Config, next Handler) http.Handler {
return AdaptHandlerFunc(config, next.ServeHTTP)
}