This repository has been archived by the owner on Apr 9, 2024. It is now read-only.
forked from gobuffalo/buffalo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrappers.go
87 lines (76 loc) · 2.49 KB
/
wrappers.go
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package buffalo
import (
"net/http"
"net/url"
"sync"
"github.com/gobuffalo/buffalo/internal/httpx"
"github.com/gorilla/mux"
)
// WrapHandler wraps a standard http.Handler and transforms it
// into a buffalo.Handler.
func WrapHandler(h http.Handler) Handler {
return func(c Context) error {
h.ServeHTTP(c.Response(), c.Request())
return nil
}
}
// WrapHandlerFunc wraps a standard http.HandlerFunc and
// transforms it into a buffalo.Handler.
func WrapHandlerFunc(h http.HandlerFunc) Handler {
return WrapHandler(h)
}
// WrapBuffaloHandler wraps a buffalo.Handler to a standard http.Handler
//
// NOTE: A buffalo Handler expects a buffalo Context. WrapBuffaloHandler uses
// the same logic as DefaultContext where possible, but some functionality
// (e.g. sessions and logging) WILL NOT work with this unwrap function. If
// those features are needed a custom UnwrapHandlerFunc needs to be
// implemented that provides a Context implementing those features.
func WrapBuffaloHandler(h Handler) http.Handler {
return WrapBuffaloHandlerFunc(h)
}
// WrapBuffaloHandlerFunc wraps a buffalo.Handler to a standard http.HandlerFunc
//
// NOTE: A buffalo Handler expects a buffalo Context. WrapBuffaloHandlerFunc uses
// the same logic as DefaultContext where possible, but some functionality
// (e.g. sessions and logging) WILL NOT work with this unwrap function. If
// those features are needed a custom WrapBuffaloHandlerFunc needs to be
// implemented that provides a Context implementing those features.
func WrapBuffaloHandlerFunc(h Handler) http.HandlerFunc {
return func(res http.ResponseWriter, req *http.Request) {
if ws, ok := res.(*Response); ok {
res = ws
}
// Parse URL Params
params := url.Values{}
vars := mux.Vars(req)
for k, v := range vars {
params.Add(k, v)
}
// Parse URL Query String Params
// For POST, PUT, and PATCH requests, it also parse the request body as a form.
// Request body parameters take precedence over URL query string values in params
if err := req.ParseForm(); err == nil {
for k, v := range req.Form {
for _, vv := range v {
params.Add(k, vv)
}
}
}
ct := httpx.ContentType(req)
data := &sync.Map{}
data.Store("current_path", req.URL.Path)
data.Store("contentType", ct)
data.Store("method", req.Method)
c := &DefaultContext{
Context: req.Context(),
contentType: ct,
response: res,
request: req,
params: params,
flash: &Flash{data: map[string][]string{}},
data: data,
}
h(c)
}
}