annotate pkg/middleware/json.go @ 2690:ef59a4255670

Added http middlewares to connect to db and parse input as JSON. This complements the JSONHandler which is for JSON in to JSON out. The new middlewares are for JSON in to abitrary out.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 15 Mar 2019 16:45:34 +0100
parents
children 6f9d00c8cc38
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2690
ef59a4255670 Added http middlewares to connect to db and parse input as JSON. This complements the JSONHandler which is for JSON in to JSON out. The new middlewares are for JSON in to abitrary out.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
1 // This is Free Software under GNU Affero General Public License v >= 3.0
ef59a4255670 Added http middlewares to connect to db and parse input as JSON. This complements the JSONHandler which is for JSON in to JSON out. The new middlewares are for JSON in to abitrary out.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
2 // without warranty, see README.md and license for details.
ef59a4255670 Added http middlewares to connect to db and parse input as JSON. This complements the JSONHandler which is for JSON in to JSON out. The new middlewares are for JSON in to abitrary out.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
3 //
ef59a4255670 Added http middlewares to connect to db and parse input as JSON. This complements the JSONHandler which is for JSON in to JSON out. The new middlewares are for JSON in to abitrary out.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
4 // SPDX-License-Identifier: AGPL-3.0-or-later
ef59a4255670 Added http middlewares to connect to db and parse input as JSON. This complements the JSONHandler which is for JSON in to JSON out. The new middlewares are for JSON in to abitrary out.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
5 // License-Filename: LICENSES/AGPL-3.0.txt
ef59a4255670 Added http middlewares to connect to db and parse input as JSON. This complements the JSONHandler which is for JSON in to JSON out. The new middlewares are for JSON in to abitrary out.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
6 //
ef59a4255670 Added http middlewares to connect to db and parse input as JSON. This complements the JSONHandler which is for JSON in to JSON out. The new middlewares are for JSON in to abitrary out.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
7 // Copyright (C) 2019 by via donau
ef59a4255670 Added http middlewares to connect to db and parse input as JSON. This complements the JSONHandler which is for JSON in to JSON out. The new middlewares are for JSON in to abitrary out.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
8 // – Österreichische Wasserstraßen-Gesellschaft mbH
ef59a4255670 Added http middlewares to connect to db and parse input as JSON. This complements the JSONHandler which is for JSON in to JSON out. The new middlewares are for JSON in to abitrary out.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
9 // Software engineering by Intevation GmbH
ef59a4255670 Added http middlewares to connect to db and parse input as JSON. This complements the JSONHandler which is for JSON in to JSON out. The new middlewares are for JSON in to abitrary out.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
10 //
ef59a4255670 Added http middlewares to connect to db and parse input as JSON. This complements the JSONHandler which is for JSON in to JSON out. The new middlewares are for JSON in to abitrary out.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
11 // Author(s):
ef59a4255670 Added http middlewares to connect to db and parse input as JSON. This complements the JSONHandler which is for JSON in to JSON out. The new middlewares are for JSON in to abitrary out.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de>
ef59a4255670 Added http middlewares to connect to db and parse input as JSON. This complements the JSONHandler which is for JSON in to JSON out. The new middlewares are for JSON in to abitrary out.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
13
ef59a4255670 Added http middlewares to connect to db and parse input as JSON. This complements the JSONHandler which is for JSON in to JSON out. The new middlewares are for JSON in to abitrary out.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
14 package middleware
ef59a4255670 Added http middlewares to connect to db and parse input as JSON. This complements the JSONHandler which is for JSON in to JSON out. The new middlewares are for JSON in to abitrary out.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
15
ef59a4255670 Added http middlewares to connect to db and parse input as JSON. This complements the JSONHandler which is for JSON in to JSON out. The new middlewares are for JSON in to abitrary out.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
16 import (
ef59a4255670 Added http middlewares to connect to db and parse input as JSON. This complements the JSONHandler which is for JSON in to JSON out. The new middlewares are for JSON in to abitrary out.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
17 "context"
ef59a4255670 Added http middlewares to connect to db and parse input as JSON. This complements the JSONHandler which is for JSON in to JSON out. The new middlewares are for JSON in to abitrary out.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
18 "encoding/json"
ef59a4255670 Added http middlewares to connect to db and parse input as JSON. This complements the JSONHandler which is for JSON in to JSON out. The new middlewares are for JSON in to abitrary out.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
19 "io"
ef59a4255670 Added http middlewares to connect to db and parse input as JSON. This complements the JSONHandler which is for JSON in to JSON out. The new middlewares are for JSON in to abitrary out.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
20 "net/http"
ef59a4255670 Added http middlewares to connect to db and parse input as JSON. This complements the JSONHandler which is for JSON in to JSON out. The new middlewares are for JSON in to abitrary out.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
21 )
ef59a4255670 Added http middlewares to connect to db and parse input as JSON. This complements the JSONHandler which is for JSON in to JSON out. The new middlewares are for JSON in to abitrary out.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
22
ef59a4255670 Added http middlewares to connect to db and parse input as JSON. This complements the JSONHandler which is for JSON in to JSON out. The new middlewares are for JSON in to abitrary out.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
23 type jsonInputKeyType int
ef59a4255670 Added http middlewares to connect to db and parse input as JSON. This complements the JSONHandler which is for JSON in to JSON out. The new middlewares are for JSON in to abitrary out.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
24
ef59a4255670 Added http middlewares to connect to db and parse input as JSON. This complements the JSONHandler which is for JSON in to JSON out. The new middlewares are for JSON in to abitrary out.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
25 const jsonInputKey jsonInputKeyType = 0
ef59a4255670 Added http middlewares to connect to db and parse input as JSON. This complements the JSONHandler which is for JSON in to JSON out. The new middlewares are for JSON in to abitrary out.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
26
ef59a4255670 Added http middlewares to connect to db and parse input as JSON. This complements the JSONHandler which is for JSON in to JSON out. The new middlewares are for JSON in to abitrary out.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
27 const DefaultLimit = 2048
ef59a4255670 Added http middlewares to connect to db and parse input as JSON. This complements the JSONHandler which is for JSON in to JSON out. The new middlewares are for JSON in to abitrary out.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
28
ef59a4255670 Added http middlewares to connect to db and parse input as JSON. This complements the JSONHandler which is for JSON in to JSON out. The new middlewares are for JSON in to abitrary out.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
29 func GetJSONInput(req *http.Request) interface{} {
ef59a4255670 Added http middlewares to connect to db and parse input as JSON. This complements the JSONHandler which is for JSON in to JSON out. The new middlewares are for JSON in to abitrary out.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
30 return req.Context().Value(jsonInputKey)
ef59a4255670 Added http middlewares to connect to db and parse input as JSON. This complements the JSONHandler which is for JSON in to JSON out. The new middlewares are for JSON in to abitrary out.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
31 }
ef59a4255670 Added http middlewares to connect to db and parse input as JSON. This complements the JSONHandler which is for JSON in to JSON out. The new middlewares are for JSON in to abitrary out.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
32
ef59a4255670 Added http middlewares to connect to db and parse input as JSON. This complements the JSONHandler which is for JSON in to JSON out. The new middlewares are for JSON in to abitrary out.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
33 func JSONInput(next http.Handler, input func(*http.Request) interface{}, limit int64) http.Handler {
ef59a4255670 Added http middlewares to connect to db and parse input as JSON. This complements the JSONHandler which is for JSON in to JSON out. The new middlewares are for JSON in to abitrary out.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
34
ef59a4255670 Added http middlewares to connect to db and parse input as JSON. This complements the JSONHandler which is for JSON in to JSON out. The new middlewares are for JSON in to abitrary out.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
35 return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
ef59a4255670 Added http middlewares to connect to db and parse input as JSON. This complements the JSONHandler which is for JSON in to JSON out. The new middlewares are for JSON in to abitrary out.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
36 dst := input(req)
ef59a4255670 Added http middlewares to connect to db and parse input as JSON. This complements the JSONHandler which is for JSON in to JSON out. The new middlewares are for JSON in to abitrary out.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
37 defer req.Body.Close()
ef59a4255670 Added http middlewares to connect to db and parse input as JSON. This complements the JSONHandler which is for JSON in to JSON out. The new middlewares are for JSON in to abitrary out.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
38 var r io.Reader
ef59a4255670 Added http middlewares to connect to db and parse input as JSON. This complements the JSONHandler which is for JSON in to JSON out. The new middlewares are for JSON in to abitrary out.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
39 switch {
ef59a4255670 Added http middlewares to connect to db and parse input as JSON. This complements the JSONHandler which is for JSON in to JSON out. The new middlewares are for JSON in to abitrary out.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
40 case limit == 0:
ef59a4255670 Added http middlewares to connect to db and parse input as JSON. This complements the JSONHandler which is for JSON in to JSON out. The new middlewares are for JSON in to abitrary out.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
41 r = io.LimitReader(req.Body, DefaultLimit)
ef59a4255670 Added http middlewares to connect to db and parse input as JSON. This complements the JSONHandler which is for JSON in to JSON out. The new middlewares are for JSON in to abitrary out.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
42 case limit > 0:
ef59a4255670 Added http middlewares to connect to db and parse input as JSON. This complements the JSONHandler which is for JSON in to JSON out. The new middlewares are for JSON in to abitrary out.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
43 r = io.LimitReader(req.Body, limit)
ef59a4255670 Added http middlewares to connect to db and parse input as JSON. This complements the JSONHandler which is for JSON in to JSON out. The new middlewares are for JSON in to abitrary out.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
44 default:
ef59a4255670 Added http middlewares to connect to db and parse input as JSON. This complements the JSONHandler which is for JSON in to JSON out. The new middlewares are for JSON in to abitrary out.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
45 r = req.Body
ef59a4255670 Added http middlewares to connect to db and parse input as JSON. This complements the JSONHandler which is for JSON in to JSON out. The new middlewares are for JSON in to abitrary out.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
46 }
ef59a4255670 Added http middlewares to connect to db and parse input as JSON. This complements the JSONHandler which is for JSON in to JSON out. The new middlewares are for JSON in to abitrary out.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
47 if err := json.NewDecoder(r).Decode(dst); err != nil {
ef59a4255670 Added http middlewares to connect to db and parse input as JSON. This complements the JSONHandler which is for JSON in to JSON out. The new middlewares are for JSON in to abitrary out.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
48 http.Error(rw, "error: "+err.Error(), http.StatusBadRequest)
ef59a4255670 Added http middlewares to connect to db and parse input as JSON. This complements the JSONHandler which is for JSON in to JSON out. The new middlewares are for JSON in to abitrary out.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
49 return
ef59a4255670 Added http middlewares to connect to db and parse input as JSON. This complements the JSONHandler which is for JSON in to JSON out. The new middlewares are for JSON in to abitrary out.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
50 }
ef59a4255670 Added http middlewares to connect to db and parse input as JSON. This complements the JSONHandler which is for JSON in to JSON out. The new middlewares are for JSON in to abitrary out.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
51 parent := req.Context()
ef59a4255670 Added http middlewares to connect to db and parse input as JSON. This complements the JSONHandler which is for JSON in to JSON out. The new middlewares are for JSON in to abitrary out.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
52 ctx := context.WithValue(parent, jsonInputKey, input)
ef59a4255670 Added http middlewares to connect to db and parse input as JSON. This complements the JSONHandler which is for JSON in to JSON out. The new middlewares are for JSON in to abitrary out.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
53 req = req.WithContext(ctx)
ef59a4255670 Added http middlewares to connect to db and parse input as JSON. This complements the JSONHandler which is for JSON in to JSON out. The new middlewares are for JSON in to abitrary out.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
54 next.ServeHTTP(rw, req)
ef59a4255670 Added http middlewares to connect to db and parse input as JSON. This complements the JSONHandler which is for JSON in to JSON out. The new middlewares are for JSON in to abitrary out.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
55 })
ef59a4255670 Added http middlewares to connect to db and parse input as JSON. This complements the JSONHandler which is for JSON in to JSON out. The new middlewares are for JSON in to abitrary out.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
56 }