Mercurial > gemma
comparison pkg/middleware/jsonhandler.go @ 5712:6270951dda28 revive-cleanup
/interface{}/any/
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Tue, 20 Feb 2024 22:37:51 +0100 |
parents | 5f47eeea988d |
children |
comparison
equal
deleted
inserted
replaced
5711:2dd155cc95ec | 5712:6270951dda28 |
---|---|
31 type JSONResult struct { | 31 type JSONResult struct { |
32 // Code is the HTTP status code to be set which defaults to http.StatusOK (200). | 32 // Code is the HTTP status code to be set which defaults to http.StatusOK (200). |
33 Code int | 33 Code int |
34 // Result is serialized to JSON. | 34 // Result is serialized to JSON. |
35 // If the type is an io.Reader its copied through. | 35 // If the type is an io.Reader its copied through. |
36 Result interface{} | 36 Result any |
37 } | 37 } |
38 | 38 |
39 // JSONDefaultLimit is default size limit in bytes of an accepted | 39 // JSONDefaultLimit is default size limit in bytes of an accepted |
40 // input document. | 40 // input document. |
41 const JSONDefaultLimit = 2048 | 41 const JSONDefaultLimit = 2048 |
43 // JSONHandler implements a middleware to ease the handing JSON input | 43 // JSONHandler implements a middleware to ease the handing JSON input |
44 // streams and return JSON documents as output. | 44 // streams and return JSON documents as output. |
45 type JSONHandler struct { | 45 type JSONHandler struct { |
46 // Input (if not nil) is called to fill a data structure | 46 // Input (if not nil) is called to fill a data structure |
47 // returned by this function. | 47 // returned by this function. |
48 Input func(*http.Request) interface{} | 48 Input func(*http.Request) any |
49 // Handle is called to handle the incoming HTTP request. | 49 // Handle is called to handle the incoming HTTP request. |
50 // in is the data structure returned by Input. Its nil if Input is nil. | 50 // in is the data structure returned by Input. Its nil if Input is nil. |
51 Handle func(rep *http.Request) (JSONResult, error) | 51 Handle func(rep *http.Request) (JSONResult, error) |
52 // NoConn if set to true no database connection is established and | 52 // NoConn if set to true no database connection is established and |
53 // the conn parameter of the Handle call is nil. | 53 // the conn parameter of the Handle call is nil. |
87 } | 87 } |
88 return nil | 88 return nil |
89 } | 89 } |
90 | 90 |
91 // JSONInput extracts the de-serialized input from the context of the request. | 91 // JSONInput extracts the de-serialized input from the context of the request. |
92 func JSONInput(req *http.Request) interface{} { | 92 func JSONInput(req *http.Request) any { |
93 return req.Context().Value(jsonHandlerInputKey) | 93 return req.Context().Value(jsonHandlerInputKey) |
94 } | 94 } |
95 | 95 |
96 // ServeHTTP makes the JSONHandler a middleware. | 96 // ServeHTTP makes the JSONHandler a middleware. |
97 func (j *JSONHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) { | 97 func (j *JSONHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) { |
199 } | 199 } |
200 } | 200 } |
201 | 201 |
202 // SendJSON sends data JSON encoded to the response writer | 202 // SendJSON sends data JSON encoded to the response writer |
203 // with a given HTTP status code. | 203 // with a given HTTP status code. |
204 func SendJSON(rw http.ResponseWriter, code int, data interface{}) { | 204 func SendJSON(rw http.ResponseWriter, code int, data any) { |
205 rw.Header().Set("Content-Type", "application/json") | 205 rw.Header().Set("Content-Type", "application/json") |
206 rw.Header().Set("X-Content-Type-Options", "nosniff") | 206 rw.Header().Set("X-Content-Type-Options", "nosniff") |
207 rw.WriteHeader(code) | 207 rw.WriteHeader(code) |
208 if err := json.NewEncoder(rw).Encode(data); err != nil { | 208 if err := json.NewEncoder(rw).Encode(data); err != nil { |
209 log.Errorf("%v\n", err) | 209 log.Errorf("%v\n", err) |