-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmethods.go
150 lines (132 loc) · 4.02 KB
/
methods.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Tideland Go HTTP Extensions
//
// Copyright (C) 2020-2022 Frank Mueller / Tideland / Oldenburg / Germany
//
// All rights reserved. Use of this source code is governed
// by the new BSD license.
package httpx // import "tideland.dev/go/httpx"
//--------------------
// IMPORTS
//--------------------
import (
"net/http"
)
//--------------------
// METHOD HANDLER INTERFACES
//--------------------
// GetHandler has to be implemented by a handler for GET requests
// dispatched through the MethodHandler.
type GetHandler interface {
ServeHTTPGet(w http.ResponseWriter, r *http.Request)
}
// HeadHandler has to be implemented by a handler for HEAD requests
// dispatched through the MethodHandler.
type HeadHandler interface {
ServeHTTPHead(w http.ResponseWriter, r *http.Request)
}
// PostHandler has to be implemented by a handler for POST requests
// dispatched through the MethodHandler.
type PostHandler interface {
ServeHTTPPost(w http.ResponseWriter, r *http.Request)
}
// PutHandler has to be implemented by a handler for PUT requests
// dispatched through the MethodHandler.
type PutHandler interface {
ServeHTTPPut(w http.ResponseWriter, r *http.Request)
}
// PatchHandler has to be implemented by a handler for PATCH requests
// dispatched through the MethodHandler.
type PatchHandler interface {
ServeHTTPPatch(w http.ResponseWriter, r *http.Request)
}
// DeleteHandler has to be implemented by a handler for DELETE requests
// dispatched through the MethodHandler.
type DeleteHandler interface {
ServeHTTPDelete(w http.ResponseWriter, r *http.Request)
}
// ConnectHandler has to be implemented by a handler for CONNECT requests
// dispatched through the MethodHandler.
type ConnectHandler interface {
ServeHTTPConnect(w http.ResponseWriter, r *http.Request)
}
// OptionsHandler has to be implemented by a handler for OPTIONS requests
// dispatched through the MethodHandler.
type OptionsHandler interface {
ServeHTTPOptions(w http.ResponseWriter, r *http.Request)
}
// TraceHandler has to be implemented by a handler for TRACE requests
// dispatched through the MethodHandler.
type TraceHandler interface {
ServeHTTPTrace(w http.ResponseWriter, r *http.Request)
}
//--------------------
// METHOD HANDLER
//--------------------
// MethodHandler wraps a http.Handler implementing also individual httpx handler
// interfaces. It distributes the requests to the handler methods if those are
// implemented.
type MethodHandler struct {
handler http.Handler
}
// NewMethodHandler returns a new method handler.
func NewMethodHandler(h http.Handler) *MethodHandler {
return &MethodHandler{
handler: h,
}
}
// ServeHTTP implements the http.Handler interface. If the wrapped handler implements
// the matching interface for the HTTP request method the according ServeHTTP<method>()
// method will be called. Other it simply calls the default ServeHTTP() method.
func (h *MethodHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
if hh, ok := h.handler.(GetHandler); ok {
hh.ServeHTTPGet(w, r)
return
}
case http.MethodPost:
if hh, ok := h.handler.(PostHandler); ok {
hh.ServeHTTPPost(w, r)
return
}
case http.MethodPut:
if hh, ok := h.handler.(PutHandler); ok {
hh.ServeHTTPPut(w, r)
return
}
case http.MethodDelete:
if hh, ok := h.handler.(DeleteHandler); ok {
hh.ServeHTTPDelete(w, r)
return
}
case http.MethodHead:
if hh, ok := h.handler.(HeadHandler); ok {
hh.ServeHTTPHead(w, r)
return
}
case http.MethodPatch:
if hh, ok := h.handler.(PatchHandler); ok {
hh.ServeHTTPPatch(w, r)
return
}
case http.MethodConnect:
if hh, ok := h.handler.(ConnectHandler); ok {
hh.ServeHTTPConnect(w, r)
return
}
case http.MethodOptions:
if hh, ok := h.handler.(OptionsHandler); ok {
hh.ServeHTTPOptions(w, r)
return
}
case http.MethodTrace:
if hh, ok := h.handler.(TraceHandler); ok {
hh.ServeHTTPTrace(w, r)
return
}
}
// Fall back to default for no matching handler method or any
// other HTTP method.
h.handler.ServeHTTP(w, r)
}
// EOF